Class: NonConfig::ConfigFile

Inherits:
NonHash
  • Object
show all
Defined in:
lib/non_config/config_file.rb

Overview

Hash with filepath and #save method

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from NonHash

#[]=, convert_array_subhashes, from_hash, #method_missing, #to_hash, #to_yaml

Constructor Details

#initialize(path = nil) ⇒ ConfigFile

parses yaml from path



13
14
15
16
17
18
19
20
21
22
# File 'lib/non_config/config_file.rb', line 13

def initialize(path = nil)
  super()
  return unless path
  fail FileNotFound unless File.exist? path

  @path = path
  file_data = File.read @path
  records = YAML.load file_data
  merge! self.class.from_hash(records)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NonConfig::NonHash

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/non_config/config_file.rb', line 10

def path
  @path
end

Instance Method Details

#saveObject

rewrite file so you can save your changes



25
26
27
# File 'lib/non_config/config_file.rb', line 25

def save
  File.open(@path, 'w') { |f| f.write to_yaml }
end