Class: Smooth::Presentable::Chain

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#formatObject

Returns the value of attribute format.



10
11
12
# File 'lib/smooth/presentable/chain.rb', line 10

def format
  @format
end

#presenter_methodObject

Returns the value of attribute presenter_method.



10
11
12
# File 'lib/smooth/presentable/chain.rb', line 10

def presenter_method
  @presenter_method
end

#recipientObject

Returns the value of attribute recipient.



10
11
12
# File 'lib/smooth/presentable/chain.rb', line 10

def recipient
  @recipient
end

#scopeObject

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

#resultsObject



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_aObject



36
37
38
# File 'lib/smooth/presentable/chain.rb', line 36

def to_a
  results
end