class StrongPresenter::Presenter

Public Class Methods

new(object) { |self| ... } click to toggle source

Constructs the presenter, taking 1 argument for the object being wrapped. For example:

user_presenter = UserPresenter.new @user

A block can also be passed to use the presenter. For example:

<% UserPresenter.new @user do |user_presenter| %>
  Username: <%= user_presenter.username %>
<% end %>
# File lib/strong_presenter/presenter.rb, line 23
def initialize(object)
  @object = object
  yield self if block_given?
end

Protected Class Methods

alias_object_to_object_class_name() click to toggle source
# File lib/strong_presenter/presenter.rb, line 62
def alias_object_to_object_class_name
  if object_class?
    alias_method object_class.name.underscore, :object
    private object_class.name.underscore
  end
end
inferred_presenter(object) click to toggle source
# File lib/strong_presenter/presenter.rb, line 57
def inferred_presenter(object)
  Inferrer.new(object.class.name).inferred_class { |name| "#{name}Presenter" } or raise StrongPresenter::UninferrablePresenterError.new(self)
end
set_presenter_collection() click to toggle source
# File lib/strong_presenter/presenter.rb, line 69
def set_presenter_collection
  collection_presenter = get_collection_presenter
  const_set "Collection", collection_presenter # will overwrite if constant only defined in superclass
end

Private Class Methods

get_collection_presenter() click to toggle source
# File lib/strong_presenter/presenter.rb, line 81
def get_collection_presenter
  collection_presenter = Inferrer.new(name).chomp("Presenter").inferred_class {|name| "#{name.pluralize}Presenter"}
  return collection_presenter unless collection_presenter.nil? || collection_presenter == self
  Class.new(StrongPresenter::CollectionPresenter).presents_with(self)
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/strong_presenter/presenter.rb, line 75
def inherited(subclass)
  subclass.alias_object_to_object_class_name
  subclass.set_presenter_collection
  super
end
object_class() click to toggle source

Returns the source class corresponding to the presenter class, as set by {presents}, or as inferred from the presenter class name (e.g. `ProductPresenter` maps to `Product`).

@return [Class] the source class that corresponds to this presenter.

# File lib/strong_presenter/presenter.rb, line 92
def object_class
  @object_class ||= Inferrer.new(name).chomp("Presenter").inferred_class or raise UninferrableSourceError.new(self)
end
object_class?() click to toggle source

Checks whether this presenter class has a corresponding {object_class}.

# File lib/strong_presenter/presenter.rb, line 97
def object_class?
  !!(@object_class ||= Inferrer.new(name).chomp("Presenter").inferred_class)
end
presents(name) click to toggle source

Sets the model presented by the class

# File lib/strong_presenter/presenter.rb, line 103
def presents name
  @object_class = name.to_s.camelize.constantize
  alias_object_to_object_class_name
end

Public Instance Methods

attributes() click to toggle source

@return [Hash] the object's attributes, sliced to only include those implemented by the presenter.

# File lib/strong_presenter/presenter.rb, line 41
def attributes
  object.attributes.select {|attribute, _| respond_to?(attribute) }
end
to_model() click to toggle source

ActiveModel compatibility @private

# File lib/strong_presenter/presenter.rb, line 35
def to_model
  self
end

Protected Instance Methods

object() click to toggle source
# File lib/strong_presenter/presenter.rb, line 52
def object
  @object
end