Class: Predicated::Operation

Inherits:
Binary show all
Includes:
JsonStructToJsonStr
Defined in:
lib/predicated/lib/predicated/to/xml.rb,
lib/predicated/lib/predicated/to/arel.rb,
lib/predicated/lib/predicated/to/json.rb,
lib/predicated/lib/predicated/evaluate.rb,
lib/predicated/lib/predicated/predicate.rb,
lib/predicated/lib/predicated/to/sentence.rb,
lib/predicated/lib/predicated/simple_templated_predicate.rb,
lib/predicated/lib/predicated/simple_templated_predicate.rb

Instance Attribute Summary collapse

Attributes inherited from Binary

#left, #right

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JsonStructToJsonStr

#to_json_str

Methods included from Binary::FlipThroughMe

#each

Methods included from PrintSupport

#inspect

Methods inherited from Predicate

from_callable_object, from_json_str, from_json_struct, from_ruby_code_string, from_url_part, from_xml

Constructor Details

#initialize(left, method_sym, right) ⇒ Operation

Returns a new instance of Operation.



9
10
11
12
# File 'lib/predicated/lib/predicated/evaluate.rb', line 9

def initialize(left, method_sym, right)
  super(left, right)
  @method_sym = method_sym
end

Instance Attribute Details

#method_symObject (readonly)

Returns the value of attribute method_sym.



7
8
9
# File 'lib/predicated/lib/predicated/evaluate.rb', line 7

def method_sym
  @method_sym
end

Class Method Details

.register_verb_phrase(method_sym, positive_verb_phrase, negative_verb_phrase, accepts_object = true) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/predicated/lib/predicated/to/sentence.rb', line 31

def self.register_verb_phrase(method_sym, 
                              positive_verb_phrase, 
                              negative_verb_phrase, 
                              accepts_object=true)      
  @@method_sym_to_phrase_info[method_sym] = {
    :positive => positive_verb_phrase,
    :negative => negative_verb_phrase,
    :accepts_object => accepts_object
  }
end

.reset_verb_phrasesObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/predicated/lib/predicated/to/sentence.rb', line 42

def self.reset_verb_phrases
  @@method_sym_to_phrase_info = {}
  
  register_verb_phrase(:==, "is equal to", "is not equal to")
  register_verb_phrase(:>, "is greater than", "is not greater than")
  register_verb_phrase(:<, "is less than", "is not less than")
  register_verb_phrase(:>=, "is greater than or equal to", "is not greater than or equal to")
  register_verb_phrase(:<=, "is less than or equal to", "is not less than or equal to")

  register_verb_phrase(:include?, "includes", "does not include")
  register_verb_phrase(:is_a?, "is a", "is not a")
  register_verb_phrase(:nil?, "is nil", "is not nil", accepts_object=false)
  
  nil
end

Instance Method Details

#==(other) ⇒ Object



19
20
21
# File 'lib/predicated/lib/predicated/evaluate.rb', line 19

def ==(other)
  super && method_sym==other.method_sym
end

#evaluateObject



14
15
16
17
# File 'lib/predicated/lib/predicated/evaluate.rb', line 14

def evaluate
  right_values = right.nil? ? [nil] : right #1.9 problem where nils and varargs don't play nicely
  left.send(@method_sym, *right_values)
end

#fill_in(placeholder_replacement) ⇒ Object



33
34
35
# File 'lib/predicated/lib/predicated/simple_templated_predicate.rb', line 33

def fill_in(placeholder_replacement)
  self.class.new(placeholder_replacement, right)
end

#to_arel(arel_table) ⇒ Object



29
30
31
# File 'lib/predicated/lib/predicated/to/arel.rb', line 29

def to_arel(arel_table)
  arel_class.new(arel_table.attributes[left], right)
end

#to_json_structObject



36
37
38
# File 'lib/predicated/lib/predicated/to/json.rb', line 36

def to_json_struct
  [left, json_sign, right]
end

#to_negative_sentenceObject



65
66
67
# File 'lib/predicated/lib/predicated/to/sentence.rb', line 65

def to_negative_sentence
  sentence(verb_phrase[:negative])
end

#to_sObject



59
60
61
62
63
64
65
# File 'lib/predicated/lib/predicated/simple_templated_predicate.rb', line 59

def to_s
  if left == Placeholder
    "#{self.class.shorthand}(#{part_to_s(right)})"
  else
    super
  end
end

#to_sentenceObject



61
62
63
# File 'lib/predicated/lib/predicated/to/sentence.rb', line 61

def to_sentence
  sentence(verb_phrase[:positive])
end

#to_xml(indent = "") ⇒ Object



34
35
36
# File 'lib/predicated/lib/predicated/to/xml.rb', line 34

def to_xml(indent="")
  "#{indent}<#{tag_name}><left>#{escape(left)}</left><right>#{escape(right)}</right></#{tag_name}>"
end