Class: Presenter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = nil) ⇒ Presenter

Returns a new instance of Presenter.



4
5
6
7
8
9
10
11
12
13
# File 'lib/presenter.rb', line 4

def initialize(context = nil)
  if context
    @context = context 
    class << self
      Object.instance_methods.each do |method|
        undef_method(method) unless method =~ /^__/ || method == 'method_missing'
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



15
16
17
18
# File 'lib/presenter.rb', line 15

def method_missing(method, *args, &block)
  method = method.to_s.gsub!(/^original_/, '').to_sym if method.to_s =~ /^original_/
  @context.__send__(method, *args, &block)
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



3
4
5
# File 'lib/presenter.rb', line 3

def context
  @context
end

Class Method Details

.presents_like(*presenters) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/presenter.rb', line 20

def self.presents_like(*presenters)
  presenters.each do |other_presenter|
    other_presenter.instance_methods.each do |method|
      class_eval %[
        def #{method}(*args, &block)
          (@#{other_presenter.to_s.downcase} ||= #{other_presenter.to_s}.new(context)).__send__(:#{method}, *args, &block) 
        end
      ] unless method =~ /^__/ || [:context, :context=, :method_missing].include?(method.to_sym)
    end
  end
end