Class: YAMLator
- Inherits:
-
Object
- Object
- YAMLator
- Defined in:
- lib/yamlator.rb
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
-
#initialize(hash) ⇒ YAMLator
constructor
A new instance of YAMLator.
- #to_yaml ⇒ Object
Constructor Details
#initialize(hash) ⇒ YAMLator
Returns a new instance of YAMLator.
14 15 16 17 |
# File 'lib/yamlator.rb', line 14 def initialize(hash) #@data = File.file?(path_or_string) ? File.read(path_or_string) : path_or_string @hash = hash end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
12 13 14 |
# File 'lib/yamlator.rb', line 12 def hash @hash end |
Instance Method Details
#[]=(key, value) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/yamlator.rb', line 24 def []=(key, value) chain = key.split('.') this_hash = @hash chain.each_with_index do |part, index| is_last = index==chain.length-1 key_this_far = chain[0..index].join('.') case this_hash[part] when Hash raise("trying to add a string to a hash key in use: #{key_this_far.inspect}") if is_last # Uncomment the following two lines if you would like to prevent overwriting existing keys #when String # raise("trying to add to a string key in use: #{key_this_far.inspect}") else this_hash[part] = is_last ? value : {} end this_hash = this_hash[part] end value end |
#to_yaml ⇒ Object
19 20 21 22 |
# File 'lib/yamlator.rb', line 19 def to_yaml #[preamble, yaml].join yaml end |