Class: MacAppSync::Persistence

Inherits:
Object
  • Object
show all
Defined in:
lib/mac_app_sync/persistence.rb

Constant Summary collapse

SAVE_PATH =
Pathname.new("~/.app_prefs").expand_path
PATH_REPLACEMENTS =
{
  "/" => "%-",
  ":" => "%.",
  "%" => "%%"
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace) ⇒ Persistence

Returns a new instance of Persistence.



18
19
20
# File 'lib/mac_app_sync/persistence.rb', line 18

def initialize(namespace)
  @root_path = SAVE_PATH.join(namespace)
end

Class Method Details

.write(name, content, path = SAVE_PATH) ⇒ Object



14
15
16
# 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

Instance Method Details

#each_fileObject



40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



27
28
29
30
31
32
33
34
# 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!Object



22
23
24
25
# 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) ⇒ Object



36
37
38
# File 'lib/mac_app_sync/persistence.rb', line 36

def write(name, content)
  self.class.write(escape_path(name), content, @save_path)
end