Class: BlockHelpers::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



42
43
44
45
46
47
48
49
# File 'lib/block_helpers.rb', line 42

def method_missing(method, *args, &block)
  if helper.respond_to?(method)
    self.class_eval "def #{method}(*args, &block); helper.send('#{method}', *args, &block); end"
    self.send(method, *args, &block)
  else
    super
  end
end

Class Method Details

.inherited(klass) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/block_helpers.rb', line 7

def self.inherited(klass)
  # Define the helper method
  # e.g. for a class:
  #   class HelloHelper < BlockHelpers::Base
  #     #.....
  #   end
  #
  # then we define a helper method 'hello_helper'
  #
  method_name = klass.name.split('::').last.underscore
  klass.parent.class_eval %(
    def #{method_name}(*args, &block)
      renderer = #{klass.name}.new(*args)
      renderer.send(:helper=, self)
      if renderer.public_methods(false).include? 'display'
        if method(:concat).arity == 1
          concat renderer.display(capture(renderer, &block))
        else
          concat renderer.display(capture(renderer, &block)), binding
        end
      else
        block.call(renderer)
      end
    end
  )
end

Instance Method Details

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(method)
  super or helper.respond_to?(method)
end