Class: Transit::Delivery

Inherits:
Object
  • Object
show all
Defined in:
lib/transit/delivery.rb

Overview

Manages delivery results for each available context. This allows easily customizing how a particular context is output into the page.

Deliveries are configured using the .configure class method, passing the name of the context class the delivery applies to, and a block specifying how to process the delivery. Each proc/lambda is passed the context to be delivered, and is evaluated in the scope of the current view.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Delivery) initialize(instance, tpl, opts = {})

Create a new delivery instance for the provided resource.

Parameters:

  • instance (Object)

    A deliverable class/object

  • tpl (Object)

    The current instance of a view or template



19
20
21
22
23
24
# File 'lib/transit/delivery.rb', line 19

def initialize(instance, tpl, opts = {})
  @template = tpl
  @resource = instance
  @options  = opts
  self
end

Instance Attribute Details

- (Object) options (readonly)

Returns the value of attribute options



11
12
13
# File 'lib/transit/delivery.rb', line 11

def options
  @options
end

- (Object) resource (readonly)

Returns the value of attribute resource



11
12
13
# File 'lib/transit/delivery.rb', line 11

def resource
  @resource
end

- (Object) template (readonly)

Returns the value of attribute template



11
12
13
# File 'lib/transit/delivery.rb', line 11

def template
  @template
end

Instance Method Details

- (Object) deliver

Loops through each context within a resource, and performs its delivery, returning the final output.

Deliveries can be defined in one of three ways:

1. By adding a .deliver method to a context. Unless this method explicitly returns `false` 
   the result of this method will be used.
2. Creating a partial under app/views/transit/contexts/_context_name.html.erb (ie: _audio.html.erb)
3. Configuring a deliverable block via Transit::Delivery.configure

Raises:

  • (UndeliverableResourceError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/transit/delivery.rb', line 36

def deliver

  # Support delivering only a single context
  if ::Context.descendants.include?(resource.class)
    return deliver_context(resource)
  end
  raise UndeliverableResourceError.new("The class #{resource.class.name} is not deliverable.") unless resource.respond_to?(:contexts)
  
  response = template.capture do
    resource.contexts.ascending(:position).each do |context|          
      template.concat(deliver_context(context))
    end
  end
  response.to_s.html_safe
end