Class: AngryHash
- Inherits:
-
Hash
- Object
- Hash
- AngryHash
- Extended by:
- Initialiser
- Includes:
- Conversion::ByReference, Conversion::Duplicating, DSL, ExtensionAware, Merging
- Defined in:
- lib/angry_hash.rb,
lib/angry_hash/dsl.rb,
lib/angry_hash/merging.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/conversion/duplicating.rb,
lib/angry_hash/conversion/by_reference.rb
Defined Under Namespace
Modules: Conversion, DSL, Extension, ExtensionAware, Initialiser, MergeString, Merging
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 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
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/angry_hash.rb', line 107 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
136 137 138 |
# File 'lib/angry_hash.rb', line 136 def self.__convert_key(key) Symbol === key ? key.to_s : key end |
Instance Method Details
#[](key) ⇒ Object
fetch value stored under key.
26 27 28 |
# File 'lib/angry_hash.rb', line 26 def [](key) regular_reader(__convert_key(key)) end |
#[]=(key, value) ⇒ Object
store value under key, without dup-ing.
21 22 23 |
# File 'lib/angry_hash.rb', line 21 def []=(key, value) regular_writer(__convert_key(key), __convert_value_without_dup(value)) end |
#__convert_key(key) ⇒ Object
please be Strings
133 134 135 |
# File 'lib/angry_hash.rb', line 133 def __convert_key(key) Symbol === key ? key.to_s : key end |
#__to_hash(value, keys, cycle_guard = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/angry_hash.rb', line 77 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 when Array new_array = cycle_guard[value.hash] = [] value.each {|v| new_array << __to_hash(v,keys,cycle_guard)} else value end end |
#delete(key) ⇒ Object
63 64 65 |
# File 'lib/angry_hash.rb', line 63 def delete(key) super __convert_key(key) end |
#dup ⇒ Object
Duplicate the AngryHash
41 42 43 |
# File 'lib/angry_hash.rb', line 41 def dup self.class[ self ] end |
#dup_and_store(key, value) ⇒ Object
store value under key by duping the value.
36 37 38 |
# File 'lib/angry_hash.rb', line 36 def dup_and_store(key,value) regular_writer(__convert_key(key), __convert_value(value)) end |
#fetch(key, *extras) ⇒ Object
56 57 58 |
# File 'lib/angry_hash.rb', line 56 def fetch(key, *extras) super(__convert_key(key), *extras) end |
#id ⇒ Object
override id to fetch value stored under ‘id’.
31 32 33 |
# File 'lib/angry_hash.rb', line 31 def id regular_reader('id') end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
override normal Hash methods
48 49 50 |
# File 'lib/angry_hash.rb', line 48 def key?(key) super(__convert_key(key)) end |
#regular_reader ⇒ Object
17 |
# File 'lib/angry_hash.rb', line 17 alias_method :regular_reader, :[] |
#regular_writer ⇒ Object
16 |
# File 'lib/angry_hash.rb', line 16 alias_method :regular_writer, :[]= |
#to_hash ⇒ Object
67 68 69 |
# File 'lib/angry_hash.rb', line 67 def to_hash self end |
#to_normal_hash(keys = nil) ⇒ Object
Convert back to a plain hash
73 74 75 |
# File 'lib/angry_hash.rb', line 73 def to_normal_hash(keys=nil) __to_hash(self,keys) end |
#values_at(*indices) ⇒ Object
59 60 61 |
# File 'lib/angry_hash.rb', line 59 def values_at(*indices) indices.collect {|key| self[__convert_key(key)]} end |