Class: AngryHash
- Inherits:
-
Hash
- Object
- Hash
- AngryHash
- Extended by:
- Initialiser
- Includes:
- Conversion::ByReference, Conversion::Duplicating, DSL, ExtensionAware, Merging, Parsing
- Defined in:
- lib/angry_hash.rb,
lib/angry_hash/dsl.rb,
lib/angry_hash/merging.rb,
lib/angry_hash/parsing.rb,
lib/angry_hash/extension.rb,
lib/angry_hash/initialiser.rb,
lib/angry_hash/merge_string.rb,
lib/angry_hash/extension_aware.rb,
lib/angry_hash/parsing/dot_notation.rb,
lib/angry_hash/conversion/duplicating.rb,
lib/angry_hash/conversion/by_reference.rb
Defined Under Namespace
Modules: Conversion, DSL, Extension, ExtensionAware, Initialiser, MergeString, Merging, Parsing, Utils
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
fetch value stored under key.
-
#[]=(key, value) ⇒ Object
store value under key, without dup-ing.
-
#__convert_key(key) ⇒ Object
please be Strings.
- #__to_hash(value, keys, cycle_guard = {}) ⇒ Object
- #delete(key) ⇒ Object
-
#dup ⇒ Object
Duplicate the AngryHash.
-
#dup_and_store(key, value) ⇒ Object
store value under key by duping the value.
- #fetch(key, *extras) ⇒ Object
-
#id ⇒ Object
override id to fetch value stored under ‘id’.
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?, #member?)
override normal Hash methods.
-
#method_missing(method, *args, &blk) ⇒ Object
Support dot notation access.
- #regular_reader ⇒ Object
- #regular_writer ⇒ Object
- #to_hash ⇒ Object
-
#to_normal_hash(keys = nil) ⇒ Object
Convert back to a plain hash.
- #values_at(*indices) ⇒ Object
Methods included from Parsing::DotNotation
#__resolve_dotted, #add_dotted, #set_dotted
Methods included from ExtensionAware
Methods included from Conversion::ByReference
#__convert_value_without_dup, #__convert_without_dup
Methods included from Conversion::Duplicating
#__convert, #__convert_value, included
Methods included from Merging
#deep_merge, #deep_merge!, included, #merge, #merge!, #reverse_deep_merge, #reverse_deep_merge!
Methods included from DSL
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blk) ⇒ Object
Support dot notation access
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/angry_hash.rb', line 117 def method_missing(method,*args,&blk) method_s = method.to_s key = method_s[0..-2] case method_s[-1] when ?= return super unless args.size == 1 && !block_given? self[ key ] = args.first when ?? return super unless args.empty? && !block_given? !! self[key] when ?! return super unless args.empty? self[key] = AngryHash.new unless self.key?(key) self[key] else return super unless args.empty? && !block_given? self[method_s] end end |
Class Method Details
.__convert_key(key) ⇒ Object
146 147 148 |
# File 'lib/angry_hash.rb', line 146 def self.__convert_key(key) Symbol === key ? key.to_s : key end |
Instance Method Details
#[](key) ⇒ Object
fetch value stored under key.
32 33 34 |
# File 'lib/angry_hash.rb', line 32 def [](key) regular_reader(__convert_key(key)) end |
#[]=(key, value) ⇒ Object
store value under key, without dup-ing.
27 28 29 |
# File 'lib/angry_hash.rb', line 27 def []=(key, value) regular_writer(__convert_key(key), __convert_value_without_dup(value)) end |
#__convert_key(key) ⇒ Object
please be Strings
143 144 145 |
# File 'lib/angry_hash.rb', line 143 def __convert_key(key) Symbol === key ? key.to_s : key end |
#__to_hash(value, keys, cycle_guard = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/angry_hash.rb', line 83 def __to_hash(value,keys,cycle_guard={}) return cycle_guard[value.hash] if cycle_guard.key?(value.hash) case value when Hash new_hash = cycle_guard[value.hash] = {} if keys == :symbols # TODO DRY value.inject(new_hash) do |hash,(k,v)| hash[k.to_sym] = __to_hash(v,keys,cycle_guard) hash end else value.inject(new_hash) do |hash,(k,v)| hash[k] = __to_hash(v,keys,cycle_guard) hash end end new_hash when Array new_array = cycle_guard[value.hash] = [] value.each {|v| new_array << __to_hash(v,keys,cycle_guard)} new_array else value end end |
#delete(key) ⇒ Object
69 70 71 |
# File 'lib/angry_hash.rb', line 69 def delete(key) super __convert_key(key) end |
#dup ⇒ Object
Duplicate the AngryHash
47 48 49 |
# File 'lib/angry_hash.rb', line 47 def dup self.class[ self ] end |
#dup_and_store(key, value) ⇒ Object
store value under key by duping the value.
42 43 44 |
# File 'lib/angry_hash.rb', line 42 def dup_and_store(key,value) regular_writer(__convert_key(key), __convert_value(value)) end |
#fetch(key, *extras) ⇒ Object
62 63 64 |
# File 'lib/angry_hash.rb', line 62 def fetch(key, *extras) super(__convert_key(key), *extras) end |
#id ⇒ Object
override id to fetch value stored under ‘id’.
37 38 39 |
# File 'lib/angry_hash.rb', line 37 def id regular_reader('id') end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
override normal Hash methods
54 55 56 |
# File 'lib/angry_hash.rb', line 54 def key?(key) super(__convert_key(key)) end |
#regular_reader ⇒ Object
23 |
# File 'lib/angry_hash.rb', line 23 alias_method :regular_reader, :[] |
#regular_writer ⇒ Object
22 |
# File 'lib/angry_hash.rb', line 22 alias_method :regular_writer, :[]= |
#to_hash ⇒ Object
73 74 75 |
# File 'lib/angry_hash.rb', line 73 def to_hash self end |
#to_normal_hash(keys = nil) ⇒ Object
Convert back to a plain hash
79 80 81 |
# File 'lib/angry_hash.rb', line 79 def to_normal_hash(keys=nil) __to_hash(self,keys) end |
#values_at(*indices) ⇒ Object
65 66 67 |
# File 'lib/angry_hash.rb', line 65 def values_at(*indices) indices.collect {|key| self[__convert_key(key)]} end |