Class: Wayfinding::Destination

Inherits:
Object
  • Object
show all
Defined in:
lib/wayfinding/destination.rb

Overview

A named, engine-scoped URL resolver with arbitrary application metadata. Metadata is stored without interpretation and read through #[]. Any field can be resolved lazily through #value_for.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, engine: nil, kind: nil, helper: nil, resolver: nil, **attributes) ⇒ Destination

rubocop:disable Metrics/ParameterLists



11
12
13
14
15
16
17
18
19
# File 'lib/wayfinding/destination.rb', line 11

def initialize(name:, engine: nil, kind: nil, helper: nil, resolver: nil, **attributes)
  @name = name.to_sym
  @engine = engine
  @kind = kind&.to_sym
  @helper = helper&.to_sym
  @resolver = resolver
  @attributes = attributes.freeze
  freeze
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/wayfinding/destination.rb', line 8

def attributes
  @attributes
end

#kindObject (readonly)

Returns the value of attribute kind.



8
9
10
# File 'lib/wayfinding/destination.rb', line 8

def kind
  @kind
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/wayfinding/destination.rb', line 8

def name
  @name
end

Instance Method Details

#[](key) ⇒ Object



26
# File 'lib/wayfinding/destination.rb', line 26

def [](key) = attributes[key.to_sym]

#actionObject

rubocop:enable Metrics/ParameterLists



22
# File 'lib/wayfinding/destination.rb', line 22

def action = self[:action]

#engineObject



46
47
48
# File 'lib/wayfinding/destination.rb', line 46

def engine
  @engine.is_a?(Proc) ? @engine.call : @engine
end

#path(**params) ⇒ Object



30
31
32
33
34
# File 'lib/wayfinding/destination.rb', line 30

def path(*, **params)
  return resolver_result(:path, *, **params) unless @helper

  url_helpers.public_send(@helper, *, **params)
end

#subjectObject



24
# File 'lib/wayfinding/destination.rb', line 24

def subject = value_for(:subject)

#url(**params) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/wayfinding/destination.rb', line 36

def url(*, **params)
  return resolver_result(:url, *, **params) unless @helper

  unless @helper.to_s.end_with?("_path")
    raise Error, "#{name.inspect} declares helper #{@helper.inspect}, which does not end in `_path`"
  end

  url_helpers.public_send(@helper.to_s.sub(/_path\z/, "_url"), *, **params)
end

#value_for(key) ⇒ Object



28
# File 'lib/wayfinding/destination.rb', line 28

def value_for(key) = resolve(self[key])