Class: EnumerableProvider

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

Direct Known Subclasses

HashProvider

Instance Method Summary collapse

Constructor Details

#initialize(enumerable) ⇒ EnumerableProvider

Returns a new instance of EnumerableProvider.



5
6
7
# File 'lib/providers/enumerable_provider.rb', line 5

def initialize(enumerable)
  @enumerable = enumerable.to_enum
end

Instance Method Details

#evaluate(linq_exp) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/providers/enumerable_provider.rb', line 42

def evaluate (linq_exp)
  filtered_values =linq_exp.where?? handle_where(linq_exp) : @enumerable
  filtered_values = linq_exp.order_by?? handle_order_by(linq_exp,filtered_values) : filtered_values
  if (linq_exp.group_by?)
    handle_group_by(linq_exp,filtered_values)
  else
    handle_select(linq_exp,filtered_values)
  end
end

#handle_group_by(linq_exp, filtered_values) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/providers/enumerable_provider.rb', line 25

def handle_group_by(linq_exp,filtered_values)
  grouped_values = filtered_values.group_by(&linq_exp.with_vars do |e|
    linq_exp.group_by.visit(EnumerableExpessionEvaluator.new(linq_exp))
  end)

  grouped_values.collect do |(k,v)|
    linq_exp.set_variable(linq_exp.group_by.grouping_var.to_s, Grouped.new(k,v) )
    linq_exp.select.visit(EnumerableExpessionEvaluator.new(linq_exp))
  end
end

#handle_order_by(linq_exp, filtered_values) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/providers/enumerable_provider.rb', line 15

def handle_order_by(linq_exp,filtered_values)
  order_by = linq_exp.order_by
  order_by.expressions.reduce(filtered_values) do |values, sort_exp|
    values.lazy_sort_by(&linq_exp.with_vars do|e| 
      sort_val = sort_exp.visit(EnumerableExpessionEvaluator.new(linq_exp))
      order_by.descending?? 1 - sort_val : sort_val
    end)
  end
end

#handle_select(linq_exp, filtered_values) ⇒ Object



36
37
38
39
40
# File 'lib/providers/enumerable_provider.rb', line 36

def handle_select(linq_exp,filtered_values)
  filtered_values.lazy_map(&linq_exp.with_vars do |e|
    linq_exp.select.visit(EnumerableExpessionEvaluator.new(linq_exp))
  end)
end

#handle_where(linq_exp) ⇒ Object



8
9
10
11
12
13
# File 'lib/providers/enumerable_provider.rb', line 8

def handle_where(linq_exp)
  evaluator = EnumerableExpessionEvaluator.new(linq_exp)
    @enumerable.lazy_select(&linq_exp.with_vars do|e|
      linq_exp.where.visit(evaluator)
    end)
end