Class: CleanHash::Indifferent
- Inherits:
-
Object
- Object
- CleanHash::Indifferent
show all
- Defined in:
- lib/clean-hash/types/indifferent_type.rb
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Indifferent.
3
4
5
|
# File 'lib/clean-hash/types/indifferent_type.rb', line 3
def initialize data
@data = data
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/clean-hash/types/indifferent_type.rb', line 61
def method_missing name, *args, &block
return @data.send(name, *args, &block) if @data.respond_to?(name)
m = name.to_s
if m.end_with?('=')
m = m.sub('=', '')
self[m] = args.first
elsif m.end_with?('?')
!self[m.sub('?', '')].nil?
elsif block
self[m] = block
else
_method_missing_key m
end
end
|
Instance Method Details
#[](key) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/clean-hash/types/indifferent_type.rb', line 44
def [] key
data = @data[key]
data = @data[key.to_s] if data.nil? && !key.is_a?(String)
data = @data[key.to_sym] if data.nil? && key.respond_to?(:to_sym)
if data.is_a?(Hash)
self.class.new data
else
data
end
end
|
#[]=(key, value) ⇒ Object
56
57
58
59
|
# File 'lib/clean-hash/types/indifferent_type.rb', line 56
def []= key, value
delete key
@data[key] = value
end
|
#delete(key) ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'lib/clean-hash/types/indifferent_type.rb', line 27
def delete key
out = []
out.push @data.delete(key)
out.push @data.delete(key.to_s)
out.push @data.delete(key.to_sym) if key.respond_to?(:to_sym)
out.each { |el| return el unless el.nil? }
nil
end
|
#key ⇒ Object
overload key ruby method because it is common hash key name still accessible via to_h.key
19
20
21
|
# File 'lib/clean-hash/types/indifferent_type.rb', line 19
def key
self[:key]
end
|
#key?(name) ⇒ Boolean
23
24
25
|
# File 'lib/clean-hash/types/indifferent_type.rb', line 23
def key? name
@data.key?(name) || @data.key?(name.to_s) || (name.respond_to?(:to_sym) ? @data.key?(name.to_sym) : nil)
end
|
#to_ary ⇒ Object
13
14
15
|
# File 'lib/clean-hash/types/indifferent_type.rb', line 13
def to_ary
[@data]
end
|
#to_h ⇒ Object
36
37
38
|
# File 'lib/clean-hash/types/indifferent_type.rb', line 36
def to_h
@data
end
|
#to_json(*args) ⇒ Object
8
9
10
|
# File 'lib/clean-hash/types/indifferent_type.rb', line 8
def to_json
JSON.pretty_generate @data
end
|