Class: Hyrb::Model
- Inherits:
-
Object
- Object
- Hyrb::Model
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/hyrb/model.rb
Direct Known Subclasses
Hyrb::Models::AnsibleHost, Hyrb::Models::Cache::Developers, Hyrb::Models::Struct
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #filepath ⇒ Object
-
#initialize(path, data = nil) ⇒ Model
constructor
A new instance of Model.
- #load_config_file ⇒ Object
- #reload! ⇒ Object
- #save! ⇒ Object
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
#data ⇒ Object
Returns the value of attribute data.
13 14 15 |
# File 'lib/hyrb/model.rb', line 13 def data @data end |
#path ⇒ Object
Returns the value of attribute path.
13 14 15 |
# File 'lib/hyrb/model.rb', line 13 def path @path end |
Instance Method Details
#filepath ⇒ Object
28 29 30 |
# File 'lib/hyrb/model.rb', line 28 def filepath @path + ".yml" end |
#load_config_file ⇒ Object
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 |