module Matestack::Ui::Core::Properties

Public Class Methods

included(base) click to toggle source
# File lib/matestack/ui/core/properties.rb, line 6
def self.included(base)
  base.extend ClassMethods
  base.send :prepend, Initializer
end

Public Instance Methods

context() click to toggle source
# File lib/matestack/ui/core/properties.rb, line 47
def context
  @context ||= OpenStruct.new
end
Also aliased as: ctx
create_context() click to toggle source
# File lib/matestack/ui/core/properties.rb, line 60
def create_context
  create_context_for_properties(self.required_property_keys, required: true)
  create_context_for_properties(self.optional_property_keys)
end
create_context_for_properties(properties, required: false) click to toggle source
# File lib/matestack/ui/core/properties.rb, line 65
def create_context_for_properties(properties, required: false)
  properties.uniq.each do |property|
    if property.is_a? Hash
      property.each do |key, value|
        method_name = value[:as] || key
        raise "required property '#{key}' is missing for '#{self.class}'" if required && self.options[key].nil?
        context.send(:"#{method_name}=", self.options.delete(key))
      end
    else
      raise "required property '#{property}' is missing for '#{self.class}'" if required && self.options[property].nil?
      context.send(:"#{property}=", self.options.delete(property))
    end
  end if properties
end
ctx()
Alias for: context
optional_property_keys() click to toggle source
# File lib/matestack/ui/core/properties.rb, line 56
def optional_property_keys
  self.class.optional_property_keys || []
end
required_property_keys() click to toggle source
# File lib/matestack/ui/core/properties.rb, line 52
def required_property_keys
  self.class.required_property_keys || []
end
set_text() click to toggle source
# File lib/matestack/ui/core/properties.rb, line 80
def set_text
  # the text property is treated specially since 2.0.0 enables text injection for all components like:
  #
  # some_component "foo", class: "whatever" -> self.text -> "foo"
  #
  # prior to 2.0.0, text injection happened like that:
  #
  # some_component text: "foo", class: "whatever" -> self.options[:text] -> "foo"
  #
  # in both cases "foo" should be available via self.context.text AND self.text
  #
  # in 2.0.0 text is available via context.text if text is marked as required or optional
  # in order to have a consistent access, we make this text accessable via self.text as well in this case
  # in all cases, text is accessable via self.text AND self.context.text
  # we make the passed in text option available via context.text by default, even if not marked as required or optional
  #
  # additionally we need to delete text from the options, as they might be used to be rendered as
  # tag attributes without any whitelisting as happened prior to 2.0.0
  self.text = self.options.delete(:text) if self.options.has_key?(:text)
  self.context.text = self.text
end