Class: OpenComponents::PrerenderedComponent
- Defined in:
- lib/opencomponents/prerendered_component.rb
Overview
Wrapper object for components using the ‘pre-rendered` rendering mode.
Constant Summary collapse
- DEFAULT_HEADERS =
Internal: Default HTTP headers to send when requesting a component.
{accept: 'application/vnd.oc.prerendered+json'}
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Public: Returns a Hash of data to use when rendering the component.
-
#template ⇒ Object
readonly
Public: Returns a Template with data for rendering the raw component.
Attributes inherited from Component
#headers, #href, #name, #params, #registry_version, #render_mode, #type, #version
Instance Method Summary collapse
-
#initialize(name, opts = {}) ⇒ PrerenderedComponent
constructor
Public: Initializes a new PrerenderedComponent.
-
#load ⇒ Object
Public: Executes a request for the Component against the configured registry and sets the component attributes.
Methods inherited from Component
#flush!, #reload!, #request_version
Constructor Details
#initialize(name, opts = {}) ⇒ PrerenderedComponent
Public: Initializes a new PrerenderedComponent.
name - The String name of the component to request. opts - A Hash of options to use when requesting the component
(default: {}).
:params - A Hash of parameters to send in the component request
(optional, default: {}).
:version - The String version of the component to request
(optional, default: nil).
:headers - A Hash of HTTP request headers to include in the
component request (optional, default: DEFAULT_HEADERS).
24 25 26 27 28 |
# File 'lib/opencomponents/prerendered_component.rb', line 24 def initialize(name, opts = {}) super(name, opts) @headers.merge!(DEFAULT_HEADERS) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Public: Returns a Hash of data to use when rendering the component.
8 9 10 |
# File 'lib/opencomponents/prerendered_component.rb', line 8 def data @data end |
#template ⇒ Object (readonly)
Public: Returns a Template with data for rendering the raw component.
11 12 13 |
# File 'lib/opencomponents/prerendered_component.rb', line 11 def template @template end |
Instance Method Details
#load ⇒ Object
Public: Executes a request for the Component against the configured
registry and sets the component attributes.
Examples
component = OpenComponents::RenderedComponent.new('my-component')
component.load
# => #<OpenComponents::RenderedComponent: ... >
Returns the Component with attributes set.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/opencomponents/prerendered_component.rb', line 40 def load template_data = response_data['template'] @data = response_data['data'] @template = Template.new( template_data['src'], template_data['type'], template_data['key'] ) super end |