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 Method Summary collapse

Constructor Details

#initialize(hash) ⇒ YamlHash

Returns a new instance of YamlHash.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ 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



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_yaml/yaml_hash.rb', line 19

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

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

    value
  else
    super
  end
end

Instance Method Details

#hashObject

The method is used to obtain a hash from an incomplete call chain. By default, if there is a value from a method of the same name, it will be returned. Otherwise the current hash will be returned



13
14
15
# File 'lib/active_yaml/yaml_hash.rb', line 13

def hash
  @hash['hash'] || @hash
end

#inspectObject



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

def inspect
  @hash
end

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

Returns:

  • (Boolean)


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

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

#to_sObject



39
40
41
# File 'lib/active_yaml/yaml_hash.rb', line 39

def to_s
  @hash.to_s
end