Class: Hyrb::Model

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/hyrb/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, data = nil) ⇒ Model

Returns a new instance of Model.



17
18
19
20
# File 'lib/hyrb/model.rb', line 17

def initialize(path, data = nil)
  @path = File.join(ENV["HYFN_CONFIG_PATH"] || Hyrb::DEFAULT_PATH, path)
  @data = deserialize(load_config_file) || data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



13
14
15
# File 'lib/hyrb/model.rb', line 13

def data
  @data
end

#pathObject

Returns the value of attribute path.



13
14
15
# File 'lib/hyrb/model.rb', line 13

def path
  @path
end

Instance Method Details

#filepathObject



28
29
30
# File 'lib/hyrb/model.rb', line 28

def filepath
  @path + ".yml"
end

#load_config_fileObject



22
23
24
25
26
# File 'lib/hyrb/model.rb', line 22

def load_config_file
  YAML.load_file(filepath)
rescue Errno::ENOENT
  nil
end

#reload!Object



32
33
34
# File 'lib/hyrb/model.rb', line 32

def reload!
  @data = self.load_config_file
end

#save!Object



36
37
38
39
40
41
# File 'lib/hyrb/model.rb', line 36

def save!
  FileUtils.mkdir_p(File.dirname(@path))
  File.open(filepath, File::RDWR|File::TRUNC|File::CREAT, 0600) do |file|
    file.write(serialize(@data).to_yaml)
  end
end