Class: LinqrExp

Inherits:
Object show all
Defined in:
lib/linqr_exp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proc_exp) ⇒ LinqrExp

Returns a new instance of LinqrExp.



53
54
55
56
# File 'lib/linqr_exp.rb', line 53

def initialize(proc_exp)
  @exp = Ripper::RubyBuilder.build(proc_exp.to_source)
  @binding = proc_exp.binding
end

Instance Attribute Details

#bindingObject (readonly)

Returns the value of attribute binding.



52
53
54
# File 'lib/linqr_exp.rb', line 52

def binding
  @binding
end

Instance Method Details

#evaluateObject



58
59
60
# File 'lib/linqr_exp.rb', line 58

def evaluate 
  source.linqr_provider.evaluate(self)
end

#fcall(exp, fname) ⇒ Object



68
69
70
# File 'lib/linqr_exp.rb', line 68

def fcall(exp, fname)
  exp.select(Ruby::Call).select {|call|call.identifier &&  call.token == fname}.first
end

#fexp(exp, fname) ⇒ Object



73
74
75
76
# File 'lib/linqr_exp.rb', line 73

def fexp(exp, fname)
  f_arg = fcall(exp,fname).arguments.first
  f_arg # make this less confusing
end

#group_byObject



35
36
37
# File 'lib/linqr_exp.rb', line 35

def group_by
  GroupBy.new(fexp(@exp,"group_by")) 
end

#group_by?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/linqr_exp.rb', line 39

def group_by?
  !fcall(@exp,"group_by").nil?
end

#order_byObject



43
44
45
# File 'lib/linqr_exp.rb', line 43

def order_by
  OrderBy.new(fcall(@exp,"order_by")) 
end

#order_by?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/linqr_exp.rb', line 47

def order_by?
  !fcall(@exp,"order_by").nil?
end

#set_variable(var, val) ⇒ Object



22
23
24
25
# File 'lib/linqr_exp.rb', line 22

def set_variable(var,val)
  @variables ||= {}
  @variables[var] = val
end

#sourceObject



77
78
79
80
81
# File 'lib/linqr_exp.rb', line 77

def source
  token =fcall(@exp,"in_").arguments.first
  source_name = token.evaluate_source_name(SourceNameEvaluator.new)
  @binding.eval(source_name)
end

#variableObject



62
63
64
# File 'lib/linqr_exp.rb', line 62

def variable
  variables.first
end

#variable_val(var_name) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/linqr_exp.rb', line 27

def variable_val(var_name)
  if (@variables && var = @variables[var_name])
    var
  else
   @binding.eval(var_name)
  end
end

#variablesObject



65
66
67
# File 'lib/linqr_exp.rb', line 65

def variables
  fcall(@exp,"from").arguments.collect(&:name)
end

#with_varsObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/linqr_exp.rb', line 11

def with_vars
  @variables ||= {}
  Proc.new do |args|
    args= [args] unless args.is_a? Array
    args.each_with_index do |param, idx|
      @variables[variables[idx].to_s]= param
    end
    yield *args
  end
end