Class: NativeQuery::Join
- Inherits:
-
Object
- Object
- NativeQuery::Join
- Defined in:
- lib/native-query/join.rb
Overview
Represents join request.
Instance Attribute Summary collapse
-
#joins ⇒ Object
readonly
Returns the value of attribute joins.
Instance Method Summary collapse
-
#backward(*args) ⇒ Object
Indicates backward joining.
-
#build ⇒ Object
Builds ON join string.
-
#direct(*args) ⇒ Object
Indicates direct joining.
-
#fields(*args) ⇒ Object
Sets hash for select from joined table.
-
#indirect(*args) ⇒ Object
Indicates indirect joining.
-
#initialize(original, table) ⇒ Join
constructor
Constructor.
-
#method_missing(sym, *args, &block) ⇒ Object
Calls mapping to joins specification.
-
#where(*args) ⇒ Object
Selects where conditions to load.
-
#wheres ⇒ Object
Return wheres.
Constructor Details
#initialize(original, table) ⇒ Join
Constructor.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/native-query/join.rb', line 72 def initialize(original, table) @table = table @original = original @fields = [ ] @where = [ ] @joins = [ ] @type = :direct @indirect_source = original end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
Calls mapping to joins specification. Call name is name of the target table.
Block works by the same way as query, but for join. But intra-join calls doesn’t work because it returns Query too.
188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/native-query/join.rb', line 188 def method_missing(sym, *args, &block) join = self.class::new(@table, sym) if args and not args.empty? join.fields(*args) end join.instance_eval(&block) @joins << join return self end |
Instance Attribute Details
#joins ⇒ Object (readonly)
Returns the value of attribute joins.
38 39 40 |
# File 'lib/native-query/join.rb', line 38 def joins @joins end |
Instance Method Details
#backward(*args) ⇒ Object
Indicates backward joining.
151 152 153 |
# File 'lib/native-query/join.rb', line 151 def backward(*args) @direct = [:backward, args] end |
#build ⇒ Object
Builds ON join string.
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/native-query/join.rb', line 159 def build result = nil case @type when :indirect result = __indirect when :direct result = __direct end return result end |
#direct(*args) ⇒ Object
Indicates direct joining. (1:M)
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/native-query/join.rb', line 136 def direct(*args) @type = :direct if args.first.array? and (args.first.first == :backward) and (args.first.second.array?) @direct = args.first else @direct = args end return self end |
#fields(*args) ⇒ Object
Sets hash for select from joined table. Without arguments returns fields list.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/native-query/join.rb', line 88 def fields(*args) if args.empty? result = { } @fields.each do |i| if not i.kind_of? Hash i = {i => i} end i.each_pair do |from, to| result[__fix_field(from)] = @table.to_s << "_" << to.to_s end end return result else @fields += args return self end end |
#indirect(*args) ⇒ Object
Indicates indirect joining. (M:N)
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/native-query/join.rb', line 121 def indirect(*args) @type = :indirect if args.first.array? and (args.first.first == :backward) and (args.first.second.array?) @indirect = args.first else @indirect = args end return self end |
#where(*args) ⇒ Object
Selects where conditions to load.
112 113 114 115 |
# File 'lib/native-query/join.rb', line 112 def where(*args) @where << args return self end |
#wheres ⇒ Object
Return wheres.
176 177 178 |
# File 'lib/native-query/join.rb', line 176 def wheres self._fix_where end |