Class: DelegatedPresenter::Base Abstract

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/delegated_presenter/base.rb

Overview

This class is abstract.

Inherit from it to create a presenter.

Raises:

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Base

Initializes the presenter with an object.

Parameters:

  • object

    Can be an collection or instance of a presentable models.

Raises:



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/delegated_presenter/base.rb', line 84

def initialize(object)
  if object.is_a?(Array)
    map_array(object)
  else
    raise DelegatedPresenter::Error::NotPresentable, "#{self.presenter_class} cannot present a #{object.class}" unless object_is_presentable?(object)
    __setobj__(object)
    if exposed_methods.present?
      expose_methods
    elsif hidden_methods.present?
      hide_methods
    end
  end
end

Class Attribute Details

.exposed_methods=(value) ⇒ Object

Sets the attribute exposed_methods

Parameters:

  • value

    the value to set the attribute exposed_methods to.



14
15
16
# File 'lib/delegated_presenter/base.rb', line 14

def exposed_methods=(value)
  @exposed_methods = value
end

.hidden_methods=(value) ⇒ Object

Sets the attribute hidden_methods

Parameters:

  • value

    the value to set the attribute hidden_methods to.



14
15
16
# File 'lib/delegated_presenter/base.rb', line 14

def hidden_methods=(value)
  @hidden_methods = value
end

.presentable=(value) ⇒ Object

Sets the attribute presentable

Parameters:

  • value

    the value to set the attribute presentable to.



14
15
16
# File 'lib/delegated_presenter/base.rb', line 14

def presentable=(value)
  @presentable = value
end

Class Method Details

.expose(*exposed_methods) ⇒ Object

Exposes methods to the presenter, when defined all others will be hidden.

Examples:

Expose methods:

expose :first_name, :last_name

Parameters:

  • :methods

    one ore more methods from the model to expose from the presenter.



61
62
63
# File 'lib/delegated_presenter/base.rb', line 61

def expose(*exposed_methods)
  self.exposed_methods += exposed_methods.flatten
end

.hide(*hidden_methods) ⇒ Object

Hide methods from the presenter.

Examples:

Hide methods:

hide :id, :crypted_password, :password_salt

Parameters:

  • methods

    one ore more methods from the model to hide from the presenter.



52
53
54
# File 'lib/delegated_presenter/base.rb', line 52

def hide(*hidden_methods)
  self.hidden_methods += hidden_methods
end

.presents(*objects) ⇒ Object

Determine what objects what to present.

Examples:

Add some presentable objects:

presents :contact, :user, :client

Parameters:

  • :objects

    One or more models to be presented.



43
44
45
# File 'lib/delegated_presenter/base.rb', line 43

def presents(*objects)
  self.presentable += objects.flatten.collect{ |i| i.to_s.camelize.singularize }
end

Instance Method Details

#classObject

The class of the presented object.



102
103
104
# File 'lib/delegated_presenter/base.rb', line 102

def class
  presented_model.class
end

#presenter_classObject

The class of the presenter.



99
# File 'lib/delegated_presenter/base.rb', line 99

alias_method :presenter_class, :class