Module: Resubject::Naming

Defined in:
lib/resubject/naming.rb

Class Method Summary collapse

Class Method Details

.presenter_for(presentable) ⇒ Presenter

Discover the presenter class given the class name or string/symbol passed

Examples:


Naming.presenter_for :post         # => PostPresenter
Naming.presenter_for "post"        # => PostPresenter
Naming.presenter_for Post.new      # => PostPresenter
Naming.presenter_for Ns::Post.new  # => Ns::PostPresenter

Parameters:

  • presentable (Object, String, Symbol)

    the reference object

Returns:

  • (Presenter)

    the related presenter class based on the object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/resubject/naming.rb', line 16

def self.presenter_for(presentable)
  klass = case presentable
          when Symbol
            presentable.to_s
          when String
            presentable
          else
            presentable.class.to_s
          end

  presenter = "#{klass.camelize}Presenter"

  # Gets each constant in the namespace
  presenter.split('::').inject(Object) { |ns, cons| ns.const_get(cons) }
end