Class: PresenterObject::Base

Inherits:
Object
  • Object
show all
Includes:
Delegation
Defined in:
lib/presenter_object/base.rb

Overview

Presenter superclass. Create your own presenter by sub-classing ‘PresenterObject::Base` and declaring the name of the model it will present.

e.g:

class DocumentPresenter << PresenterObject::Base
  presents :document

 .. add instance methods here ..
end

Create a presenter instance by giving it a model instance e.g:

document = DocumentPresenter.new Document.find(params[:id])
document.sender # can call any document model methods...
document.pretty_sender # ...as well as any presenter defined method
document.class # is the wrapped object's class, helps it behave like models do in forms, link helpers etc.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delegation

#method_missing, #respond_to?

Constructor Details

#initialize(object, view_context = nil) ⇒ Base

Returns a new instance of Base.



38
39
40
41
42
43
# File 'lib/presenter_object/base.rb', line 38

def initialize(object, view_context = nil)
  @object = object
  @class = object.class # impersonate object's class
  @to_param = parameterize object
  @view_context = view_context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class PresenterObject::Delegation

Instance Attribute Details

#classObject (readonly)

Returns the value of attribute class.



21
22
23
# File 'lib/presenter_object/base.rb', line 21

def class
  @class
end

#objectObject (readonly)

Returns the value of attribute object.



21
22
23
# File 'lib/presenter_object/base.rb', line 21

def object
  @object
end

#to_paramObject (readonly)

Returns the value of attribute to_param.



21
22
23
# File 'lib/presenter_object/base.rb', line 21

def to_param
  @to_param
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



21
22
23
# File 'lib/presenter_object/base.rb', line 21

def view_context
  @view_context
end

Class Method Details

.presented_classObject



29
30
31
# File 'lib/presenter_object/base.rb', line 29

def presented_class
  @presented_class ||= @class_name.constantize
end

.presentersObject



33
34
35
# File 'lib/presenter_object/base.rb', line 33

def presenters
  @@presenters ||= {}
end

.presents(class_name) ⇒ Object



24
25
26
27
# File 'lib/presenter_object/base.rb', line 24

def presents(class_name)
  @class_name = class_name.to_s.classify
  presenters[@class_name] = self
end