Class: BreadcrumbTrail::Breadcrumb

Inherits:
Object
  • Object
show all
Defined in:
lib/breadcrumb_trail/breadcrumb.rb

Overview

A single representation of a breadcrumb.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, path: nil, **options, &block) ⇒ Breadcrumb

Initialize the breadcrumb. If a block is given, and a path is not, then the path is set to be the block.

Parameters:

  • name (String, Symbol, Proc, nil) (defaults to: nil)

    The name of the breadcrumb. See #name.

  • path (String, Symbol, Proc, Hash) (defaults to: nil)

    The path of the breadcrumb. See #path.

  • options (Hash)

    Options that are used as HTML attributes. See #options.


35
36
37
38
39
# File 'lib/breadcrumb_trail/breadcrumb.rb', line 35

def initialize(name: nil, path: nil, **options, &block)
  @name    = name
  @path    = path || block
  @options = options
end

Instance Attribute Details

#nameString, ... (readonly)

The name of the breadcrumb. Normally, this represents the text that is displayed in place of the link to give meaning to the breadcrumb.

Returns:

  • (String, Symbol, Proc, nil)

11
12
13
# File 'lib/breadcrumb_trail/breadcrumb.rb', line 11

def name
  @name
end

#optionsHash (readonly)

Options for the breadcrumb. Normally, these are HTML attributes that are used for the link tag.

Returns:

  • (Hash)

23
24
25
# File 'lib/breadcrumb_trail/breadcrumb.rb', line 23

def options
  @options
end

#pathString, ... (readonly)

The path the breadcrumb represents. Normally, this is where the breadcrumb should take the user when clicked.

Returns:

  • (String, Symbol, Proc, Hash)

17
18
19
# File 'lib/breadcrumb_trail/breadcrumb.rb', line 17

def path
  @path
end

Instance Method Details

#computed(context) ⇒ Breadcrumb

Creates a version of the breadcrumb that has a computed name and path. This is used, for example, in a builder that exposes a breadcrumb to application code.

Parameters:

  • context (ActionView::Base)

    The context to compute the elements under.

Returns:

See Also:


50
51
52
53
54
# File 'lib/breadcrumb_trail/breadcrumb.rb', line 50

def computed(context)
  self.class.new(name: computed_name(context),
                 path: computed_path(context),
                 **@options)
end

#computed_name(context) ⇒ String

Computes the name of the breadcrumb under the given context.

Returns:

  • (String)

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/breadcrumb_trail/breadcrumb.rb', line 77

def computed_name(context)
  @_name ||= case @name
  when String
    @name
  when Symbol
    context.public_send(@name) # todo
  when Proc
    context.instance_exec(&@name)
  when nil
    computed_path(context)
  else
    raise ArgumentError,
      "Expected one of String, Symbol, or Proc, " \
      "got #{@name.class}"
  end
end

#computed_path(context) ⇒ String, Hash

Computes the path of the breadcrumb under the given context.

Returns:

  • (String, Hash)

59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/breadcrumb_trail/breadcrumb.rb', line 59

def computed_path(context)
  @_path ||= case @path
  when String, Hash
    @path
  when Symbol
    context.public_send(@path) # todo
  when Proc
    context.instance_exec(&@path)
  else
    raise ArgumentError,
      "Expected one of String, Symbol, or Proc, " \
      "got #{@path.class}"
  end
end