Class: Simple::SQL::Helpers::Immutable
- Inherits:
-
Object
- Object
- Simple::SQL::Helpers::Immutable
show all
- Defined in:
- lib/simple/sql/helpers/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
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/simple/sql/helpers/immutable.rb', line 31
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.
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/simple/sql/helpers/immutable.rb', line 13
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
61
62
63
|
# File 'lib/simple/sql/helpers/immutable.rb', line 61
def ==(other)
@hsh == other
end
|
#inspect ⇒ Object
53
54
55
|
# File 'lib/simple/sql/helpers/immutable.rb', line 53
def inspect
"<Immutable: #{@hsh.inspect}>"
end
|
#respond_to?(sym) ⇒ Boolean
57
58
59
|
# File 'lib/simple/sql/helpers/immutable.rb', line 57
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
47
48
49
50
51
|
# File 'lib/simple/sql/helpers/immutable.rb', line 47
def respond_to_missing?(method_name, include_private = false)
@hsh.key?(method_name.to_sym) ||
@hsh.key?(method_name.to_s) ||
super
end
|