Class: BreadcrumbTrail::Breadcrumb
- Inherits:
-
Object
- Object
- BreadcrumbTrail::Breadcrumb
- Defined in:
- lib/breadcrumb_trail/breadcrumb.rb
Overview
A single representation of a breadcrumb.
Instance Attribute Summary collapse
-
#name ⇒ String, ...
readonly
The name of the breadcrumb.
-
#options ⇒ Hash
readonly
Options for the breadcrumb.
-
#path ⇒ String, ...
readonly
The path the breadcrumb represents.
Instance Method Summary collapse
-
#computed(context) ⇒ Breadcrumb
Creates a version of the breadcrumb that has a computed name and path.
-
#computed_name(context) ⇒ String
Computes the name of the breadcrumb under the given context.
-
#computed_path(context) ⇒ String, Hash
Computes the path of the breadcrumb under the given context.
-
#initialize(name: nil, path: nil, **options, &block) ⇒ Breadcrumb
constructor
Initialize the breadcrumb.
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.
35 36 37 38 39 |
# File 'lib/breadcrumb_trail/breadcrumb.rb', line 35 def initialize(name: nil, path: nil, **, &block) @name = name @path = path || block = end |
Instance Attribute Details
#name ⇒ String, ... (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.
11 12 13 |
# File 'lib/breadcrumb_trail/breadcrumb.rb', line 11 def name @name end |
#options ⇒ Hash (readonly)
Options for the breadcrumb. Normally, these are HTML attributes that are used for the link tag.
23 24 25 |
# File 'lib/breadcrumb_trail/breadcrumb.rb', line 23 def end |
#path ⇒ String, ... (readonly)
The path the breadcrumb represents. Normally, this is where the breadcrumb should take the user when clicked.
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.
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), **) end |
#computed_name(context) ⇒ String
Computes the name of the breadcrumb under the given context.
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 I18n.translate(@name) 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.
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 |