Class: Simple::SQL::Helpers::Immutable

Inherits:
Object
  • Object
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 (private)



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/simple/sql/helpers/immutable.rb', line 32

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
      # STDERR.puts "Missing attribute #{sym} for Immutable w/#{@hsh.inspect}"
      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
24
# 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



62
63
64
# File 'lib/simple/sql/helpers/immutable.rb', line 62

def ==(other)
  @hsh == other
end

#inspectObject



54
55
56
# File 'lib/simple/sql/helpers/immutable.rb', line 54

def inspect
  "<Immutable: #{@hsh.inspect}>"
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/simple/sql/helpers/immutable.rb', line 58

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

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/simple/sql/helpers/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