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
# 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)
  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



36
37
38
# File 'lib/structish/hash.rb', line 36

def compact
  self.class.new(to_h.compact)
end

#except(*except_keys) ⇒ Object



27
28
29
# File 'lib/structish/hash.rb', line 27

def except(*except_keys)
  self.class.new(to_h.except(*except_keys))
end

#except!(*except_keys) ⇒ Object



31
32
33
34
# File 'lib/structish/hash.rb', line 31

def except!(*except_keys)
  super(*except_keys)
  validate_structish(self)
end

#merge(other) ⇒ Object



18
19
20
# File 'lib/structish/hash.rb', line 18

def merge(other)
  self.class.new(to_h.merge(other))
end

#merge!(other) ⇒ Object



22
23
24
25
# File 'lib/structish/hash.rb', line 22

def merge!(other)
  super(other)
  validate_structish(self)
end