Module: Well::Component Abstract

Includes:
ActionView::Helpers
Included in:
Block, Element
Defined in:
lib/well/component.rb

Overview

This module is abstract.

Included by both Block and Element, this is the module which contains the shared fuctionality of all BEM components.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



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

def identifier
  @identifier
end

Instance Method Details

#compiled_identifierObject

This method is abstract.

Must be implemented by composing classes.

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/well/component.rb', line 50

def compiled_identifier
  raise NotImplementedError
end

#element(*args, &content) ⇒ Element

DSL method used to add a nested element in the context of self. Both block and element components may contain further nested elements.

Returns:



64
65
66
# File 'lib/well/component.rb', line 64

def element(*args, &content)
  Element.new(base, *args).evaluate(&content)
end

#evaluate { ... } ⇒ String

Used by the DSL to build nested BEM components.

Yields:

  • Content block is evaluated in the context of self.

Returns:

  • (String)


43
44
45
46
# File 'lib/well/component.rb', line 43

def evaluate(&content)
  result = instance_eval(&content) if block_given?
  (@tag_name, result, class: output_classes)
end

#modified_identifierString

Adds BEM modifier if provided.

Returns:

  • (String)


56
57
58
59
# File 'lib/well/component.rb', line 56

def modified_identifier
  return unless modifier
  "#{compiled_identifier}#{Well.config.modifier_separator}#{modifier}"
end

#modifierString

Extracts BEM modifier from options.

Returns:

  • (String)


22
23
24
# File 'lib/well/component.rb', line 22

def modifier
  @modifier ||= @opts[:modifier]
end

#other_classesArray<String>

Extracts other non-BEM CSS classes from options.

Returns:

  • (Array<String>)


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

def other_classes
  @other_classes ||= @opts.fetch(:class) { [] }
end

#output_bufferActionView::OutputBuffer

Must be defined since many methods from ActionView::Helpers. expect to be able to write to an ActionView::OutputBuffer.

Returns:

  • (ActionView::OutputBuffer)


16
17
18
# File 'lib/well/component.rb', line 16

def output_buffer
  @output_buffer ||= ActionView::OutputBuffer.new
end

#output_classesArray<String>

Combines all CSS classes for output.

Returns:

  • (Array<String>)


34
35
36
37
38
# File 'lib/well/component.rb', line 34

def output_classes
  @output_classes ||= (
    (other_classes << compiled_identifier << modified_identifier).compact
  )
end