Class: ActiveYaml::YamlHash

Inherits:
Object
  • Object
show all
Defined in:
lib/active_yaml/yaml_hash.rb

Overview

Class for creating hashes of similar objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ YamlHash

Returns a new instance of YamlHash.



8
9
10
# File 'lib/active_yaml/yaml_hash.rb', line 8

def initialize(hash)
  @hash = hash || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

The main logic of this class is implemented in this method. Allows you to filter method calls and redirect them to a hash by key



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_yaml/yaml_hash.rb', line 14

def method_missing(method, *args, &)
  value = hash[method.to_s]

  if value
    return self.class.new(value) if value.is_a?(Hash)

    value
  else
    super
  end
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



6
7
8
# File 'lib/active_yaml/yaml_hash.rb', line 6

def hash
  @hash
end

Instance Method Details

#inspectObject



30
31
32
# File 'lib/active_yaml/yaml_hash.rb', line 30

def inspect
  hash
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/active_yaml/yaml_hash.rb', line 26

def respond_to_missing?(method, include_private = false)
  hash.key?(method.to_s) || super
end

#to_sObject



34
35
36
# File 'lib/active_yaml/yaml_hash.rb', line 34

def to_s
  hash.to_s
end