Class: ModelExtensions::Order::NonNilHash

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

Overview

rubocop:enable Metrics/MethodLength

Instance Method Summary collapse

Constructor Details

#initialize(key_style_operation = :symbolize_keys) ⇒ NonNilHash

rubocop:todo Style/Documentation


97
98
99
100
# File 'app/api/model_extensions/order.rb', line 97

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

Instance Method Details

#[](*keys) ⇒ Object


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

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

#[]=(*keys_and_values) ⇒ Object


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

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


102
103
104
105
# File 'app/api/model_extensions/order.rb', line 102

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

#fetch(*keys_and_default) ⇒ Object


118
119
120
121
# File 'app/api/model_extensions/order.rb', line 118

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


123
124
125
# File 'app/api/model_extensions/order.rb', line 123

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