Class: Simple::Immutable
- Inherits:
-
Object
show all
- Defined in:
- lib/simple/immutable.rb
Defined Under Namespace
Classes: TestCase
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
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/simple/immutable.rb', line 28
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 = 5) ⇒ 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 = 5)
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
|
Instance Method Details
#==(other) ⇒ Object
58
59
60
|
# File 'lib/simple/immutable.rb', line 58
def ==(other)
@hsh == other
end
|
#inspect ⇒ Object
44
45
46
|
# File 'lib/simple/immutable.rb', line 44
def inspect
"<#{self.class.name}: #{@hsh.inspect}>"
end
|
#respond_to?(sym) ⇒ Boolean
54
55
56
|
# File 'lib/simple/immutable.rb', line 54
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
48
49
50
51
52
|
# File 'lib/simple/immutable.rb', line 48
def respond_to_missing?(method_name, include_private = false)
@hsh.key?(method_name.to_sym) ||
@hsh.key?(method_name.to_s) ||
super
end
|