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 = {}) {|_self| ... } ⇒ Base

rubocop:disable Lint/UnusedMethodArgument

Yields:

  • (_self)

Yield Parameters:



15
16
17
18
19
20
21
22
# File 'lib/lite/component/base.rb', line 15

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

  @components = []

  yield(self) if block_given?
end

Instance Attribute Details

#componentsObject

Returns the value of attribute components.



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

def components
  @components
end

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

Returns the value of attribute context.



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

def context
  @context
end

#iterationObject Also known as: i



56
57
58
# File 'lib/lite/component/base.rb', line 56

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

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.build(name) ⇒ Object



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

def build(name)
  return name if name.respond_to?(:component_path)

  "#{name}_component".classify.constantize
end

.component_nameObject



37
38
39
# File 'lib/lite/component/base.rb', line 37

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

.component_pathObject



41
42
43
# File 'lib/lite/component/base.rb', line 41

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

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



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

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

Instance Method Details

#add(name, options = {}, &block) ⇒ Object



52
53
54
# File 'lib/lite/component/base.rb', line 52

def add(name, options = {}, &block)
  components << [name, options, block]
end

#localsObject Also known as: l



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

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

#renderObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lite/component/base.rb', line 68

def render
  return unless 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?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/lite/component/base.rb', line 81

def render?
  true
end

#render_contentObject



85
86
87
# File 'lib/lite/component/base.rb', line 85

def render_content
  context.render(options)
end

#to_partial_pathObject



89
90
91
# File 'lib/lite/component/base.rb', line 89

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

#yieldObject



93
94
95
# File 'lib/lite/component/base.rb', line 93

def yield
  context.safe_join(yield_content)
end