Class: Lite::Component::Base

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers
Defined in:
lib/lite/component/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, options = {}) ⇒ Base

Returns a new instance of Base.



13
14
15
16
# File 'lib/lite/component/base.rb', line 13

def initialize(context, options = {})
  @context = context
  @options = default_options.deep_merge(options)
end

Instance Attribute Details

#contextObject (readonly) Also known as: helpers, c

Returns the value of attribute context.



10
11
12
# File 'lib/lite/component/base.rb', line 10

def context
  @context
end

#iterationObject Also known as: i



39
40
41
# File 'lib/lite/component/base.rb', line 39

def iteration
  @iteration ||= Lite::Component::Iteration.new(1, 0)
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/lite/component/base.rb', line 10

def options
  @options
end

Class Method Details

.component_nameObject



24
25
26
# File 'lib/lite/component/base.rb', line 24

def component_name
  component_path.split('/').last
end

.component_pathObject



28
29
30
# File 'lib/lite/component/base.rb', line 28

def component_path
  name.underscore.sub('_component', '')
end

.render(context, options = {}) ⇒ Object



32
33
34
35
# File 'lib/lite/component/base.rb', line 32

def render(context, options = {})
  klass = new(context, options)
  klass.render
end

Instance Method Details

#localsObject Also known as: l



45
46
47
# File 'lib/lite/component/base.rb', line 45

def locals
  @locals ||= Lite::Component::Locals.new(options[:locals])
end

#renderObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/lite/component/base.rb', line 51

def render
  collection = options.delete(:collection)
  return render_content if collection.nil? || !collection.respond_to?(:each)

  Lite::Component::Collection.render(
    collection,
    component: self,
    spacer_template: options.delete(:spacer_template)
  )
end

#render_contentObject



62
63
64
# File 'lib/lite/component/base.rb', line 62

def render_content
  context.render(options)
end

#to_partial_pathObject



66
67
68
# File 'lib/lite/component/base.rb', line 66

def to_partial_path
  "components/#{self.class.component_path}"
end