Class: Simple::Immutable
- Inherits:
-
Object
show all
- Defined in:
- lib/simple/immutable.rb
Constant Summary
collapse
- SELF =
self
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/simple/immutable.rb', line 39
def method_missing(sym, *args, &block)
if args.empty? && !block
begin
value = @hsh.fetch(sym.to_sym) { @hsh.fetch(sym.to_s) }
return SELF.create(value)
rescue KeyError
nil
end
end
super
end
|
Class Method Details
.create(object, max_depth = 8) ⇒ Object
turns an object, which can be a hash or array of hashes, arrays, and scalars into an object which you can use to access with dot methods.
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/simple/immutable.rb', line 9
def self.create(object, max_depth = 8)
case object
when Array
raise ArgumentError, "Object nested too deep (or inner loop?)" if max_depth < 0
object.map { |obj| create obj, max_depth - 1 }
when Hash
new(object)
else
object
end
end
|
.raw_data(immutable) ⇒ Object
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/simple/immutable.rb', line 22
def self.raw_data(immutable)
case immutable
when SELF
immutable.instance_variable_get :@hsh
when Array
immutable.map { |e| raw_data(e) }
else
immutable
end
end
|
Instance Method Details
#==(other) ⇒ Object
69
70
71
|
# File 'lib/simple/immutable.rb', line 69
def ==(other)
@hsh == other
end
|
#inspect ⇒ Object
55
56
57
|
# File 'lib/simple/immutable.rb', line 55
def inspect
"<#{self.class.name}: #{@hsh.inspect}>"
end
|
#respond_to?(sym) ⇒ Boolean
65
66
67
|
# File 'lib/simple/immutable.rb', line 65
def respond_to?(sym)
super || @hsh.key?(sym.to_s) || @hsh.key?(sym.to_sym)
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
59
60
61
62
63
|
# File 'lib/simple/immutable.rb', line 59
def respond_to_missing?(method_name, include_private = false)
@hsh.key?(method_name.to_sym) ||
@hsh.key?(method_name.to_s) ||
super
end
|