class Lite::Component::Base
Attributes
c[R]
components[RW]
context[R]
h[R]
helpers[R]
i[R]
iteration[R]
options[R]
Public Class Methods
build(name)
click to toggle source
# File lib/lite/component/base.rb, line 32 def build(name) return name if name.respond_to?(:component_path) "#{name}_component".classify.constantize end
component_name()
click to toggle source
# File lib/lite/component/base.rb, line 38 def component_name component_path.split('/').last end
component_path()
click to toggle source
# File lib/lite/component/base.rb, line 42 def component_path name.underscore.sub('_component', '') end
new(context, options = {}) { |self| ... }
click to toggle source
rubocop:disable Layout/LineLength
# File lib/lite/component/base.rb, line 14 def initialize(context, options = {}, &block) @components = [] @iteration = (options[:locals] || {}).delete(:iteration) || Lite::Component::Iteration.new(1, 0) @context = context @options = default_options.deep_merge(options) yield(self) if block end
render(context, options = {}, &block)
click to toggle source
# File lib/lite/component/base.rb, line 46 def render(context, options = {}, &block) klass = new(context, options, &block) klass.render end
Public Instance Methods
add(name, options = {}, &block)
click to toggle source
# File lib/lite/component/base.rb, line 53 def add(name, options = {}, &block) components << [name, options, block] end
locals()
click to toggle source
# File lib/lite/component/base.rb, line 57 def locals @locals ||= Lite::Component::Locals.new(options[:locals]) end
Also aliased as: l
render()
click to toggle source
# File lib/lite/component/base.rb, line 63 def render return unless render? collection = options.delete(:collection) return render_collection(collection) if collection.respond_to?(:each) render_content end
render?()
click to toggle source
# File lib/lite/component/base.rb, line 72 def render? true end
render_content()
click to toggle source
# File lib/lite/component/base.rb, line 76 def render_content context.render(options) end
to_partial_path()
click to toggle source
# File lib/lite/component/base.rb, line 80 def to_partial_path "components/#{self.class.component_path}" end
yield()
click to toggle source
# File lib/lite/component/base.rb, line 84 def yield context.safe_join(yield_content) end
Private Instance Methods
default_options()
click to toggle source
# File lib/lite/component/base.rb, line 90 def default_options { partial: to_partial_path, object: self, as: :component, locals: { c: self, object: nil, iteration: iteration } } end
render_collection(collection)
click to toggle source
rubocop:disable Metrics/AbcSize
# File lib/lite/component/base.rb, line 104 def render_collection(collection) collection_size = collection.size spacer_template = options.delete(:spacer_template) results = collection.each_with_object([]).with_index do |(item, array), index| array << context.render(spacer_template) if index.positive? && spacer_template iteration = Lite::Component::Iteration.new(collection_size, index) instance = self.class.new(context, locals: { object: item, iteration: iteration }) array << instance.render end context.safe_join(results) end
yield_content()
click to toggle source
rubocop:enable Metrics/AbcSize
# File lib/lite/component/base.rb, line 119 def yield_content components.map do |name, options, block| klass = self.class.build(name) klass.render(context, options, &block) end end