Class: Rools::DefaultParameterProc

Inherits:
Base
  • Object
show all
Defined in:
lib/rools/default_parameter_proc.rb

Overview

The DefaultParameterProc binds to a Rule and is allows the block to use method_missing to refer to the asserted object.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#logger, logger=

Constructor Details

#initialize(rule, b) ⇒ DefaultParameterProc

The “rule” parameter must respond to an :assert method. The “b” parameter is a block that will be rebound to this instance.

Raises:

  • (ArgumentError)


24
25
26
27
28
29
# File 'lib/rools/default_parameter_proc.rb', line 24

def initialize(rule, b)
  raise ArgumentError.new('The "rule" parameter must respond to an :assert method') unless rule.respond_to?(:assert)
  @rule = rule
  @proc = b
  #@working_object = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Parameterless method calls by the attached block are assumed to be references to the working object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rools/default_parameter_proc.rb', line 47

def method_missing(sym, *args)
  # puts "method missing: #{sym} args:#{args.inspect}"
  # check if it is a fact first
  #begin
    facts = @rule.rule_set.get_facts
    if facts.has_key?( sym.to_s )
      #puts "return fact #{facts[sym.to_s].value}" 
      return facts[sym.to_s].value
    else
      raise Exception, "symbol: #{sym} not found in facts"
    end
  #rescue Exception => e
  #  puts "miss exception #{e} #{e.backtrace.join("\n")}"
  #  return nil
  #end
end

Class Method Details

.is_vital(method) ⇒ Object

Determines whether a method is vital to the functionality of the class.



11
12
13
# File 'lib/rools/default_parameter_proc.rb', line 11

def self.is_vital(method)
  return method =~ /__(.+)__|method_missing|instance_eval/
end

Instance Method Details

#assert(obj) ⇒ Object

Assert a new object up the composition-chain into the current RuleSet



41
42
43
# File 'lib/rools/default_parameter_proc.rb', line 41

def assert(obj)
  @rule.assert(obj)
end

#call(obj) ⇒ Object

Call the bound block and set the working object so that it can be referred to by method_missing



33
34
35
36
37
38
# File 'lib/rools/default_parameter_proc.rb', line 33

def call(obj)
  #@working_object = obj
  status = instance_eval(&@proc)
  #@working_object = nil
  return status
end

#fail(message = nil) ⇒ Object

Stops the current assertion and change status to :fail



70
71
72
# File 'lib/rools/default_parameter_proc.rb', line 70

def fail(message = nil)
  @rule.fail(message)
end

#stop(message = nil) ⇒ Object

Stops the current assertion. Does not indicate failure.



65
66
67
# File 'lib/rools/default_parameter_proc.rb', line 65

def stop(message = nil)
  @rule.stop(message)
end