Class: Proscenium::UI::Breadcrumbs::ComputedElement

Inherits:
Object
  • Object
show all
Defined in:
lib/proscenium/ui/breadcrumbs/computed_element.rb

Instance Method Summary collapse

Constructor Details

#initialize(element, context) ⇒ ComputedElement

Returns a new instance of ComputedElement.



5
6
7
8
# File 'lib/proscenium/ui/breadcrumbs/computed_element.rb', line 5

def initialize(element, context)
  @element = element
  @context = context
end

Instance Method Details

#nameString

If name is a Symbol of a controller method, that method is called. If name is a Symbol of a controller instance variable, that variable is returned. If name is a Proc, it is executed in the context of the controller instance.

Returns:

  • (String)

    the content of the breadcrumb element.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/proscenium/ui/breadcrumbs/computed_element.rb', line 15

def name
  @name ||= case name = @element.name
            when Symbol
              if name.to_s.starts_with?('@')
                name = get_instance_variable(name)
                name.respond_to?(:for_breadcrumb) ? name.for_breadcrumb : name.to_s
              else
                res = @context.controller.send(name)
                res.try(:for_breadcrumb) || res.to_s
              end
            when Proc
              @context.controller.instance_exec(&name)
            else
              name.respond_to?(:for_breadcrumb) ? name.for_breadcrumb : name.to_s
            end
end

#pathString

If path is a Symbol of a controller method, that method is called. If path is a Symbol of a controller instance variable, that variable is returned. If path is an Array, each element is processed as above. If path is a Proc, it is executed in the context of the controller instance.

No matter what, the result is always passed to ‘url_for` before being returned.

Returns:

  • (String)

    the URL for the element



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/proscenium/ui/breadcrumbs/computed_element.rb', line 40

def path
  @path ||= unless @element.path.nil?
              case path = @element.path
              when Array
                path.map! { |x| x.to_s.starts_with?('@') ? get_instance_variable(x) : x }
              when Symbol
                if path.to_s.starts_with?('@')
                  path = get_instance_variable(path)
                elsif @context.controller.respond_to?(path, true)
                  path = @context.controller.send(path)
                end
              when Proc
                path = @context.controller.instance_exec(&path)
              end

              @context.url_for path
            end
end