Module: Matestack::Ui::Core::Properties

Included in:
Base
Defined in:
lib/matestack/ui/core/properties.rb

Defined Under Namespace

Modules: ClassMethods, Initializer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
# File 'lib/matestack/ui/core/properties.rb', line 6

def self.included(base)
  base.extend ClassMethods
  base.send :prepend, Initializer
end

Instance Method Details

#contextObject Also known as: ctx



47
48
49
# File 'lib/matestack/ui/core/properties.rb', line 47

def context
  @context ||= OpenStruct.new
end

#create_contextObject



60
61
62
63
# 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) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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

#optional_property_keysObject



56
57
58
# File 'lib/matestack/ui/core/properties.rb', line 56

def optional_property_keys
  self.class.optional_property_keys || []
end

#required_property_keysObject



52
53
54
# File 'lib/matestack/ui/core/properties.rb', line 52

def required_property_keys
  self.class.required_property_keys || []
end

#set_textObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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