Class: Configoro::Hash

Inherits:
HashWithIndifferentAccess show all
Defined in:
lib/configoro/hash.rb

Instance Method Summary collapse

Methods inherited from HashWithIndifferentAccess

#deep_merge

Constructor Details

#initialize(hsh = {}) ⇒ Hash

Returns a new instance of Hash.



5
6
7
8
9
10
11
12
# File 'lib/configoro/hash.rb', line 5

def initialize(hsh={})
  if hsh.kind_of?(::Hash) then
    super()
    update hsh
  else
    super
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

To optimize access, we create a getter method every time we encounter a key that exists in the hash. If the key is later deleted, the method will be removed.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/configoro/hash.rb', line 73

def method_missing(meth, *args)
  if include?(meth.to_s) then
    if args.empty? then
      create_getter meth
    else
      raise ArgumentError, "wrong number of arguments (#{args.size} for 0)"
    end
  elsif meth.to_s =~ /^(.+)\?$/ and include?(root_meth = $1) then
    if args.empty? then
      !!create_getter(root_meth) #TODO duplication of logic
    else
      raise ArgumentError, "wrong number of arguments (#{args.size} for 0)"
    end
  else
    super
  end
end

Instance Method Details

#<<(hash) ⇒ Configoro::Hash #<<(path) ⇒ Configoro::Hash Also known as: push

Deep-merges additional hash entries into this hash.

Overloads:

  • #<<(hash) ⇒ Configoro::Hash

    Adds the entries from another hash.

    Parameters:

    • hash (::Hash)

      The additional keys to add.

  • #<<(path) ⇒ Configoro::Hash

    Adds the entries from a YAML file. The entries will be added under a sub-hash named after the YAML file’s name.

    Parameters:

    • path (String)

      The path to a YAML file ending in “.yml”.

    Raises:

    • (ArgumentError)

      If the filename does not end in “.yml”.

Returns:



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/configoro/hash.rb', line 26

def <<(hsh_or_path)
  case hsh_or_path
    when String
      raise ArgumentError, "Only files ending in .yml can be added" unless File.extname(hsh_or_path) == '.yml'
      return self unless File.exist?(hsh_or_path)
      data = load_preprocessed_yaml(hsh_or_path)
      deep_merge! File.basename(hsh_or_path, ".yml") => data
    when ::Hash
      deep_merge! hsh_or_path
  end
end

#deep_merge!(other_hash) ⇒ Configoro::Hash

Recursively merges this hash with another hash. All nested hashes are forced to be ‘Configoro::Hash` instances.

Parameters:

  • other_hash (::Hash)

    The hash to merge into this one.

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/configoro/hash.rb', line 51

def deep_merge!(other_hash)
  other_hash.each_pair do |k, v|
    tv      = self[k]
    self[k] = if v.kind_of?(::Hash) then
                if tv.kind_of?(::Hash) then
                  Configoro::Hash.new(tv).deep_merge!(v)
                else
                  Configoro::Hash.new(v)
                end
              else
                v
              end
  end
  self
end

#dupObject



41
42
43
# File 'lib/configoro/hash.rb', line 41

def dup
  Hash.new(self)
end

#inspectObject



92
93
94
# File 'lib/configoro/hash.rb', line 92

def inspect
  "#{to_hash.inspect}:#{self.class.to_s}"
end

#to_symbolized_hashObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/configoro/hash.rb', line 96

def to_symbolized_hash
  inject({}) do |hsh, (key, value)|
    case value
      when Configoro::Hash
        hsh[key.to_sym] = value.to_symbolized_hash
      else
        hsh[key.to_sym] = value
    end
    hsh
  end
end