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)



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

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
33
34
# 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'
        body = block ? capture(renderer, &block) : nil
        if method(:concat).arity == 2
          concat renderer.display(body), binding
        else
          concat renderer.display(body)
        end
      else
        block.call(renderer) if block
      end
      renderer
    end
  )
end

Instance Method Details

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/block_helpers.rb', line 36

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