Class: Hensel::Builder

Inherits:
Object
  • Object
show all
Includes:
Helpers::TagHelpers
Defined in:
lib/hensel/builder.rb,
lib/hensel/builder/item.rb,
lib/hensel/builder/node.rb

Defined Under Namespace

Classes: Item, Node

Constant Summary

Constants included from Helpers::TagHelpers

Helpers::TagHelpers::BOOLEAN_ATTRIBUTES, Helpers::TagHelpers::ESCAPE_REGEXP, Helpers::TagHelpers::ESCAPE_VALUES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::TagHelpers

#append_attribute, #content_tag, #tag

Constructor Details

#initialize(**options) ⇒ Builder

Returns a new instance of Builder.



11
12
13
14
15
# File 'lib/hensel/builder.rb', line 11

def initialize(**options)
  @items   = []
  @options = options.empty? ? Hensel.configuration.parent_attributes.dup : options
  instance_eval(&Hensel.configuration.before_load) if Hensel.configuration.before_load
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



9
10
11
# File 'lib/hensel/builder.rb', line 9

def items
  @items
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/hensel/builder.rb', line 9

def options
  @options
end

Instance Method Details

#add(*arguments) ⇒ Object

Adds an item to items



18
19
20
21
22
# File 'lib/hensel/builder.rb', line 18

def add(*arguments)
  items << (item = Item.new(*parse_arguments(arguments)))
  item.parent = self
  item
end

#item_filtersObject



57
58
59
# File 'lib/hensel/builder.rb', line 57

def item_filters
  @item_filters ||= []
end

#processed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/hensel/builder.rb', line 53

def processed?
  !!@processed
end

#remove(text = nil, &block) ⇒ Object

Removes the item from items

Examples:

without block

builder = Hensel::Builder.new
builder.add("Index", "/")
builder.remove("Index")

with block

builder = Hensel::Builder.new
builder.add("Index", "/")
builder.remove{|item| item.text == "Index" }


34
35
36
# File 'lib/hensel/builder.rb', line 34

def remove(text = nil, &block)
  block_given? ? items.delete_if(&block) : items.delete_if{|item| text == item.text }
end

#render(&block) ⇒ Object

Renders the breadcrumb html



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hensel/builder.rb', line 39

def render(&block)
  process! unless processed?

  concatenated_items = map_items do |item|
    if block_given?
      block.arity.zero? ? item.instance_eval(&block) : block.call(item)
    else
      item.render
    end
  end.join(Hensel.configuration.indentation? ? "\n" : "")

  (Hensel.configuration.parent_element, concatenated_items, options)
end