Class: CastedHash
- Inherits:
-
Object
- Object
- CastedHash
- Extended by:
- Forwardable
- Defined in:
- lib/casted_hash.rb,
lib/casted_hash/value.rb,
lib/casted_hash/version.rb
Defined Under Namespace
Classes: Value
Constant Summary collapse
- VERSION =
"0.1.2"
Instance Attribute Summary collapse
-
#cast_proc ⇒ Object
readonly
Returns the value of attribute cast_proc.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object (also: #store)
- #casted?(key) ⇒ Boolean
- #casted_hash ⇒ Object
- #delete(key) ⇒ Object
- #dup ⇒ Object
- #each ⇒ Object (also: #each_pair)
- #fetch(key, *args) ⇒ Object
-
#initialize(constructor = {}, cast_proc = nil) ⇒ CastedHash
constructor
A new instance of CastedHash.
- #inspect ⇒ Object
- #merge(other) ⇒ Object
- #pack_hash(casted_hash) ⇒ Object
- #to_hash ⇒ Object
- #update(other_hash) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(constructor = {}, cast_proc = nil) ⇒ CastedHash
Returns a new instance of CastedHash.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/casted_hash.rb', line 14 def initialize(constructor = {}, cast_proc = nil) raise ArgumentError, "`cast_proc` required" unless cast_proc @cast_proc = cast_proc if constructor.is_a?(Hash) @hash = HashWithIndifferentAccess.new update(constructor) elsif constructor.is_a?(CastedHash) @hash = constructor.pack_hash(self) else raise ArgumentError end end |
Instance Attribute Details
#cast_proc ⇒ Object (readonly)
Returns the value of attribute cast_proc.
9 10 11 |
# File 'lib/casted_hash.rb', line 9 def cast_proc @cast_proc end |
Instance Method Details
#[](key) ⇒ Object
50 51 52 53 |
# File 'lib/casted_hash.rb', line 50 def [](key) val = @hash[key] val.casted_value unless val.nil? end |
#[]=(key, value) ⇒ Object Also known as: store
55 56 57 |
# File 'lib/casted_hash.rb', line 55 def []=(key, value) @hash[key] = pack(value) end |
#casted?(key) ⇒ Boolean
90 91 92 93 |
# File 'lib/casted_hash.rb', line 90 def casted?(key) val = @hash[key] val.casted? if val end |
#casted_hash ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/casted_hash.rb', line 99 def casted_hash cast_all! @hash.inject({}) do |hash, (key, value)| hash.merge(key => value.value) end end |
#delete(key) ⇒ Object
60 61 62 |
# File 'lib/casted_hash.rb', line 60 def delete(key) @hash.delete(key) end |
#dup ⇒ Object
107 108 109 |
# File 'lib/casted_hash.rb', line 107 def dup self.class.new(self, @cast_proc) end |
#each ⇒ Object Also known as: each_pair
29 30 31 32 33 |
# File 'lib/casted_hash.rb', line 29 def each @hash.each do |key, value| yield key, value.casted_value end end |
#fetch(key, *args) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/casted_hash.rb', line 40 def fetch(key, *args) val = @hash[key] if val.nil? @hash.fetch(key, *args) else cast(key, val).value end end |
#inspect ⇒ Object
95 96 97 |
# File 'lib/casted_hash.rb', line 95 def inspect "#<#{self.class.name} hash=#{@hash.inject({}){|hash, (k, v)|hash.merge(k => casted?(k) ? v.value : "<#{v.value}>")}}>" end |
#merge(other) ⇒ Object
86 87 88 |
# File 'lib/casted_hash.rb', line 86 def merge(other) self.dup.update other end |
#pack_hash(casted_hash) ⇒ Object
68 69 70 71 72 |
# File 'lib/casted_hash.rb', line 68 def pack_hash(casted_hash) @hash.inject(HashWithIndifferentAccess.new) do |hash, (key, value)| hash.merge key => pack(value, casted_hash) end end |
#to_hash ⇒ Object
64 65 66 |
# File 'lib/casted_hash.rb', line 64 def to_hash @hash end |
#update(other_hash) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/casted_hash.rb', line 74 def update(other_hash) return self if other_hash.empty? if other_hash.is_a? CastedHash @hash.update other_hash.pack_hash(self) else other_hash.each_pair { |key, value| store(key, value) } end self end |
#values ⇒ Object
36 37 38 |
# File 'lib/casted_hash.rb', line 36 def values @hash.values.map(&:casted_value) end |