Class: ActiveRecordProvider

Inherits:
Object show all
Defined in:
lib/providers/active_record/activerecord_provider.rb

Instance Method Summary collapse

Constructor Details

#initialize(active_record_class) ⇒ ActiveRecordProvider

Returns a new instance of ActiveRecordProvider.



6
7
8
# File 'lib/providers/active_record/activerecord_provider.rb', line 6

def initialize(active_record_class)
  @active_record_class = active_record_class
end

Instance Method Details

#evaluate(linq_exp) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/providers/active_record/activerecord_provider.rb', line 9

def evaluate(linq_exp)
  query_params = {}
  evaluator = ActiveRecordExpressionEvaluator.new(linq_exp)
  if (linq_exp.where?)
    linq_exp.where.visit(evaluator)
    query_params.merge!(:conditions =>evaluator.conditions)
  end

  group_by_evaluator = ArGroupByExpressionEvaluator.new(linq_exp)
  if(linq_exp.group_by?)
    query_params.merge!(:group =>linq_exp.group_by.visit(group_by_evaluator))
  end
  if(linq_exp.order_by?)
    query_params.merge!(:order =>linq_exp.order_by.visit(evaluator))
  end

  selected_values = @active_record_class.find(:all,query_params)

  if (linq_exp.group_by?)
    grouped_values = selected_values.group_by(&group_by_evaluator.group_by)
    grouped_values.collect do |(k,v)|
      Object.send(:define_method,group_by_evaluator.grouping_var) { Grouped.new(k,v) }
    linq_exp.select.visit(EnumerableExpessionEvaluator.new(linq_exp))
    end
  else 
    selected_values.collect do |e|
      Object.send(:define_method,linq_exp.variable.to_sym) { e }
      linq_exp.select.visit(EnumerableExpessionEvaluator.new(linq_exp))
    end
  end
end