Module: FactoryBurgers::Presenters
- Defined in:
- lib/factory_burgers/presenters.rb,
lib/factory_burgers/presenters/base.rb
Overview
Module for adding, finding, and using application data presenters. Presenters are used to define what attributes to show in the front end, what to call the built objects, and managing links to the objects in the application if they exist.
Defined Under Namespace
Classes: Base
Class Method Summary collapse
-
.build_presenter(klass, &blk) ⇒ Object
block provided to ‘present`; build a presenter class.
-
.data_for(object) ⇒ Object
TODO: use this from FactoryOutput.
- .present(klass, with: nil, &blk) ⇒ Object
- .presenter_class_for(object) ⇒ Object
- .presenter_for(object) ⇒ Object
- .purge! ⇒ Object
Class Method Details
.build_presenter(klass, &blk) ⇒ Object
block provided to ‘present`; build a presenter class
46 47 48 |
# File 'lib/factory_burgers/presenters.rb', line 46 def build_presenter(klass, &blk) PresenterBuilder.new(klass).build(&blk) end |
.data_for(object) ⇒ Object
TODO: use this from FactoryOutput
36 37 38 39 40 41 42 43 |
# File 'lib/factory_burgers/presenters.rb', line 36 def data_for(object) presenter = presenter_for(object) or return nil { type: presenter.type, attributes: presenter.attributes, link: presenter.link_path, } end |
.present(klass, with: nil, &blk) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/factory_burgers/presenters.rb', line 11 def present(klass, with: nil, &blk) raise ArgumentError, "Provide `with` or block, but not both" if with && blk presenter = with || build_presenter(klass, &blk) @presenters[klass.to_s] = presenter end |
.presenter_class_for(object) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/factory_burgers/presenters.rb', line 27 def presenter_class_for(object) matching_class = object.class.ancestors.map(&:name).find do |class_name| @presenters.key?(class_name) end return matching_class ? @presenters[matching_class] : FactoryBurgers::Presenters::Base end |
.presenter_for(object) ⇒ Object
23 24 25 |
# File 'lib/factory_burgers/presenters.rb', line 23 def presenter_for(object) presenter_class_for(object).new(object) end |
.purge! ⇒ Object
18 19 20 21 |
# File 'lib/factory_burgers/presenters.rb', line 18 def purge! @links = {} @presenters = {} end |