Class: Transfigr::Presenter

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

Overview

:api: private

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, options = {}) ⇒ Presenter

Returns a new instance of Presenter.


22
23
24
# File 'lib/transfigr/presenter.rb', line 22

def initialize(target, options = {})   
  @target, @options = target, options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *rest) ⇒ Object

:api: overwritable


49
50
51
52
53
54
55
# File 'lib/transfigr/presenter.rb', line 49

def method_missing(name, *rest)
  if name.to_s =~ /^to_/
    return target.send(name) if target.respond_to?(name)
    raise FormatterNotFound, "No active #{name.inspect} format was found" 
  end
  super
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options


20
21
22
# File 'lib/transfigr/presenter.rb', line 20

def options
  @options
end

#targetObject (readonly)

Returns the value of attribute target


20
21
22
# File 'lib/transfigr/presenter.rb', line 20

def target
  @target
end

Class Method Details

.add_format!(format) ⇒ Object

Adds a formatter to the presenter :api: private


34
35
36
37
38
39
40
# File 'lib/transfigr/presenter.rb', line 34

def self.add_format!(format)
  self.class_eval <<-RUBY
    def to_#{format}
      Transfigr.format!(:#{format}, target, options)
    end
  RUBY
end

.remove_format!(format) ⇒ Object

Removes the formatter method from the presenter :api: private


44
45
46
# File 'lib/transfigr/presenter.rb', line 44

def self.remove_format!(format)
  undef_method :"to_#{format}"
end