Class: Polytrix::StateFile

Inherits:
Object
  • Object
show all
Defined in:
lib/polytrix/state_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(polytrix_root, name) ⇒ StateFile

Returns a new instance of StateFile.



6
7
8
9
10
# File 'lib/polytrix/state_file.rb', line 6

def initialize(polytrix_root, name)
  @file_name = File.expand_path(
    File.join(polytrix_root, '.polytrix', "#{name}.yml")
  )
end

Instance Method Details

#destroyObject



28
29
30
# File 'lib/polytrix/state_file.rb', line 28

def destroy
  FileUtils.rm_f(file_name) if File.exist?(file_name)
end

#readObject



12
13
14
15
16
17
18
# File 'lib/polytrix/state_file.rb', line 12

def read
  if File.exist?(file_name)
    Hashie::Mash.load(file_name).dup || {}
  else
    Hashie::Mash.new
  end
end

#write(state) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/polytrix/state_file.rb', line 20

def write(state)
  dir = File.dirname(file_name)
  serialized_string = serialize_hash(Util.stringified_hash(state))

  FileUtils.mkdir_p(dir)
  File.open(file_name, 'wb') { |f| f.write(serialized_string) }
end