module Rafters::Component

Attributes

controller[W]
identifier[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/rafters/component.rb, line 11
def initialize(options = {})
  raise IdentifierMissing unless options.has_key?(:as)

  @source = if options.has_key?(:source)
    options.delete(:source).constantize.new(self)
  end

  @identifier = options.delete(:as)
  @settings = options.delete(:settings) || {}
end

Public Instance Methods

as_json() click to toggle source
# File lib/rafters/component.rb, line 84
def as_json
  { identifier => { "class" => self.class.name, "attributes" => attributes.as_json } }
end
attributes() click to toggle source
# File lib/rafters/component.rb, line 42
def attributes
  return {} if self.class._attributes.nil?

  @_attributes ||= Hashie::Mash.new.tap do |_attributes|
    (self.class._attributes || []).each do |name|
      _attributes[name] = send(name)
    end
  end
end
controller(variable_or_method_name) click to toggle source
# File lib/rafters/component.rb, line 74
def controller(variable_or_method_name)
  if @controller.instance_variable_defined?("@#{variable_or_method_name}")
    @controller.instance_variable_get("@#{variable_or_method_name}")
  elsif @controller.respond_to?(variable_or_method_name, true)
    @controller.send(variable_or_method_name)
  else
    nil
  end
end
defaults() click to toggle source
# File lib/rafters/component.rb, line 56
def defaults
  @_defaults ||= Hashie::Mash.new.tap do |_defaults|
    (self.class._defaults || {}).each do |name, value|
      _defaults[name] = value.is_a?(Proc) ? value.call(self) : value
    end
  end
end
name() click to toggle source
# File lib/rafters/component.rb, line 22
def name
  self.class.name.underscore
end
overrides() click to toggle source
# File lib/rafters/component.rb, line 64
def overrides
  return {} if @controller.nil?

  @_overrides ||= Hashie::Mash.new.tap do |_overrides|
    (controller(:params)[identifier] || {}).each do |name, value|
      _overrides[name] = value
    end
  end
end
settings() click to toggle source
# File lib/rafters/component.rb, line 52
def settings
  @_settings ||= Hashie::Mash.new(defaults.merge(@settings).merge(overrides))
end
source() click to toggle source
# File lib/rafters/component.rb, line 26
def source
  if @source.nil?
    raise SourceMissing
  else
    @source
  end
end
template_name() click to toggle source
# File lib/rafters/component.rb, line 34
def template_name
  @_template_name ||= begin
    _template_name = (self.class._template_name || self.class.name.underscore)
    _template_name = _template_name.call(self) if _template_name.is_a?(Proc)
    _template_name
  end
end