Class: Fire::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/fire/rule.rb

Overview

Rule class encapsulates a rule definition.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logic, options = {}, &procedure) ⇒ Rule

Initialize new instanance of Rule.

Parameters:

  • logic

    Logic condition. [Logic]

  • procedure

    Procedure to run if logic condition is met. [Proc]



14
15
16
17
18
# File 'lib/fire/rule.rb', line 14

def initialize(logic, options={}, &procedure)
  @logic     = logic
  @requisite = options[:todo]
  @procedure = procedure
end

Instance Attribute Details

#logicLogic (readonly)

Access logic condition.

Returns:

  • (Logic)

    Returns



23
24
25
# File 'lib/fire/rule.rb', line 23

def logic
  @logic
end

#procedureProc (readonly)

Returns [Proc]

Returns:

  • (Proc)


26
27
28
# File 'lib/fire/rule.rb', line 26

def procedure
  @procedure
end

Instance Method Details

#apply(&prepare) ⇒ void Also known as: invoke

This method returns an undefined value.

Apply logic, running the rule’s prcedure if the logic condition is satisfied.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fire/rule.rb', line 45

def apply(&prepare)
  case logic
  when true
    call
  when false, nil
  else
    result_set = logic.call
    if result_set && !result_set.empty?
      prepare.call
      call(*result_set)
    end
  end
end

#call(*result_set) ⇒ Object (protected)

Run rule procedure.

Parameters:

  • result_set

    The result set returned by the logic condition.

Returns:

  • (Object)

    Returns whatever the procedure returns.



69
70
71
72
73
74
75
76
# File 'lib/fire/rule.rb', line 69

def call(*result_set)
  if @procedure.arity == 0
    @procedure.call
  else
    #@procedure.call(session, *args)
    @procedure.call(*result_set)
  end
end

#nameObject

Rules don’t generally have names, but task rules do.



37
38
39
# File 'lib/fire/rule.rb', line 37

def name
  nil
end

#requisiteObject Also known as: todo

Names of requisite tasks.



29
30
31
# File 'lib/fire/rule.rb', line 29

def requisite
  @requisite
end