Class: Para::ComponentsConfiguration::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/para/components_configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, type_identifier, options = {}) ⇒ Component

Returns a new instance of Component.



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/para/components_configuration.rb', line 139

def initialize(identifier, type_identifier, options = {})
  self.identifier = identifier.to_s
  self.type = Para::Component.registered_components[type_identifier]
  self.options = options

  unless type
    raise UndefinedComponentTypeError.new(
      "Undefined Para component : #{ type_identifier }. " +
      "Please ensure that your app or gems define this component type."
    )
  end
end

Instance Attribute Details

#identifierObject

Returns the value of attribute identifier.



137
138
139
# File 'lib/para/components_configuration.rb', line 137

def identifier
  @identifier
end

#modelObject

Returns the value of attribute model.



137
138
139
# File 'lib/para/components_configuration.rb', line 137

def model
  @model
end

#optionsObject

Returns the value of attribute options.



137
138
139
# File 'lib/para/components_configuration.rb', line 137

def options
  @options
end

#typeObject

Returns the value of attribute type.



137
138
139
# File 'lib/para/components_configuration.rb', line 137

def type
  @type
end

Instance Method Details

#options_with_defaultsObject

Ensures unset :configuration store options are set to nil to allow removing a configuration option from the components.rb file



161
162
163
164
165
166
167
168
169
# File 'lib/para/components_configuration.rb', line 161

def options_with_defaults
  configurable_keys = type.local_stored_attributes.try(:[], :configuration) || []
  configurable_keys += options.keys
  configurable_keys.uniq!

  configurable_keys.each_with_object({}) do |key, hash|
    hash[key] = options[key]
  end
end

#refresh(attributes = {}) ⇒ Object



152
153
154
155
156
# File 'lib/para/components_configuration.rb', line 152

def refresh(attributes = {})
  self.model = type.where(identifier: identifier).first_or_initialize
  model.assign_attributes(attributes.merge(options_with_defaults))
  model.save!
end