Class: Gretel::Crumb
- Inherits:
-
Object
- Object
- Gretel::Crumb
- Defined in:
- lib/gretel/crumb.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
The current view context.
-
#key ⇒ Object
readonly
Key of the breadcrumb.
Instance Method Summary collapse
-
#initialize(context, key, *args) ⇒ Crumb
constructor
Initializes a new crumb from the given
key. -
#link(text, url = nil) ⇒ Object
Sets link of the breadcrumb.
-
#links ⇒ Object
Holds all of the breadcrumb’s links as a breadcrumb can have multiple links.
-
#method_missing(method, *args, &block) ⇒ Object
Proxy to view context.
-
#parent(*args) ⇒ Object
Sets or gets the parent breadcrumb.
Constructor Details
#initialize(context, key, *args) ⇒ Crumb
Initializes a new crumb from the given key. It finds the breadcrumb created in Gretel::Crumbs.layout and renders the block using the arguments supplied in args.
5 6 7 8 9 10 11 |
# File 'lib/gretel/crumb.rb', line 5 def initialize(context, key, *args) block = Gretel::Crumbs.crumbs[key] raise ArgumentError, "Breadcrumb :#{key} not found." unless block @key = key @context = context instance_exec *args, &block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Proxy to view context
46 47 48 |
# File 'lib/gretel/crumb.rb', line 46 def method_missing(method, *args, &block) context.send(method, *args, &block) end |
Instance Attribute Details
#context ⇒ Object (readonly)
The current view context.
43 44 45 |
# File 'lib/gretel/crumb.rb', line 43 def context @context end |
#key ⇒ Object (readonly)
Key of the breadcrumb.
40 41 42 |
# File 'lib/gretel/crumb.rb', line 40 def key @key end |
Instance Method Details
#link(text, url = nil) ⇒ Object
Sets link of the breadcrumb.
14 15 16 17 18 19 |
# File 'lib/gretel/crumb.rb', line 14 def link(text, url = nil) # Transform objects to real paths. url = url_for(url) if url links << Gretel::Link.new(key, text, url) end |
#links ⇒ Object
Holds all of the breadcrumb’s links as a breadcrumb can have multiple links.
22 23 24 |
# File 'lib/gretel/crumb.rb', line 22 def links @links ||= [] end |
#parent(*args) ⇒ Object
Sets or gets the parent breadcrumb. If you supply a parent key and optional arguments, it will set the parent. If nothing is supplied, it will return the parent, if this has been set.
Example:
parent :category, category
32 33 34 35 36 37 |
# File 'lib/gretel/crumb.rb', line 32 def parent(*args) return @parent if args.empty? key = args.shift @parent = Gretel::Crumb.new(context, key, *args) end |