Class: Mao::Filter::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/mao/filter.rb

Overview

A context for the Mao::Query#where DSL, and for Mao::Query#join’s table objects. Any non-lexically bound names hit WhereContext#method_missing, which checks if it belongs to a column, and if so, constructs a Mao::Filter::Column.

Instance Method Summary collapse

Constructor Details

#initialize(query, explicit) ⇒ Table

Constructs a Table; query is the Mao::Query instance, and explicit refers to whether we need to explicitly name tables in the generated SQL. (e.g. when a JOIN is being performed)



154
155
156
157
# File 'lib/mao/filter.rb', line 154

def initialize(query, explicit)
  @query = query
  @explicit = explicit
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Ensure args and block are both empty. Assert that a column for the query this context belongs to by the name name exists, and return a Mao::Filter::Column for that column.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/mao/filter.rb', line 162

def method_missing(name, *args, &block)
  if args.length > 0
    raise ArgumentError, "args not expected in #where subclause"
  end

  if block
    raise ArgumentError, "block not expected in #where subclause"
  end

  unless @query.col_types[name]
    raise ArgumentError, "unknown column for #{@query.table}: #{name}"
  end

  if @explicit
    Column.new(:table => @query.table.to_sym, :name => name).freeze
  else
    Column.new(:name => name).freeze
  end
end