Class: RoadForest::Graph::NavAffordanceBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/graph/nav-affordance-builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(focus, path_provider) ⇒ NavAffordanceBuilder

Returns a new instance of NavAffordanceBuilder.



6
7
8
# File 'lib/roadforest/graph/nav-affordance-builder.rb', line 6

def initialize(focus, path_provider)
  @focus, @path_provider = focus, path_provider
end

Instance Attribute Details

#focusObject (readonly)

Returns the value of attribute focus.



9
10
11
# File 'lib/roadforest/graph/nav-affordance-builder.rb', line 9

def focus
  @focus
end

#path_providerObject (readonly)

Returns the value of attribute path_provider.



9
10
11
# File 'lib/roadforest/graph/nav-affordance-builder.rb', line 9

def path_provider
  @path_provider
end

Instance Method Details

#iri_template(route, params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/roadforest/graph/nav-affordance-builder.rb', line 23

def iri_template(route, params)
  klass = route.interface_class
  return if klass.nil?

  variables = klass.path_params

  params ||= {}
  params = params.dup

  variables -= params.keys

  path_spec = route.resolve_path_spec(params)

  path = path_provider.services.canonical_host.to_s.sub(%r{/$}, '') +
    path_spec.map do |segment|
    case segment
    when Symbol
      variables.delete(segment)
      "{/#{segment}}"
    when '*'
      "{/extra*}"
    else
      "/" + segment.to_s
    end
    end.join("")

    unless params.empty?
      path += "?" + params.map do |key, value|
        [key, value].join("=")
      end.join("&")
    end

    unless variables.empty?
      path += "{?#{variables.join(",")}}"
    end
end

#to(name, params = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/roadforest/graph/nav-affordance-builder.rb', line 11

def to(name, params=nil)
  route = path_provider.route_for_name(name)

  pattern = iri_template(route, params)

  node = ::RDF::Node.new
  tmpl = ::RDF::Node.new
  focus << [ node, ::RDF.type, Af.Navigate ]
  focus << [ node, Af.target, tmpl ]
  focus << [ tmpl, Af.pattern, pattern ]
end