class MacAppSync::Persistence

Constants

PATH_REPLACEMENTS
SAVE_PATH

Public Class Methods

new(namespace) click to toggle source
# File lib/mac_app_sync/persistence.rb, line 18
def initialize(namespace)
  @root_path = SAVE_PATH.join(namespace)
end
write(name, content, path = SAVE_PATH) click to toggle source
# File lib/mac_app_sync/persistence.rb, line 14
def self.write(name, content, path = SAVE_PATH)
  path.join(name).write(JSON.pretty_generate(content))
end

Public Instance Methods

each_file() { |basename.to_s, basename.to_s, content| ... } click to toggle source
# File lib/mac_app_sync/persistence.rb, line 40
def each_file
  @root_path.each_child do |group|
    group.each_child do |file|
      content = JSON.parse(file.read)

      yield group.basename.to_s, file.basename.to_s, content
    end
  end
end
in_dir(name) { || ... } click to toggle source
# File lib/mac_app_sync/persistence.rb, line 27
def in_dir(name)
  @save_path = @root_path.join(escape_path(name))
  @save_path.mkpath

  yield

  @save_path = @root_path
end
reset!() click to toggle source
# File lib/mac_app_sync/persistence.rb, line 22
def reset!
  @root_path.rmtree if @root_path.exist?
  @root_path.mkpath
end
write(name, content) click to toggle source
# File lib/mac_app_sync/persistence.rb, line 36
def write(name, content)
  self.class.write(escape_path(name), content, @save_path)
end

Private Instance Methods

escape_path(str) click to toggle source
# File lib/mac_app_sync/persistence.rb, line 52
def escape_path(str)
  pattern = "[#{PATH_REPLACEMENTS.keys.join}]"
  str.gsub(Regexp.new(pattern), PATH_REPLACEMENTS)
end
unescape_path(str) click to toggle source
# File lib/mac_app_sync/persistence.rb, line 57
def unescape_path(str)
  str.gsub(/%./, PATH_REPLACEMENTS.invert)
end