Class: Search::PresenterFactory

Inherits:
Object
  • Object
show all
Defined in:
app/presenters/search/presenter_factory.rb

Overview

Factory that resolves a view-model instance to the appropriate Search presenter.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(presentable, _presentable_type = nil) ⇒ PresenterFactory

Creates a new factory for the given presentable object.

Parameters:

  • presentable (Object)

    the view-model instance to present

  • _presentable_type (String, nil) (defaults to: nil)

    optional base type override (unused; inferred from the class name)



20
21
22
23
24
# File 'app/presenters/search/presenter_factory.rb', line 20

def initialize(presentable, _presentable_type = nil)
  @namespace = "Search::"
  @presentable = presentable
  @presentable_type = presentable.class.name.scan(/View(\w+)/).join
end

Instance Attribute Details

#namespaceString (readonly)

Returns the namespace prefix used to resolve presenter classes.

Returns:

  • (String)

    the namespace prefix used to resolve presenter classes



8
9
10
# File 'app/presenters/search/presenter_factory.rb', line 8

def namespace
  @namespace
end

#presentableObject (readonly)

Returns the object being presented.

Returns:

  • (Object)

    the object being presented



11
12
13
# File 'app/presenters/search/presenter_factory.rb', line 11

def presentable
  @presentable
end

#presentable_typeString (readonly)

Returns the inferred base name of the presenter class.

Returns:

  • (String)

    the inferred base name of the presenter class



14
15
16
# File 'app/presenters/search/presenter_factory.rb', line 14

def presentable_type
  @presentable_type
end

Instance Method Details

#html(view_context = nil) ⇒ BasePresenter, Object

Returns the HTML presenter instance for the presentable object.

Falls back to the raw presentable when no presenter class exists.

Parameters:

Returns:



32
33
34
35
# File 'app/presenters/search/presenter_factory.rb', line 32

def html(view_context = nil)
  initialize_html_presenter_klass(view_context) ||
    initialize_fallback_presenter
end

#text(view_context = nil) ⇒ BasePresenter, Object

Returns the text presenter instance for the presentable object.

Falls back to the HTML presenter when no text presenter class exists.

Parameters:

Returns:



43
44
45
# File 'app/presenters/search/presenter_factory.rb', line 43

def text(view_context = nil)
  initialize_text_presenter(view_context) || html(view_context)
end