Class: Predicated::Predicate::SexpToPredicate

Inherits:
Object
  • Object
show all
Defined in:
lib/predicated/lib/predicated/from/ruby_code_string.rb

Constant Summary collapse

SIGN_TO_PREDICATE_CLASS =
{
  :== => Equal,
  :> => GreaterThan,
  :< => LessThan,
  :>= => GreaterThanOrEqualTo,
  :<= => LessThanOrEqualTo,
}

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ SexpToPredicate

Returns a new instance of SexpToPredicate.



24
25
26
# File 'lib/predicated/lib/predicated/from/ruby_code_string.rb', line 24

def initialize(context)
  @context = context
end

Instance Method Details

#convert(sexp) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/predicated/lib/predicated/from/ruby_code_string.rb', line 28

def convert(sexp)
  first_element = sexp.first
  if first_element == :block
    #eval all the top lines and then treat the last one as a predicate
    body_sexps = sexp.sexp_body.to_a
    body_sexps.slice(0..-2).each do |upper_sexp|
      eval_sexp(upper_sexp)
    end
    convert(body_sexps.last)
  elsif first_element == :call
    sym, left_sexp, method_sym, right_sexp = sexp
    left = eval_sexp(left_sexp)
    right = eval_sexp(right_sexp)

    if operation_class=SIGN_TO_PREDICATE_CLASS[method_sym]
      operation_class.new(left, right)
    else
      Call.new(left, method_sym, right)
    end
  elsif first_element == :and
    sym, left, right = sexp
    And.new(convert(left), convert(right))
  elsif first_element == :or
    sym, left, right = sexp
    Or.new(convert(left), convert(right))
  elsif first_element == :not
    sym, inner = sexp
    Not.new(convert(inner))
  else
    raise DontKnowWhatToDoWithThisSexpError.new(sexp)
  end
end

#eval_sexp(sexp) ⇒ Object



61
62
63
# File 'lib/predicated/lib/predicated/from/ruby_code_string.rb', line 61

def eval_sexp(sexp)
  eval(Ruby2Ruby.new.process(sexp), @context)
end