Class: Philiprehberger::TypedHash::Instance
- Inherits:
-
Object
- Object
- Philiprehberger::TypedHash::Instance
- Defined in:
- lib/philiprehberger/typed_hash/instance.rb
Overview
A typed hash instance with validation and coercion
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Access a value by key.
-
#[]=(key, value) ⇒ Object
Set a value by key (raises if frozen).
-
#diff(other) ⇒ Hash
Compute the diff between this instance and another.
-
#errors ⇒ Array<String>
Return validation errors.
-
#freeze ⇒ self
Freeze the instance, making it immutable.
-
#frozen? ⇒ Boolean
Check if the instance is frozen.
-
#initialize(schema, data) ⇒ Instance
constructor
A new instance of Instance.
-
#key?(key) ⇒ Boolean
Hash-like membership predicate.
-
#merge(other) ⇒ Instance
Merge with another hash or Instance, returning a new Instance.
-
#omit(*keys) ⇒ Instance
Return a new Instance without the specified keys.
-
#pick(*keys) ⇒ Instance
Return a new Instance with only the specified keys.
-
#to_h ⇒ Hash
Convert to a plain hash.
-
#to_json(*_args) ⇒ String
Serialize to JSON string.
-
#valid? ⇒ Boolean
Check if the instance is valid.
Constructor Details
#initialize(schema, data) ⇒ Instance
Returns a new instance of Instance.
9 10 11 12 13 14 15 16 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 9 def initialize(schema, data) @schema = schema @data = {} @errors = [] @frozen = false process(data) end |
Instance Method Details
#[](key) ⇒ Object?
Access a value by key
22 23 24 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 22 def [](key) @data[key] end |
#[]=(key, value) ⇒ Object
Set a value by key (raises if frozen)
30 31 32 33 34 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 30 def []=(key, value) raise Philiprehberger::TypedHash::FrozenError, 'cannot modify a frozen instance' if @frozen @data[key] = value end |
#diff(other) ⇒ Hash
Compute the diff between this instance and another
120 121 122 123 124 125 126 127 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 120 def diff(other) all_keys = (to_h.keys | other.to_h.keys).uniq all_keys.each_with_object({}) do |key, result| old_val = self[key] new_val = other[key] result[key] = { old: old_val, new: new_val } unless old_val == new_val end end |
#errors ⇒ Array<String>
Return validation errors
54 55 56 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 54 def errors @errors.dup end |
#freeze ⇒ self
Freeze the instance, making it immutable
104 105 106 107 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 104 def freeze @frozen = true self end |
#frozen? ⇒ Boolean
Check if the instance is frozen
112 113 114 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 112 def frozen? @frozen end |
#key?(key) ⇒ Boolean
Hash-like membership predicate.
40 41 42 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 40 def key?(key) @data.key?(key.to_sym) end |
#merge(other) ⇒ Instance
Merge with another hash or Instance, returning a new Instance
71 72 73 74 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 71 def merge(other) other_data = other.is_a?(Instance) ? other.to_h : other @schema.new(to_h.merge(other_data)) end |
#omit(*keys) ⇒ Instance
Return a new Instance without the specified keys
89 90 91 92 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 89 def omit(*keys) omitted = to_h.except(*keys) @schema.new(omitted) end |
#pick(*keys) ⇒ Instance
Return a new Instance with only the specified keys
80 81 82 83 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 80 def pick(*keys) picked = to_h.slice(*keys) @schema.new(picked) end |
#to_h ⇒ Hash
Convert to a plain hash
61 62 63 64 65 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 61 def to_h @data.each_with_object({}) do |(k, v), result| result[k] = v.is_a?(Instance) ? v.to_h : v end end |
#to_json(*_args) ⇒ String
Serialize to JSON string
97 98 99 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 97 def to_json(*_args) to_h.to_json end |
#valid? ⇒ Boolean
Check if the instance is valid
47 48 49 |
# File 'lib/philiprehberger/typed_hash/instance.rb', line 47 def valid? @errors.empty? end |