Class: OpenComponents::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/opencomponents/component.rb

Overview

Wrapper object for a component fetched from an OC registry.

Direct Known Subclasses

PrerenderedComponent, RenderedComponent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Component

Public: Initializes a new Component subclass.

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: '').
     :headers - A Hash of HTTP request headers to include in the
       component request (optional, default: {}).


42
43
44
45
46
47
# File 'lib/opencomponents/component.rb', line 42

def initialize(name, opts = {})
  @name    = name
  @params  = opts[:params]  || {}
  @version = opts[:version] || ''
  @headers = opts[:headers] || {}
end

Instance Attribute Details

#headersObject

Public: Gets/sets a Hash of headers to send in the component request.



14
15
16
# File 'lib/opencomponents/component.rb', line 14

def headers
  @headers
end

#hrefObject (readonly)

Public: Returns the String URL of the requested component.



17
18
19
# File 'lib/opencomponents/component.rb', line 17

def href
  @href
end

#nameObject

Public: Gets/sets the String name of the component.



5
6
7
# File 'lib/opencomponents/component.rb', line 5

def name
  @name
end

#paramsObject

Public: Gets/sets a Hash of params to send in the component request.



11
12
13
# File 'lib/opencomponents/component.rb', line 11

def params
  @params
end

#registry_versionObject (readonly)

Public: Returns the String version of the component as served by the

registry.


21
22
23
# File 'lib/opencomponents/component.rb', line 21

def registry_version
  @registry_version
end

#render_modeObject (readonly)

Public: Returns the String render mode of the component as served by the

registry. Generally either `rendered` or `pre-rendered`.


29
30
31
# File 'lib/opencomponents/component.rb', line 29

def render_mode
  @render_mode
end

#typeObject (readonly)

Public: Returns the String type of the component as served by the

registry.


25
26
27
# File 'lib/opencomponents/component.rb', line 25

def type
  @type
end

#versionObject

Public: Gets/sets the desired String version of the component.



8
9
10
# File 'lib/opencomponents/component.rb', line 8

def version
  @version
end

Instance Method Details

#flush!Object

Public: Resets all component attributes from a registry response to ‘nil`.

Examples

component.flush!
# => #<OpenComponents::RenderedComponent: ... >

Returns the reset Component.



63
64
65
66
67
68
69
# File 'lib/opencomponents/component.rb', line 63

def flush!
  flush_variables_whitelist.each do |var|
    instance_variable_set(var, nil)
  end

  self
end

#reload!Object

Public: Resets all component attributes and reloads them from a registry

response.

Examples

component.reload!
# => #<OpenComponents::RenderedComponent: ... >

Returns the reloaded Component.



80
81
82
83
# File 'lib/opencomponents/component.rb', line 80

def reload!
  flush!
  load
end

#request_versionObject

Public: Returns the String value of ‘requestVersion` from a component

response, `nil` if not present.


51
52
53
# File 'lib/opencomponents/component.rb', line 51

def request_version
  @request_version == '' ? nil : @request_version
end