Module: Resubject::Builder

Defined in:
lib/resubject/builder.rb

Defined Under Namespace

Classes: InvalidPresenterArgument

Class Method Summary collapse

Class Method Details

.present(objects, template, *presenters) ⇒ Presenter+

Presents a object or a collection of objects

Examples:


Builder.present box, template
# => <BoxPresenter>

# Using a custom presenter
Builder.present box, template, CustomPresenter
# => <CustomPresenter>

# Using multiple presenters
Builder.present box, template, OnePresenter, CustomPresenter
# => <OnePresenter<CustomPresenter>>

# Using a collection
Builder.present [box, box], template
# => [<BoxPresenter>, <BoxPresenter>]

Parameters:

  • objects (Object, Array<Object>)

    objects to be instantiated with related presenter

  • template

    then HTML template context

  • presenters (Presenter)

    one or multiple presenters

Returns:

See Also:



35
36
37
38
39
40
41
# File 'lib/resubject/builder.rb', line 35

def self.present(objects, template, *presenters)
  if objects.respond_to?(:each)
    Builder.present_all(objects, template, *presenters)
  else
    Builder.present_one(objects, template, *presenters)
  end
end

.present_all(objects, template, *presenters) ⇒ Array<Presenter>

Presents a collection of objects (see .present)

Examples:


Builder.present [box, box], template
# => [<BoxPresenter>, <BoxPresenter>]

Parameters:

  • objects (Array<Object>)

    objects to be instantiated with related presenter

  • template

    then HTML template context

  • presenters (Presenter)

    one or multiple presenters

Returns:

  • (Array<Presenter>)

    collection of instances of the related presenter

See Also:



96
97
98
99
100
# File 'lib/resubject/builder.rb', line 96

def self.present_all(objects, template, *presenters)
  objects.map do |o|
    Builder.present_one(o, template, *presenters)
  end
end

.present_one(object, template, *presenters) ⇒ Presenter

Presents a single object (see .present)

Examples:


Builder.present_one box, context                              # => <BoxPresenter>
Builder.present_one box, context, CustomPresenter             # => <CustomPresenter>
Builder.present_one box, context, OnePresenter, TwoPresenter  # => <TwoPresenter<OnePresenter>>

Skips nil classes


Builder.present_one nil
# => nil

Parameters:

  • object (Object)

    object to be instantiated with related presenter

  • template

    then HTML template context

  • presenters (Presenter)

    one or multiple presenters

Returns:

  • (Presenter)

    either instance of the presenter related to the object

Raises:

See Also:



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/resubject/builder.rb', line 66

def self.present_one(object, template, *presenters)
  return unless object

  presenters = [Naming.presenter_for(object)] unless presenters.any?

  unless presenters.all? { |p| p.is_a?(Class) && p.ancestors.include?(Resubject::Presenter) }
    raise InvalidPresenterArgument, "Expected a presenter in #{presenters.inspect}"
  end

  presenters.inject(object) do |presented, klass|
    klass.new(presented, template)
  end
end