Class: Rools::DefaultParameterProc

Inherits:
Object
  • 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

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)


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

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



46
47
48
# File 'lib/rools/default_parameter_proc.rb', line 46

def method_missing(sym, *args)
  return @working_object if @working_object && args.size == 0
end

Class Method Details

.is_vital(method) ⇒ Object

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



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

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



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

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



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

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