Class: Velocity::DataQueryMap

Inherits:
Object
  • Object
show all
Defined in:
lib/velocity/data_query_map.rb

Instance Method Summary collapse

Constructor Details

#initialize(model_binding, force_adapter = nil) ⇒ DataQueryMap

Returns a new instance of DataQueryMap.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/velocity/data_query_map.rb', line 6

def initialize(model_binding, force_adapter = nil)
  begin
    @handler = "Velocity::#{force_adapter || model_binding.connection.adapter_name}Result".constantize
  rescue NameError
    raise "Velocity does not currently support the #{model_binding.connection.adapter_name} adapter.\n Head on over to http://github.com/pascalh1011/velocity to add an issue, or if you're feeling generous, submit an appropriate handler for your database adapter."
  end

  @model = model_binding
  @results = nil # For accepting last query (from Result)
  @fields = ""
  @conditions = []
  @orders = []
  @joins = []
 
  # Prepare and proxy methods pertaining to results onto the Result object (after performing the query)
  (@handler.instance_methods-@handler.superclass.instance_methods+[:inspect, :to_yaml]).each do |method|
    define_singleton_method(method) do |*arguments, &block|
      prepare_and_run_query
      @results.send(method, *arguments, &block)
    end
  end
end

Instance Method Details

#allObject



54
55
56
57
58
# File 'lib/velocity/data_query_map.rb', line 54

def all
  limit(0)
  prepare_and_run_query
  @results
end

#joins(*list_of_associations) ⇒ Object



44
45
46
47
# File 'lib/velocity/data_query_map.rb', line 44

def joins(*list_of_associations)
  @joins += list_of_associations.flatten unless list_of_associations.empty?
  self
end

#limit(limit = nil) ⇒ Object



34
35
36
37
# File 'lib/velocity/data_query_map.rb', line 34

def limit(limit=nil)
  @limit = limit.to_i
  self
end

#order(field_and_direction = '') ⇒ Object



49
50
51
52
# File 'lib/velocity/data_query_map.rb', line 49

def order(field_and_direction='')
  @orders << field_and_direction unless field_and_direction.blank?
  self
end

#select(fields = '') ⇒ Object



29
30
31
32
# File 'lib/velocity/data_query_map.rb', line 29

def select(fields='')
  @fields = [@fields, fields].join(', ') unless fields.blank?
  self
end

#where(conditions = {}) ⇒ Object



39
40
41
42
# File 'lib/velocity/data_query_map.rb', line 39

def where(conditions={})
  @conditions << @model.send(:sanitize_sql_for_conditions, conditions) unless conditions.empty?
  self
end