Class: Lite::Component::Element
- Inherits:
-
Object
- Object
- Lite::Component::Element
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/lite/component/element.rb
Direct Known Subclasses
Class Method Summary collapse
- .attribute(name, default: nil) ⇒ Object
- .attributes ⇒ Object
-
.element(name, multiple: false, &config) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity.
-
.elements ⇒ Object
rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity.
- .model_name ⇒ Object
Instance Method Summary collapse
-
#initialize(view, attributes = nil, &block) ⇒ Element
constructor
A new instance of Element.
- #to_s ⇒ Object (also: #render)
Constructor Details
#initialize(view, attributes = nil, &block) ⇒ Element
Returns a new instance of Element.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/lite/component/element.rb', line 9 def initialize(view, attributes = nil, &block) initialize_attributes(attributes || {}) initialize_elements @view = view @yield = block_given? ? @view.capture(self, &block) : nil validate! rescue ActiveModel::ValidationError => e raise Lite::Component::ValidationError, e. end |
Class Method Details
.attribute(name, default: nil) ⇒ Object
23 24 25 26 |
# File 'lib/lite/component/element.rb', line 23 def attribute(name, default: nil) attributes[name] = { default: default } define_method_or_raise(name) { get_instance_variable(name) } end |
.attributes ⇒ Object
28 29 30 |
# File 'lib/lite/component/element.rb', line 28 def attributes @attributes ||= {} end |
.element(name, multiple: false, &config) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/lite/component/element.rb', line 34 def element(name, multiple: false, &config) plural_name = name.to_s.pluralize.to_sym if multiple elements[name] = { multiple: plural_name || false, class: Class.new(Element, &config) } define_method_or_raise(name) do |attributes = nil, &block| return get_instance_variable(multiple ? plural_name : name) unless attributes || block element = self.class.elements[name][:class].new(@view, attributes, &block) if multiple get_instance_variable(plural_name) << element else set_instance_variable(name, element) end end return if !multiple || name == plural_name define_method_or_raise(plural_name) { get_instance_variable(plural_name) } end |
.elements ⇒ Object
rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
61 62 63 |
# File 'lib/lite/component/element.rb', line 61 def elements @elements ||= {} end |
Instance Method Details
#to_s ⇒ Object Also known as: render
79 80 81 |
# File 'lib/lite/component/element.rb', line 79 def to_s @yield end |