Module: Predicated::Predicate::JsonStructToPredicate

Defined in:
lib/predicated/from/json.rb

Constant Summary collapse

SIGN_TO_CLASS =
{
  "==" => Equal, 
  ">" => GreaterThan, 
  "<" => LessThan, 
  ">=" => GreaterThanOrEqualTo, 
  "<=" => LessThanOrEqualTo
}

Class Method Summary collapse

Class Method Details

.convert(json_struct) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/predicated/from/json.rb', line 25

def self.convert(json_struct)        
  if json_struct.is_a?(Array)
    left, sign, right = json_struct
    if operation_class=SIGN_TO_CLASS[sign]
      operation_class.new(left, right)
    else
      raise DontKnowWhatToDoWithThisJson.new(json_struct)
    end
  elsif json_struct.is_a?(Hash)
    if left_and_right=json_struct["and"]
      left, right = left_and_right
      And.new(convert(left), convert(right))
    elsif left_and_right=json_struct["or"]
      left, right = left_and_right
      Or.new(convert(left), convert(right))
    elsif inner=json_struct["not"]
      Not.new(convert(inner))
    else
      raise DontKnowWhatToDoWithThisJson.new(json_struct)
    end
  else
    raise DontKnowWhatToDoWithThisJson.new(json_struct)
  end
end