Class: SparkComponents::Element
- Inherits:
-
Object
- Object
- SparkComponents::Element
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/spark_components/element.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attr ⇒ Object
readonly
Returns the value of attribute attr.
-
#parents ⇒ Object
readonly
Returns the value of attribute parents.
-
#yield ⇒ Object
Returns the value of attribute yield.
Class Method Summary collapse
- .add_class(*args) ⇒ Object
- .aria_attr(*args) ⇒ Object
- .attribute(*args) ⇒ Object
- .attributes ⇒ Object
- .base_class(name = nil) ⇒ Object
- .data_attr(*args) ⇒ Object
-
.element(name, multiple: false, component: nil, &config) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity.
- .elements ⇒ Object
- .inherited(subclass) ⇒ Object
- .model_name ⇒ Object
- .root_attr(*args) ⇒ Object
- .set_attribute(name, default: nil) ⇒ Object
- .tag_attrs ⇒ Object
- .validates_choice(name, choices, required: true) ⇒ Object
Instance Method Summary collapse
- #add_class(*args) ⇒ Object
- #after_init ⇒ Object
- #aria_attr(*args) ⇒ Object
- #base_class(name = nil) ⇒ Object
- #before_render ⇒ Object
-
#blank? ⇒ Boolean
blank? is aliased to an element’s content to easily determine if content is blank.
- #classnames(*args) ⇒ Object
- #data_attr(*args) ⇒ Object
-
#initialize(view, attributes = nil, &block) ⇒ Element
constructor
A new instance of Element.
- #join_class(*args) ⇒ Object
- #parent ⇒ Object
- #parent=(obj) ⇒ Object
- #root_attr(*args) ⇒ Object
- #tag_attrs ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(view, attributes = nil, &block) ⇒ Element
Returns a new instance of Element.
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/spark_components/element.rb', line 139 def initialize(view, attributes = nil, &block) @view = view attributes ||= {} initialize_tag_attrs assign_tag_attrs(attributes) initialize_attributes(attributes) initialize_elements extend_view_methods after_init @yield = render_block(&block) validate! end |
Instance Attribute Details
#attr ⇒ Object (readonly)
Returns the value of attribute attr.
8 9 10 |
# File 'lib/spark_components/element.rb', line 8 def attr @attr end |
#parents ⇒ Object (readonly)
Returns the value of attribute parents.
8 9 10 |
# File 'lib/spark_components/element.rb', line 8 def parents @parents end |
#yield ⇒ Object
Returns the value of attribute yield.
7 8 9 |
# File 'lib/spark_components/element.rb', line 7 def yield @yield end |
Class Method Details
.add_class(*args) ⇒ Object
48 49 50 |
# File 'lib/spark_components/element.rb', line 48 def self.add_class(*args) tag_attrs.add_class(*args) end |
.aria_attr(*args) ⇒ Object
56 57 58 59 |
# File 'lib/spark_components/element.rb', line 56 def self.aria_attr(*args) arg = attribute(*args) tag_attrs.aria(arg) end |
.attribute(*args) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/spark_components/element.rb', line 22 def self.attribute(*args) args.each_with_object({}) do |arg, obj| if arg.is_a?(Hash) arg.each do |attr, default| obj[attr.to_sym] = default set_attribute(attr.to_sym, default: default) end else obj[arg.to_sym] = nil set_attribute(arg.to_sym) end end end |
.attributes ⇒ Object
14 15 16 |
# File 'lib/spark_components/element.rb', line 14 def self.attributes @attributes ||= {} end |
.base_class(name = nil) ⇒ Object
44 45 46 |
# File 'lib/spark_components/element.rb', line 44 def self.base_class(name = nil) tag_attrs.base_class(name) end |
.data_attr(*args) ⇒ Object
52 53 54 |
# File 'lib/spark_components/element.rb', line 52 def self.data_attr(*args) tag_attrs.data(attribute(*args)) end |
.element(name, multiple: false, component: nil, &config) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/spark_components/element.rb', line 84 def self.element(name, multiple: false, component: nil, &config) plural_name = name.to_s.pluralize.to_sym if multiple # Extend components by string or class; e.g., "core/header" or Core::HeaderComponent component = "#{component}_component".classify.constantize if component.is_a?(String) elements[name] = { multiple: plural_name || false, class: Class.new((component || 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) element.parent = self if multiple get_instance_variable(plural_name) << element else set_instance_variable(name, element) end if element.respond_to?(:render) element.before_render element.yield = element.render end end return if !multiple || name == plural_name define_method_or_raise(plural_name) do get_instance_variable(plural_name) end end |
.elements ⇒ Object
18 19 20 |
# File 'lib/spark_components/element.rb', line 18 def self.elements @elements ||= {} end |
.inherited(subclass) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/spark_components/element.rb', line 132 def self.inherited(subclass) attributes.each { |name, | subclass.set_attribute(name, .dup) } elements.each { |name, | subclass.elements[name] = .dup } subclass.tag_attrs.merge!(tag_attrs.dup) end |
.model_name ⇒ Object
10 11 12 |
# File 'lib/spark_components/element.rb', line 10 def self.model_name ActiveModel::Name.new(SparkComponents::Element) end |
.root_attr(*args) ⇒ Object
61 62 63 |
# File 'lib/spark_components/element.rb', line 61 def self.root_attr(*args) tag_attrs.root(attribute(*args)) end |
.set_attribute(name, default: nil) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/spark_components/element.rb', line 36 def self.set_attribute(name, default: nil) attributes[name] = { default: default } define_method_or_raise(name) do get_instance_variable(name) end end |
.tag_attrs ⇒ Object
65 66 67 |
# File 'lib/spark_components/element.rb', line 65 def self.tag_attrs @tag_attrs ||= SparkComponents::Attributes::Tag.new end |
.validates_choice(name, choices, required: true) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/spark_components/element.rb', line 69 def self.validates_choice(name, choices, required: true) choices = choices.dup choices = [choices] unless choices.is_a?(Array) supported_choices = choices.map { |c| c.is_a?(String) ? c.to_sym : c.to_s }.concat(choices) choices = choices.to_sentence(last_word_connector: ", or ") = "\"%<value>s\" is not valid. Options for #{name} include: #{choices}" validates(name, inclusion: { in: supported_choices, message: }, allow_blank: !required) end |
Instance Method Details
#add_class(*args) ⇒ Object
173 174 175 |
# File 'lib/spark_components/element.rb', line 173 def add_class(*args) classnames(*args) end |
#after_init ⇒ Object
152 |
# File 'lib/spark_components/element.rb', line 152 def after_init; end |
#aria_attr(*args) ⇒ Object
185 186 187 |
# File 'lib/spark_components/element.rb', line 185 def aria_attr(*args) @tag_attrs.aria(*args) end |
#base_class(name = nil) ⇒ Object
168 169 170 171 |
# File 'lib/spark_components/element.rb', line 168 def base_class(name = nil) classnames.base = name unless name.nil? classnames.base end |
#before_render ⇒ Object
154 |
# File 'lib/spark_components/element.rb', line 154 def before_render; end |
#blank? ⇒ Boolean
blank? is aliased to an element’s content to easily determine if content is blank. This is because template conditionals may render an element’s content empty.
204 205 206 |
# File 'lib/spark_components/element.rb', line 204 def blank? @yield.blank? end |
#classnames(*args) ⇒ Object
164 165 166 |
# File 'lib/spark_components/element.rb', line 164 def classnames(*args) @tag_attrs.classnames(*args) end |
#data_attr(*args) ⇒ Object
181 182 183 |
# File 'lib/spark_components/element.rb', line 181 def data_attr(*args) @tag_attrs.data(*args) end |
#join_class(*args) ⇒ Object
177 178 179 |
# File 'lib/spark_components/element.rb', line 177 def join_class(*args) classnames.join_class(*args) end |
#parent ⇒ Object
160 161 162 |
# File 'lib/spark_components/element.rb', line 160 def parent @parents.last end |
#parent=(obj) ⇒ Object
156 157 158 |
# File 'lib/spark_components/element.rb', line 156 def parent=(obj) @parents = [obj.parents, obj].flatten.compact end |
#root_attr(*args) ⇒ Object
189 190 191 |
# File 'lib/spark_components/element.rb', line 189 def root_attr(*args) @tag_attrs.root(*args) end |
#tag_attrs ⇒ Object
193 194 195 |
# File 'lib/spark_components/element.rb', line 193 def tag_attrs @tag_attrs.attrs end |
#to_s ⇒ Object
197 198 199 |
# File 'lib/spark_components/element.rb', line 197 def to_s @yield end |