Class: Squeel::Nodes::KeyPath

Inherits:
Object
  • Object
show all
Includes:
Operators, PredicateOperators
Defined in:
lib/squeel/nodes/key_path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, endpoint, absolute = false) ⇒ KeyPath

Returns a new instance of KeyPath.



12
13
14
15
16
17
# File 'lib/squeel/nodes/key_path.rb', line 12

def initialize(path, endpoint, absolute = false)
  @path, @endpoint = path, endpoint
  @path = [@path] unless Array === @path
  @endpoint = Stub.new(@endpoint) if Symbol === @endpoint
  @absolute = absolute
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/squeel/nodes/key_path.rb', line 99

def method_missing(method_id, *args)
  super if method_id == :to_ary
  if endpoint.respond_to? method_id
    @endpoint = @endpoint.send(method_id, *args)
    self
  elsif Stub === endpoint || Join === endpoint
    @path << endpoint
    if args.empty?
      @endpoint = Stub.new(method_id)
    elsif (args.size == 1) && (Class === args[0])
      @endpoint = Join.new(method_id, Arel::InnerJoin, args[0])
    else
      @endpoint = Nodes::Function.new method_id, args
    end
    self
  else
    super
  end
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



10
11
12
# File 'lib/squeel/nodes/key_path.rb', line 10

def endpoint
  @endpoint
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/squeel/nodes/key_path.rb', line 10

def path
  @path
end

Instance Method Details

#%(val) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/squeel/nodes/key_path.rb', line 80

def %(val)
  case endpoint
  when Stub, Function
    eq(val)
    self
  else
    endpoint % val
    self
  end
end

#&(other) ⇒ Object



34
35
36
# File 'lib/squeel/nodes/key_path.rb', line 34

def &(other)
  endpoint.respond_to?(:&) ? super : no_method_error(:&)
end

#*(other) ⇒ Object



50
51
52
# File 'lib/squeel/nodes/key_path.rb', line 50

def *(other)
  endpoint.respond_to?(:*) ? super : no_method_error(:*)
end

#+(other) ⇒ Object



42
43
44
# File 'lib/squeel/nodes/key_path.rb', line 42

def +(other)
  endpoint.respond_to?(:+) ? super : no_method_error(:+)
end

#-(other) ⇒ Object



46
47
48
# File 'lib/squeel/nodes/key_path.rb', line 46

def -(other)
  endpoint.respond_to?(:-) ? super : no_method_error(:-)
end

#-@Object



38
39
40
# File 'lib/squeel/nodes/key_path.rb', line 38

def -@
  endpoint.respond_to?(:-@) ? super : no_method_error(:-@)
end

#/(other) ⇒ Object



54
55
56
# File 'lib/squeel/nodes/key_path.rb', line 54

def /(other)
  endpoint.respond_to?(:/) ? super : no_method_error(:/)
end

#absolute?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/squeel/nodes/key_path.rb', line 19

def absolute?
  @absolute
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/squeel/nodes/key_path.rb', line 23

def eql?(other)
  self.class == other.class &&
  self.path == other.path &&
  self.endpoint.eql?(other.endpoint) &&
  self.absolute? == other.absolute?
end

#hashObject



72
73
74
# File 'lib/squeel/nodes/key_path.rb', line 72

def hash
  [self.class, endpoint, *path].hash
end

#op(operator, other) ⇒ Object



58
59
60
# File 'lib/squeel/nodes/key_path.rb', line 58

def op(operator, other)
  endpoint.respond_to?(:op) ? super : no_method_error(:/)
end

#path_with_endpointObject



91
92
93
# File 'lib/squeel/nodes/key_path.rb', line 91

def path_with_endpoint
  path + [endpoint]
end

#to_sObject



95
96
97
# File 'lib/squeel/nodes/key_path.rb', line 95

def to_s
  path.map(&:to_s).join('.') << ".#{endpoint}"
end

#to_symObject



76
77
78
# File 'lib/squeel/nodes/key_path.rb', line 76

def to_sym
  nil
end

#|(other) ⇒ Object



30
31
32
# File 'lib/squeel/nodes/key_path.rb', line 30

def |(other)
  endpoint.respond_to?(:|) ? super : no_method_error(:|)
end

#~Object



62
63
64
65
# File 'lib/squeel/nodes/key_path.rb', line 62

def ~
  @absolute = true
  self
end