Class: Structish::Hash

Inherits:
Hash
  • Object
show all
Includes:
Validations
Defined in:
lib/structish/hash.rb

Instance Method Summary collapse

Methods included from Validations

included

Methods inherited from Hash

#to_structish

Constructor Details

#initialize(raw_constructor = {}) ⇒ Hash

Returns a new instance of Hash.

Raises:

  • (ArgumentError)


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

#compactObject



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