Class: Ambition::Processors::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ambition/processors/base.rb

Direct Known Subclasses

Select, Slice, Sort

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.translator(context, name = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ambition/processors/base.rb', line 97

def self.translator(context, name = nil)
  # Grok the adapter name
  name   ||= self.name.split('::').last

  # Get the module for it
  klass    = context.owner.ambition_adapter.const_get(name)
  instance = klass.new

  # Make sure that the instance has everything it will need:
  #
  # * context
  # * owner
  # * clauses
  # * stash
  # * negated?
  unless instance.respond_to? :context=
    klass.class_eval do
      attr_accessor :context, :negated
      def owner;    @context.owner   end
      def clauses;  @context.clauses end
      def stash;    @context.stash   end
      def negated?; @negated         end
    end
  end

  instance.context = context
  instance
end

Instance Method Details

#keyObject



83
84
85
# File 'lib/ambition/processors/base.rb', line 83

def key
  self.class.name.split('::').last.downcase.intern
end

#process(node) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ambition/processors/base.rb', line 63

def process(node)
  node ||= []

  if node.is_a? Symbol
    node
  elsif respond_to?(method = "process_#{node.first}") 
    send(method, node[1..-1]) 
  elsif node.blank?
    ''
  else
    raise "Missing process method for sexp: #{node.inspect}"
  end
end

#process_array(exp) ⇒ Object



17
18
19
20
# File 'lib/ambition/processors/base.rb', line 17

def process_array(exp)
  # Branch on whether this is straight Ruby or a real array
  rubify(exp) || exp.map { |m| process(m) }
end

#process_dasgn_curr(exp) ⇒ Object Also known as: process_dasgn



12
13
14
# File 'lib/ambition/processors/base.rb', line 12

def process_dasgn_curr(exp)
  (@receiver = exp.first).to_s
end

#process_dvar(exp) ⇒ Object



42
43
44
45
# File 'lib/ambition/processors/base.rb', line 42

def process_dvar(exp)
  target = exp.shift
  value(target.to_s[0..-1])
end

#process_false(exp) ⇒ Object



38
39
40
# File 'lib/ambition/processors/base.rb', line 38

def process_false(exp)
  false
end

#process_gvar(exp) ⇒ Object



59
60
61
# File 'lib/ambition/processors/base.rb', line 59

def process_gvar(exp)
  value(exp.shift.to_s)
end

#process_ivar(exp) ⇒ Object



47
48
49
# File 'lib/ambition/processors/base.rb', line 47

def process_ivar(exp)
  value(exp.shift.to_s[0..-1])
end

#process_lit(exp) ⇒ Object



26
27
28
# File 'lib/ambition/processors/base.rb', line 26

def process_lit(exp)
  exp.first
end

#process_lvar(exp) ⇒ Object



51
52
53
# File 'lib/ambition/processors/base.rb', line 51

def process_lvar(exp)
  value(exp.shift.to_s)
end

#process_nil(exp) ⇒ Object



30
31
32
# File 'lib/ambition/processors/base.rb', line 30

def process_nil(exp)
  nil
end

#process_proc(exp) ⇒ Object

Processing methods



6
7
8
9
10
# File 'lib/ambition/processors/base.rb', line 6

def process_proc(exp)
  # puts "=> #{exp.inspect}"
  receiver, body = process(exp.shift), exp.shift
  process(body)
end

#process_str(exp) ⇒ Object



22
23
24
# File 'lib/ambition/processors/base.rb', line 22

def process_str(exp)
  exp.first
end

#process_true(exp) ⇒ Object



34
35
36
# File 'lib/ambition/processors/base.rb', line 34

def process_true(exp)
  true
end

#process_vcall(exp) ⇒ Object



55
56
57
# File 'lib/ambition/processors/base.rb', line 55

def process_vcall(exp)
  value(exp.shift.to_s)
end

#rubify(exp) ⇒ Object



126
127
128
129
130
131
# File 'lib/ambition/processors/base.rb', line 126

def rubify(exp)
  # TODO: encapsulate this check in Ruby.should_process?(exp)
  if exp.first.first == :call && exp.first[1].last != @receiver && Array(exp.first[1][1]).last != @receiver
    value Ruby.process(exp.first)
  end
end

#to_sObject

Helper methods



79
80
81
# File 'lib/ambition/processors/base.rb', line 79

def to_s
  process SexpTranslator.translate(@block)
end

#translatorObject

Gives you the current translator. Uses self.translator to look it up, if it isn’t known yet.



93
94
95
# File 'lib/ambition/processors/base.rb', line 93

def translator
  @translator ||= self.class.translator(@context)
end

#value(variable) ⇒ Object



87
88
89
# File 'lib/ambition/processors/base.rb', line 87

def value(variable)
  eval variable, @block
end