Module: ZombieFans::Actions::Persist

Included in:
Robot
Defined in:
lib/zombie_fans/actions/persist.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ATTRIBUTES =
%w(name login email password repos)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.pathObject

Returns the value of attribute path.



22
23
24
# File 'lib/zombie_fans/actions/persist.rb', line 22

def path
  @path
end

Class Method Details

.dump(yaml) ⇒ Object



33
34
35
36
# File 'lib/zombie_fans/actions/persist.rb', line 33

def dump yaml
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, YAML.dump(yaml))
end

.loadObject



28
29
30
31
# File 'lib/zombie_fans/actions/persist.rb', line 28

def load
  return [] unless File.exist? path
  YAML.load(File.read(path)) || []
end

Instance Method Details

#saveObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/zombie_fans/actions/persist.rb', line 9

def save
  record = {}
  ATTRIBUTES.each do |key|
    attribute = instance_variable_get(:"@#{key}")
    record[key] = attribute if attribute
  end

  yaml = Persist.load
  yaml << record
  Persist.dump yaml
end