Class: CastedHash
- Inherits:
-
Hash
- Object
- Hash
- CastedHash
- Defined in:
- lib/casted_hash.rb
Constant Summary collapse
- VERSION =
"0.8.0"
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object (also: #store)
- #casted ⇒ Object
- #casted!(*keys) ⇒ Object
- #casted?(key) ⇒ Boolean
- #casted_hash ⇒ Object
- #casting!(*keys) ⇒ Object
- #casting?(key) ⇒ Boolean
- #delete(key) ⇒ Object
- #dup ⇒ Object
- #each ⇒ Object
- #fetch(key, *extras) ⇒ Object
-
#initialize(constructor = {}, cast_proc = nil) ⇒ CastedHash
constructor
A new instance of CastedHash.
- #key?(key) ⇒ Boolean (also: #include?, #has_key?, #member?)
- #merge(hash) ⇒ Object
- #regular_reader ⇒ Object
- #regular_update ⇒ Object
- #regular_writer ⇒ Object
- #to_hash ⇒ Object
- #transform_keys ⇒ Object
- #update(other_hash) ⇒ Object (also: #merge!)
- #values ⇒ Object
- #values_at(*indices) ⇒ Object
Constructor Details
#initialize(constructor = {}, cast_proc = nil) ⇒ CastedHash
Returns a new instance of CastedHash.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/casted_hash.rb', line 4 def initialize(constructor = {}, cast_proc = nil) raise ArgumentError, "`cast_proc` required" unless cast_proc @cast_proc = cast_proc @casting_keys = [] if constructor.is_a?(CastedHash) super() @casted_keys = constructor.instance_variable_get(:@casted_keys).dup regular_update(constructor) elsif constructor.is_a?(Hash) @casted_keys = [] super() update(constructor) else @casted_keys = [] super(constructor) end end |
Instance Method Details
#[](key) ⇒ Object
35 36 37 |
# File 'lib/casted_hash.rb', line 35 def [](key) cast! convert_key(key) end |
#[]=(key, value) ⇒ Object Also known as: store
53 54 55 56 57 |
# File 'lib/casted_hash.rb', line 53 def []=(key, value) key = convert_key(key) uncast! key regular_writer(key, value) end |
#casted ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/casted_hash.rb', line 138 def casted Hash.new.tap do |hash| @casted_keys.each do |key| hash[key] = regular_reader(key) end end end |
#casted!(*keys) ⇒ Object
146 147 148 149 150 |
# File 'lib/casted_hash.rb', line 146 def casted!(*keys) keys.each do |key| @casted_keys << convert_key(key) if key?(key) end end |
#casted?(key) ⇒ Boolean
122 123 124 |
# File 'lib/casted_hash.rb', line 122 def casted?(key) @casted_keys.include?(convert_key(key)) end |
#casted_hash ⇒ Object
117 118 119 120 |
# File 'lib/casted_hash.rb', line 117 def casted_hash cast_all! self end |
#casting!(*keys) ⇒ Object
152 153 154 155 156 |
# File 'lib/casted_hash.rb', line 152 def casting!(*keys) keys.each do |key| @casting_keys << convert_key(key) if key?(key) end end |
#casting?(key) ⇒ Boolean
126 127 128 |
# File 'lib/casted_hash.rb', line 126 def casting?(key) @casting_keys.include?(convert_key(key)) end |
#delete(key) ⇒ Object
101 102 103 104 105 |
# File 'lib/casted_hash.rb', line 101 def delete(key) key = convert_key(key) uncast! key super(key) end |
#dup ⇒ Object
97 98 99 |
# File 'lib/casted_hash.rb', line 97 def dup self.class.new(self, @cast_proc) end |
#each ⇒ Object
112 113 114 115 |
# File 'lib/casted_hash.rb', line 112 def each cast_all! super end |
#fetch(key, *extras) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/casted_hash.rb', line 39 def fetch(key, *extras) key = convert_key(key) value = cast!(key) if value.nil? super(key, *extras) else value end end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
85 86 87 |
# File 'lib/casted_hash.rb', line 85 def key?(key) super(convert_key(key)) end |
#merge(hash) ⇒ Object
61 62 63 |
# File 'lib/casted_hash.rb', line 61 def merge(hash) self.dup.update(hash) end |
#regular_reader ⇒ Object
24 |
# File 'lib/casted_hash.rb', line 24 alias_method :regular_reader, :[] |
#regular_update ⇒ Object
51 |
# File 'lib/casted_hash.rb', line 51 alias_method :regular_update, :update |
#regular_writer ⇒ Object
50 |
# File 'lib/casted_hash.rb', line 50 alias_method :regular_writer, :[]= |
#to_hash ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/casted_hash.rb', line 130 def to_hash Hash.new.tap do |hash| keys.each do |key| hash[key] = regular_reader(key) end end end |
#transform_keys ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/casted_hash.rb', line 26 def transform_keys return enum_for(:transform_keys) unless block_given? result = dup each_key do |key| result[yield(key)] = regular_reader(key) end result end |
#update(other_hash) ⇒ Object Also known as: merge!
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/casted_hash.rb', line 65 def update(other_hash) return self if other_hash.empty? other_hash.each_pair do |key, value| converted_key = convert_key(key) regular_writer converted_key, value if other_hash.is_a?(CastedHash) && other_hash.casted?(key) casted!(key) elsif casted?(key) uncast!(converted_key) end end self end |
#values ⇒ Object
107 108 109 110 |
# File 'lib/casted_hash.rb', line 107 def values cast_all! super end |
#values_at(*indices) ⇒ Object
93 94 95 |
# File 'lib/casted_hash.rb', line 93 def values_at(*indices) indices.collect {|key| self[convert_key(key)]} end |