Class: KSTableExpr

Inherits:
KSExpression show all
Defined in:
lib/kansas/Expression.rb

Instance Method Summary collapse

Methods inherited from KSExpression

binary_function, #count_sql, #delete_sql, operator, #select_sql, unary_function, unary_operator

Constructor Details

#initialize(table, context = nil) ⇒ KSTableExpr

Returns a new instance of KSTableExpr.



208
209
210
211
212
# File 'lib/kansas/Expression.rb', line 208

def initialize(table, context = nil)
  @table = table
  @context = context ? context : KSExpression::Context.new(table)
  @context.tables << table.table_name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/kansas/Expression.rb', line 270

def method_missing(method, *args)
  # _blahblah() indicates that blahblah is a function to be invoked
  # on the database side.  This is a bit of a hack, but I don't have a
  # better solution in my head at the moment.
  if match = /^_(.*)/.match(method.to_s)
    func = match[1]
    KSFuncExpr.new(@table,func,@context,args)
  elsif field = @table.fields[method.to_s]
    KSFieldExpr.new(@table, field, @context)
  elsif @table.relations and relation = @table.relations[method.to_s]
    @context.joins << relation.join
    KSTableExpr.new(relation.foreignTable, @context)
  elsif KSExpression.respond_to?(method.to_s)
    meth = KSExpression.method(method.to_s)
    meth.call(args)
  else
    raise KSBadFieldName,"KSBadFieldName: '#{method}' is not a valid field name"
  end
end

Instance Method Details

#distinct(*exprs) ⇒ Object



236
237
238
239
240
241
# File 'lib/kansas/Expression.rb', line 236

def distinct(*exprs)
  exprs.each do |e|
    @context.distinct[e.field] = true
  end
  KSTrueExpr.new(@context)
end

#expr_bodyObject



290
291
292
# File 'lib/kansas/Expression.rb', line 290

def expr_body
  @table
end

#field(name, *args, &block) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/kansas/Expression.rb', line 243

def field(name, *args, &block)
  if match = /^_(.*)/.match(name.to_s)
    func = match[1]
    KSFuncExpr.new(@table,func,@context,args)
  elsif field = @table.fields[name.to_s]
    KSFieldExpr.new(@table, field, @context)
  elsif @table.relations and relation = @table.relations[name.to_s]
    @context.joins << relation.join
    KSTableExpr.new(relation.foreignTable, @context)
  else
    meth = KSExpression.method(name.to_s)
    meth.call(args, &block)
  end    
end

#limit(*exprs) ⇒ Object



229
230
231
232
233
234
# File 'lib/kansas/Expression.rb', line 229

def limit(*exprs)
  exprs.each do |e|
    @context.limits.push e
  end
  KSTrueExpr.new(@context)
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


258
259
260
261
262
263
264
265
266
267
268
# File 'lib/kansas/Expression.rb', line 258

def respond_to?(method)
  if match = /^_(.*)/.match(method.to_s)
    true
  elsif field = @table.fields[method.to_s]
    true
  elsif @table.relations and relation = @table.relations[method.to_s]
    true
  else
    KSExpression.respond_to?(method.to_s)
  end
end

#sort_by(*exprs) ⇒ Object Also known as: order_by



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/kansas/Expression.rb', line 214

def sort_by(*exprs)
  exprs.each do |sort_field|
    if Hash === sort_field
      sort_field.each_pair do |k,v|
        /desc/i.match(v) ? 'DESC' : 'ASC'
        @context.sort_fields.push [k,v]
      end
    else
      @context.sort_fields.push [sort_field,'ASC']
    end
  end
  KSTrueExpr.new(@context)
end