Class: Smooth::Presentable::Chain
- Inherits:
-
Object
- Object
- Smooth::Presentable::Chain
- Defined in:
- lib/smooth/presentable/chain.rb
Overview
The Chain allows for constructing an API call with the following:
Resource.present(query)
.as(format)
.to(user)
Instance Attribute Summary collapse
-
#format ⇒ Object
Returns the value of attribute format.
-
#presenter_method ⇒ Object
Returns the value of attribute presenter_method.
-
#recipient ⇒ Object
Returns the value of attribute recipient.
-
#scope ⇒ Object
Returns the value of attribute scope.
Instance Method Summary collapse
- #as(format = :default) ⇒ Object
-
#initialize(scope) ⇒ Chain
constructor
A new instance of Chain.
- #method_missing(meth, *args, &block) ⇒ Object
- #results ⇒ Object
- #to(recipient = :default) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(scope) ⇒ Chain
Returns a new instance of Chain.
12 13 14 15 16 |
# File 'lib/smooth/presentable/chain.rb', line 12 def initialize(scope) @scope = scope @format = :default @recipient = :default end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
40 41 42 |
# File 'lib/smooth/presentable/chain.rb', line 40 def method_missing meth, *args, &block results.send(meth, *args, &block) end |
Instance Attribute Details
#format ⇒ Object
Returns the value of attribute format.
10 11 12 |
# File 'lib/smooth/presentable/chain.rb', line 10 def format @format end |
#presenter_method ⇒ Object
Returns the value of attribute presenter_method.
10 11 12 |
# File 'lib/smooth/presentable/chain.rb', line 10 def presenter_method @presenter_method end |
#recipient ⇒ Object
Returns the value of attribute recipient.
10 11 12 |
# File 'lib/smooth/presentable/chain.rb', line 10 def recipient @recipient end |
#scope ⇒ Object
Returns the value of attribute scope.
10 11 12 |
# File 'lib/smooth/presentable/chain.rb', line 10 def scope @scope end |
Instance Method Details
#as(format = :default) ⇒ Object
18 19 20 21 |
# File 'lib/smooth/presentable/chain.rb', line 18 def as(format=:default) @format = format self end |
#results ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/smooth/presentable/chain.rb', line 28 def results @presenter_method ||= scope.klass.presenter_format_for_role(recipient, format) @results ||= scope.map do |record| record.present_as(presenter_method) end end |
#to(recipient = :default) ⇒ Object
23 24 25 26 |
# File 'lib/smooth/presentable/chain.rb', line 23 def to(recipient=:default) @recipient = recipient self end |
#to_a ⇒ Object
36 37 38 |
# File 'lib/smooth/presentable/chain.rb', line 36 def to_a results end |