Class: ModelExtensions::Order::NonNilHash

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

Overview

rubocop:todo Style/Documentation

Instance Method Summary collapse

Constructor Details

#initialize(key_style_operation = :symbolize_keys) ⇒ NonNilHash

Returns a new instance of NonNilHash.


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

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

Instance Method Details

#[](*keys) ⇒ Object


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

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

#[]=(*keys_and_values) ⇒ Object


103
104
105
106
107
108
# File 'app/api/model_extensions/order.rb', line 103

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


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

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

#fetch(*keys_and_default) ⇒ Object


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

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


115
116
117
# File 'app/api/model_extensions/order.rb', line 115

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