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 Layout/LineLength

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
20
21
22
# 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

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 (readonly) Also known as: i

Returns the value of attribute iteration.



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

def iteration
  @iteration
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



32
33
34
35
36
# 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_nameObject



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

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

.component_pathObject



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

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

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



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

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

Instance Method Details

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



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

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

#localsObject Also known as: l



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

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

#renderObject



63
64
65
66
67
68
69
70
# 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?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/lite/component/base.rb', line 72

def render?
  true
end

#render_contentObject



76
77
78
# File 'lib/lite/component/base.rb', line 76

def render_content
  context.render(options)
end

#to_partial_pathObject



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

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

#yieldObject



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

def yield
  context.safe_join(yield_content)
end