Class: Plato::PathTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/plato/path_template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ PathTemplate

Returns a new instance of PathTemplate.



5
6
7
8
# File 'lib/plato/path_template.rb', line 5

def initialize(string)
  @template = string
  compile!
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



3
4
5
# File 'lib/plato/path_template.rb', line 3

def keys
  @keys
end

#templateObject (readonly)

Returns the value of attribute template.



3
4
5
# File 'lib/plato/path_template.rb', line 3

def template
  @template
end

Instance Method Details

#materialize(attributes) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/plato/path_template.rb', line 10

def materialize(attributes)
  values = attributes.values_at(*keys).compact
  unless values.length == keys.length
    raise ArgumentError, "missing required values for path materialization (#{keys.join(', ')})"
  end

  @materializer.call(values)
end

#parse(path) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/plato/path_template.rb', line 19

def parse(path)
  match = path.match(@parser)
  return nil unless match

  match = match.to_a
  match.shift

  Hash[*keys.zip(match).flatten]
end