Class: Structish::Hash
- Includes:
- Validations
- Defined in:
- lib/structish/hash.rb
Instance Method Summary collapse
- #compact ⇒ Object
- #except(*except_keys) ⇒ Object
- #except!(*except_keys) ⇒ Object
-
#initialize(raw_constructor = {}) ⇒ Hash
constructor
A new instance of Hash.
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
Methods included from Validations
Methods inherited from Hash
Constructor Details
#initialize(raw_constructor = {}) ⇒ Hash
Returns a new instance of Hash.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/structish/hash.rb', line 6 def initialize(raw_constructor = {}) raise(ArgumentError, "Only hash-like objects can be used as constructors for Structish::Hash") unless raw_constructor.respond_to?(:to_hash) constructor = self.class.symbolize? ? raw_constructor.symbolize_keys : raw_constructor hash = constructor.to_h validate_structish(hash) hash = hash.compact if self.class.compact? super() update(hash) self.default = hash.default if hash.default self.default_proc = hash.default_proc if hash.default_proc end |
Instance Method Details
#compact ⇒ Object
37 38 39 |
# File 'lib/structish/hash.rb', line 37 def compact self.class.new(to_h.compact) end |
#except(*except_keys) ⇒ Object
28 29 30 |
# File 'lib/structish/hash.rb', line 28 def except(*except_keys) self.class.new(to_h.except(*except_keys)) end |
#except!(*except_keys) ⇒ Object
32 33 34 35 |
# File 'lib/structish/hash.rb', line 32 def except!(*except_keys) super(*except_keys) validate_structish(self) end |
#merge(other) ⇒ Object
19 20 21 |
# File 'lib/structish/hash.rb', line 19 def merge(other) self.class.new(to_h.merge(other)) end |
#merge!(other) ⇒ Object
23 24 25 26 |
# File 'lib/structish/hash.rb', line 23 def merge!(other) super(other) validate_structish(self) end |