Class: Configoro::Hash
- Inherits:
-
HashWithIndifferentAccess
- Object
- Hash
- HashWithIndifferentAccess
- Configoro::Hash
- Defined in:
- lib/configoro/hash.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(hsh_or_path) ⇒ Configoro::Hash
(also: #push)
Deep-merges additional hash entries into this hash.
-
#deep_merge!(other_hash) ⇒ Configoro::Hash
Recursively merges this hash with another hash.
- #dup ⇒ Object
-
#initialize(hsh = {}) ⇒ Hash
constructor
A new instance of Hash.
- #inspect ⇒ Object
-
#method_missing(meth, *args) ⇒ Object
To optimize access, we create a getter method every time we encounter a key that exists in the hash.
- #respond_to_missing?(meth, *args) ⇒ Boolean
- #to_symbolized_hash ⇒ Object
Methods inherited from HashWithIndifferentAccess
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) 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.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/configoro/hash.rb', line 74 def method_missing(meth, *args) if include?(meth.to_s) if args.empty? create_getter meth else raise ArgumentError, "wrong number of arguments (#{args.size} for 0)" end elsif meth.to_s =~/^(.+)\?$/ && include?((root_meth = Regexp.last_match(1))) if args.empty? !!create_getter(root_meth) #TODO duplication of logic else raise ArgumentError, "wrong number of arguments (#{args.size} for 0)" end else super end end |
Class Method Details
Instance Method Details
#<<(hash) ⇒ Configoro::Hash #<<(path) ⇒ Configoro::Hash Also known as: push
Deep-merges additional hash entries into this hash.
26 27 28 29 30 31 32 33 34 35 36 37 |
# 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.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/configoro/hash.rb', line 52 def deep_merge!(other_hash) other_hash.each_pair do |k, v| tv = self[k] self[k] = if v.kind_of?(::Hash) if tv.kind_of?(::Hash) Configoro::Hash.new(tv).deep_merge!(v) else Configoro::Hash.new(v) end else v end end self end |
#inspect ⇒ Object
100 101 102 |
# File 'lib/configoro/hash.rb', line 100 def inspect "#{to_hash.inspect}:#{self.class}" end |
#respond_to_missing?(meth, *args) ⇒ Boolean
93 94 95 96 97 |
# File 'lib/configoro/hash.rb', line 93 def respond_to_missing?(meth, *args) include?(meth.to_s) || (meth.to_s =~ /^(.+)\?$/ && include?(Regexp.last_match(1))) || super(meth, *args) end |