module Spark::Component::Element
Public Class Methods
included(base)
click to toggle source
# File lib/spark/component/element.rb, line 10 def self.included(base) base.extend(Spark::Component::Element::ClassMethods) %i[_name _parent _block view_context].each do |name| base.define_method(:"#{name}=") { |val| instance_variable_set(:"@#{name}", val) } base.define_method(:"#{name}") { instance_variable_get(:"@#{name}") } end end
new(attrs = nil)
click to toggle source
Initialize method on components must call super
Calls superclass method
# File lib/spark/component/element.rb, line 20 def initialize(attrs = nil) # Extract core element attributes # Ensure that elements have references to their: # - parent: enables elements to interact with their parent component # - block: used in render_self # - view_context, sets the view context for an element (in Rails) # unless attrs.nil? || attrs.empty? @_name = attrs.delete(:_name) @_parent = attrs.delete(:_parent) @_block = attrs.delete(:_block) @view_context = attrs.delete(:_view) end initialize_elements # Call Attributes.initialze super end
Public Instance Methods
blank?()
click to toggle source
# File lib/spark/component/element.rb, line 66 def blank? self.yield.nil? || self.yield.strip.empty? end
present?()
click to toggle source
# File lib/spark/component/element.rb, line 62 def present? !blank? end
render_block(view, &block)
click to toggle source
# File lib/spark/component/element.rb, line 54 def render_block(view, &block) block_given? ? view.capture(self, &block) : nil end
render_self()
click to toggle source
# File lib/spark/component/element.rb, line 40 def render_self # Guard with `rendered?` boolean in case render_block returns `nil` return @render_self if rendered? @render_self = render_block(@view_context, &_block) validate! if defined?(ActiveModel::Validations) @rendered = true @render_self end
rendered?()
click to toggle source
# File lib/spark/component/element.rb, line 50 def rendered? @rendered == true end
yield()
click to toggle source
# File lib/spark/component/element.rb, line 58 def yield render_self end
Private Instance Methods
element_attribute_default()
click to toggle source
# File lib/spark/component/element.rb, line 102 def element_attribute_default @element_attribute_default ||= {} end
get_element_variable(name)
click to toggle source
Simplify accessing namespaced element instance variables
# File lib/spark/component/element.rb, line 92 def get_element_variable(name) instance_variable_get(:"@element_#{name}") end
initialize_elements()
click to toggle source
Create instance variables for each element
# File lib/spark/component/element.rb, line 73 def initialize_elements self.class.elements.each do |name, options| if (plural_name = options[:multiple]) # Setting an empty array allows us to enumerate # without needing to check for presence first. set_element_variable(plural_name, []) else set_element_variable(name, nil) end end end
merge_element_attribute_default(name, attributes)
click to toggle source
Merge user defined attributes with the overriden attributes of an element
# File lib/spark/component/element.rb, line 107 def merge_element_attribute_default(name, attributes) attrs = element_attribute_default[name] attributes = attrs.merge(attributes || {}) unless attrs.nil? || attrs.empty? attributes end
set_element_attribute_default(element, attrs = {})
click to toggle source
Override the default value for an element's attribute(s)
# File lib/spark/component/element.rb, line 97 def set_element_attribute_default(element, attrs = {}) element_attribute_default[element] ||= {} element_attribute_default[element].merge!(attrs) end
set_element_variable(name, value)
click to toggle source
Simplify accessing namespaced element instance variables
# File lib/spark/component/element.rb, line 87 def set_element_variable(name, value) instance_variable_set(:"@element_#{name}", value) end