Class: Neo4j::Wrapper::Rule::Functions::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j-wrapper/rule/functions/function.rb

Overview

The base class of rule functions.

You are expected to at least implement two methods:

  • update

    update the rule node value of this function

  • function_name

    the name of this function, the name of the generated method - A class method !

Direct Known Subclasses

Size, Sum

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property) ⇒ Function

Initialize the the function with a property which is usually the same as the function identity. See the #calculate? method how this property is used.



17
18
19
# File 'lib/neo4j-wrapper/rule/functions/function.rb', line 17

def initialize(property)
  @property = property.to_s
end

Class Method Details

.rule_node_property(function_name, rule_name, prop) ⇒ Object



68
69
70
# File 'lib/neo4j-wrapper/rule/functions/function.rb', line 68

def self.rule_node_property(function_name, rule_name, prop)
  "_#{function_name}_#{rule_name}_#{prop}"
end

Instance Method Details

#add(rule_name, rule_node, new_value) ⇒ Object

Called when a node is added to a rule group Default is calling update method which is expected to be implemented in a subclass



59
60
61
# File 'lib/neo4j-wrapper/rule/functions/function.rb', line 59

def add(rule_name, rule_node, new_value)
  update(rule_name, rule_node, nil, new_value)
end

#calculate?(changed_property) ⇒ Boolean

Decides if the function should be called are not

Returns:

  • (Boolean)


27
28
29
# File 'lib/neo4j-wrapper/rule/functions/function.rb', line 27

def calculate?(changed_property)
  @property == changed_property
end

#delete(rule_name, rule_node, old_value) ⇒ Object

Called when a node is removed from a rule group Default is calling update method which is expected to be implemented in a subclass



53
54
55
# File 'lib/neo4j-wrapper/rule/functions/function.rb', line 53

def delete(rule_name, rule_node, old_value)
  update(rule_name, rule_node, old_value, nil)
end

#function_idObject

The identity of the function. Used to identify function.

Example

Person.sum(:young, :age)

In the example above the property :age is the used to identify which function will be called since there could be several sum method. In the example we want use the sum method that uses the :age property.



41
42
43
# File 'lib/neo4j-wrapper/rule/functions/function.rb', line 41

def function_id
  @property
end

#rule_node_property(rule_name) ⇒ Object

the name of the property that holds the value of the function



64
65
66
# File 'lib/neo4j-wrapper/rule/functions/function.rb', line 64

def rule_node_property(rule_name)
  self.class.rule_node_property(self.class.function_name, rule_name, @property)
end

#to_sObject



21
22
23
# File 'lib/neo4j-wrapper/rule/functions/function.rb', line 21

def to_s
  "Function #{self.class.function_name} function_id: #{function_id}"
end

#value(rule_node, rule_name) ⇒ Object

The value of the rule



46
47
48
49
# File 'lib/neo4j-wrapper/rule/functions/function.rb', line 46

def value(rule_node, rule_name)
  key = rule_node_property(rule_name)
  rule_node[key] || 0
end