class Bow::Locker
Constants
- LINE_SEP
- NOT_FOUND
- SEPARATOR
Attributes
file_path[RW]
runtime_cache[RW]
Public Class Methods
load()
click to toggle source
# File lib/bow/locker.rb, line 11 def load @instance ||= new end
load!()
click to toggle source
# File lib/bow/locker.rb, line 15 def load! @instance = new end
new()
click to toggle source
# File lib/bow/locker.rb, line 26 def initialize @file_path = self.class.file_path || Config.guest[:history] @modified = false @file_opened = false @runtime_cache = {} end
Public Instance Methods
add(task, applied = false, reverted = false)
click to toggle source
rubocop:enable Style/PerlBackrefs rubocop:enable Metrics/MethodLength
# File lib/bow/locker.rb, line 121 def add(task, applied = false, reverted = false) reset(task) file.seek(0, IO::SEEK_END) write(task, applied, reverted) self end
applied?(task)
click to toggle source
rubocop:disable Style/SafeNavigation
# File lib/bow/locker.rb, line 34 def applied?(task) record = parse(find(task)) !!(record && record[1]) end
apply(task)
click to toggle source
rubocop:enable Style/SafeNavigation
# File lib/bow/locker.rb, line 45 def apply(task) return if applied?(task) add(task, true, reverted?(task)) end
compact()
click to toggle source
# File lib/bow/locker.rb, line 147 def compact compressed = '' file.each do |l| compressed << l unless l[0] == ' ' end file.rewind file.write(compressed) file.truncate(compressed.size) @runtime_cache = {} @modified = true self end
empty!()
click to toggle source
# File lib/bow/locker.rb, line 160 def empty! file.rewind file.truncate(0) @runtime_cache = {} end
ensure_file(name)
click to toggle source
# File lib/bow/locker.rb, line 174 def ensure_file(name) return if File.exist?(name) FileUtils.mkdir_p(File.dirname(name)) FileUtils.touch(name) end
file()
click to toggle source
# File lib/bow/locker.rb, line 166 def file return @file if @file && @file_opened ensure_file(@file_path) @file = File.open(@file_path, 'r+') @file_opened = true file end
find(task)
click to toggle source
# File lib/bow/locker.rb, line 77 def find(task) cached = from_cache(task) return cached if cached result = pure_find(task) result end
flush()
click to toggle source
# File lib/bow/locker.rb, line 139 def flush return unless @modified && @file_opened file.close @file_opened = false @modified = false self end
from_cache(task)
click to toggle source
# File lib/bow/locker.rb, line 84 def from_cache(task) @runtime_cache[task] end
parse(meta)
click to toggle source
# File lib/bow/locker.rb, line 55 def parse(meta) return false if meta[:record] == NOT_FOUND record = meta[:record].split(SEPARATOR) record.map do |v| v.strip! case v when 'true' then true when 'false' then false else v end end end
pure_find(task)
click to toggle source
rubocop:disable Metrics/MethodLength rubocop:disable Style/PerlBackrefs
# File lib/bow/locker.rb, line 98 def pure_find(task) file.rewind current_line = '' first_c = 0 last_c = 0 file.each_char.with_index do |c, idx| if c == LINE_SEP current_line =~ /^([^\s]+)#{SEPARATOR}/ to_cache($1, record: current_line, first_c: first_c, last_c: last_c) from_cache(task) if task == $1 current_line = '' first_c = idx + 1 last_c = first_c else current_line << c last_c = idx end end { record: NOT_FOUND, first_c: nil, last_c: nil } end
reset(task)
click to toggle source
# File lib/bow/locker.rb, line 68 def reset(task) meta = find(task) reset_cache(task) return if meta[:record] == NOT_FOUND file.seek(meta[:first_c], IO::SEEK_SET) str_len = meta[:last_c] - meta[:first_c] + 1 file.write ' ' * str_len end
reset_cache(task)
click to toggle source
# File lib/bow/locker.rb, line 92 def reset_cache(task) @runtime_cache[task] = nil end
revert(task)
click to toggle source
# File lib/bow/locker.rb, line 50 def revert(task) return if reverted?(task) add(task, applied?(task), true) end
reverted?(task)
click to toggle source
# File lib/bow/locker.rb, line 39 def reverted?(task) record = parse(find(task)) !!(record && record[2]) end
to_cache(task, result)
click to toggle source
# File lib/bow/locker.rb, line 88 def to_cache(task, result) @runtime_cache[task] = result end
write(task, applied = false, reverted = false)
click to toggle source
# File lib/bow/locker.rb, line 128 def write(task, applied = false, reverted = false) reset_cache(task) record = [task, applied, reverted].join(SEPARATOR) first_c = file.tell file.puts(record) last_c = first_c + record.size - 1 to_cache(task, record: record, first_c: first_c, last_c: last_c) @modified = true self end