Class: Class::MethodHandler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processor, sprior, group, prior = 0) ⇒ MethodHandler

Returns a new instance of MethodHandler.



36
37
38
39
40
41
# File 'lib/define_method_handler.rb', line 36

def initialize(processor, sprior, group, prior = 0)
  @processor = processor
  @priority = prior
  @second_priority = sprior
  @group = group
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



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

def group
  @group
end

#method_nameObject

Returns the value of attribute method_name.



34
35
36
# File 'lib/define_method_handler.rb', line 34

def method_name
  @method_name
end

#priorityObject (readonly)

Returns the value of attribute priority.



31
32
33
# File 'lib/define_method_handler.rb', line 31

def priority
  @priority
end

#processorObject (readonly)

Returns the value of attribute processor.



30
31
32
# File 'lib/define_method_handler.rb', line 30

def processor
  @processor
end

#second_priorityObject (readonly)

Returns the value of attribute second_priority.



32
33
34
# File 'lib/define_method_handler.rb', line 32

def second_priority
  @second_priority
end

Instance Method Details

#condition(&blk) ⇒ Object



65
66
67
68
# File 'lib/define_method_handler.rb', line 65

def condition(&blk)
  @condition = blk
  self
end

#execute?(chain, *args) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/define_method_handler.rb', line 43

def execute?(chain, *args)
  if @condition
    tmp_method = "tmpmethod#{rand(1000000)}#{Time.now.to_i}"
    begin
      local_condition = @condition
      chain.class.class_eval do 
        define_method(tmp_method, &local_condition)
      end
      chain.send(tmp_method,*args)
    ensure
      begin
        chain.class.class_eval do
          remove_method(tmp_method)
        end
      rescue NameError
      end
    end
  else
    true
  end
end