Class: HashWithDotAccess::Hash

Inherits:
Hash
  • Object
show all
Defined in:
lib/hash_with_dot_access.rb

Instance Method Summary collapse

Constructor Details

#initialize(hsh = nil) ⇒ Hash

Returns a new instance of Hash.



34
35
36
37
38
39
40
41
# File 'lib/hash_with_dot_access.rb', line 34

def initialize(hsh = nil)
  super
  return unless hsh

  update(hsh)
  self.default_proc = hsh.default_proc
  self.default = hsh.default
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hash_with_dot_access.rb', line 76

def method_missing(method_name, *args)
  key = method_name.to_s
  if key.end_with?("=")
    key_chop = key.chop
    self.class.define_method(key) { |value| self[key_chop] = value }
    self[key.chop] = args.first
  elsif self.key?(key)
    self.class.define_method(key) { self[key] }
    self[key]
  elsif default_proc
    super unless args.empty?
    default_proc.call(self, key)
  else
    super unless args.empty?
    default
  end
end

Instance Method Details

#[](key) ⇒ Object



60
# File 'lib/hash_with_dot_access.rb', line 60

def [](key) = super(key.to_s)

#[]=(key, value) ⇒ Object Also known as: store



62
63
64
# File 'lib/hash_with_dot_access.rb', line 62

def []=(key, value)
  _assign(key.to_s, Utils.normalized_value(self, value))
end

#_assignObject

save previous method



58
# File 'lib/hash_with_dot_access.rb', line 58

alias_method :_assign, :[]=

#_to_hObject



153
# File 'lib/hash_with_dot_access.rb', line 153

alias_method :_to_h, :to_h

#assoc(key, *args) ⇒ Object



70
# File 'lib/hash_with_dot_access.rb', line 70

def assoc(key, *args) = super(key.to_s)

#compactObject



149
150
151
# File 'lib/hash_with_dot_access.rb', line 149

def compact
  dup.tap { _1.compact! }
end

#delete(key) ⇒ Object



125
# File 'lib/hash_with_dot_access.rb', line 125

def delete(key) = super(key.to_s)

#dig(*args) ⇒ Object



121
122
123
# File 'lib/hash_with_dot_access.rb', line 121

def dig(*args)
  super(args[0].to_s, *args[1..])
end

#except(*keys) ⇒ Object



127
# File 'lib/hash_with_dot_access.rb', line 127

def except(*keys) = super(*keys.map!(&:to_s))

#fetch(key, *args) ⇒ Object



68
# File 'lib/hash_with_dot_access.rb', line 68

def fetch(key, *args) = super(key.to_s, *args)

#fetch_values(*keys) ⇒ Object



74
# File 'lib/hash_with_dot_access.rb', line 74

def fetch_values(*keys) = super(*keys.map!(&:to_s))

#key?(key) ⇒ Boolean Also known as: has_key?, include?, member?

Returns:

  • (Boolean)


51
# File 'lib/hash_with_dot_access.rb', line 51

def key?(key) = super(key.to_s)

#mergeObject



114
# File 'lib/hash_with_dot_access.rb', line 114

def merge(...) = dup.update(...)

#rejectObject



137
138
139
140
141
# File 'lib/hash_with_dot_access.rb', line 137

def reject(...)
  return to_enum(:reject) unless block_given?

  dup.tap { _1.reject!(...) }
end

#replaceObject



116
117
118
119
# File 'lib/hash_with_dot_access.rb', line 116

def replace(...)
  clear
  update(...)
end

#respond_to_missing?(key, *args) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
# File 'lib/hash_with_dot_access.rb', line 43

def respond_to_missing?(key, *args)
  return false unless args.empty?

  return true if "#{key}".end_with?("=")

  key?(key)
end

#selectObject



131
132
133
134
135
# File 'lib/hash_with_dot_access.rb', line 131

def select(...)
  return to_enum(:select) unless block_given?

  dup.tap { _1.select!(...) }
end

#slice(*keys) ⇒ Object



129
# File 'lib/hash_with_dot_access.rb', line 129

def slice(*keys) = self.class.new(super(*keys.map!(&:to_s)))

#to_dot_hObject



155
156
157
# File 'lib/hash_with_dot_access.rb', line 155

def to_dot_h(...)
  self.class.new _to_h(...)
end

#to_hObject



159
160
161
162
163
164
165
# File 'lib/hash_with_dot_access.rb', line 159

def to_h
  ::Hash.new.update(self).to_h do |k, v|
    value = Utils.primitive_value(v)
    k, value = yield k, value if block_given?
    [k, value]
  end
end

#transform_valuesObject



143
144
145
146
147
# File 'lib/hash_with_dot_access.rb', line 143

def transform_values(...)
  return to_enum(:transform_values) unless block_given?

  dup.tap { _1.transform_values!(...) }
end

#update(*other_hashes) ⇒ Object Also known as: merge!



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hash_with_dot_access.rb', line 94

def update(*other_hashes)
  other_hashes.each do |other_hash|
    if other_hash.is_a? HashWithDotAccess::Hash
      super(other_hash)
    else
      other_hash.to_hash.each do |key, value|
        key = key.to_s
        if block_given? && key?(key)
          value = yield(key, self[key], value)
        end
        _assign(key, Utils.normalized_value(self, value))
      end
    end
  end

  self
end

#values_at(*keys) ⇒ Object



72
# File 'lib/hash_with_dot_access.rb', line 72

def values_at(*keys) = super(*keys.map!(&:to_s))