Class: ModelExtensions::Order::NonNilHash

Inherits:
Object
  • Object
show all
Defined in:
app/api/model_extensions/order.rb

Instance Method Summary collapse

Constructor Details

#initialize(key_style_operation = :symbolize_keys) ⇒ NonNilHash

Returns a new instance of NonNilHash


85
86
87
88
# File 'app/api/model_extensions/order.rb', line 85

def initialize(key_style_operation = :symbolize_keys)
  @key_style_operation = key_style_operation
  @store = ActiveSupport::HashWithIndifferentAccess.new
end

Instance Method Details

#[](*keys) ⇒ Object


95
96
97
# File 'app/api/model_extensions/order.rb', line 95

def [](*keys)
  node_and_leaf(*keys) { |node, leaf| node.fetch(leaf, nil) }
end

#[]=(*keys_and_values) ⇒ Object


99
100
101
102
103
104
# File 'app/api/model_extensions/order.rb', line 99

def []=(*keys_and_values)
  value = keys_and_values.pop
  return if value.nil?

  node_and_leaf(*keys_and_values) { |node, leaf| node[leaf] = value }
end

#deep_merge(hash) ⇒ Object


90
91
92
93
# File 'app/api/model_extensions/order.rb', line 90

def deep_merge(hash)
  @store.deep_merge!(hash.try(@key_style_operation) || {})
  self
end

#fetch(*keys_and_default) ⇒ Object


106
107
108
109
# File 'app/api/model_extensions/order.rb', line 106

def fetch(*keys_and_default)
  default = keys_and_default.pop
  node_and_leaf(*keys_and_default) { |node, left| node.fetch(left, default) }
end

#to_hashObject


111
112
113
# File 'app/api/model_extensions/order.rb', line 111

def to_hash
  Hash.new.deep_merge(@store)
end