Class: Octarine::PathTemplate

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

Defined Under Namespace

Modules: StringExtention

Constant Summary collapse

BadTemplateError =
Class.new(ArgumentError)

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ PathTemplate

Returns a new instance of PathTemplate.



14
15
16
# File 'lib/octarine/path_template.rb', line 14

def initialize(string)
  @parts = parse(string)
end

Instance Method Details

#+(string) ⇒ Object



81
82
83
84
85
# File 'lib/octarine/path_template.rb', line 81

def +(string)
  parts = parse(string)
  parts.shift if parts.first == [:leading_joiner, nil]
  dup.tap {|cp| cp.parts.concat(parts)}
end

#==(other) ⇒ Object



93
94
95
# File 'lib/octarine/path_template.rb', line 93

def ==(other)
  self.class === other && parts == other.parts
end

#apply(*args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/octarine/path_template.rb', line 18

def apply(*args)
  params = args.last.respond_to?(:each) ? args.pop.dup : {}
  
  @parts.find {|type, val| params[val] ||= args.pop if type == :format}
  @parts.select do |type, value|
    params[value] ||= args.shift if type == :variable && !args.empty?
  end
  @parts.find {|type, val| params[val] ||= args if type == :glob}
  
  format = nil
  path = []
  @parts.each do |type, value|
    case type
    when :variable
      path << params.delete(value)
    when :glob
      path << params.delete(value).join("/")
    when :format
      format = params.delete(value)
    else
      path << value
    end
  end
  
  out = path == [nil] ? "/" : path.join("/")
  out << ".#{format}" if format
  out << query(params) if !params.empty?
  out
end

#initialize_copy(source) ⇒ Object



97
98
99
100
# File 'lib/octarine/path_template.rb', line 97

def initialize_copy(source)
  super
  @parts = @parts.dup
end

#recognize(string) ⇒ Object Also known as: ===



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/octarine/path_template.rb', line 48

def recognize(string)
  other_parts = parse(string).each
  params = {}
  
  @parts.each do |type, value|
    other_type, other_value = (other_parts.next rescue nil)
    case type
    when :variable
      return unless other_value
      params[value] = other_value
    when :glob
      params[value] = other_value ? [other_value] : []
      while (other_parts.peek.first == :string rescue nil)
        other_type, other_value = other_parts.next
        params[value] << other_value
      end
    when :format
      return nil unless type == other_type
      params[value] = other_value.to_s
    when :string
      return nil unless type == other_type && value == other_value
    end
  end
  other_type, other_value = (other_parts.next rescue nil)
  return nil unless other_type == :query_string || other_type.nil?
  if other_value
    query = parse_query(other_value)
    params.merge!(query)
  end
  params
end

#without(string) ⇒ Object Also known as: -



87
88
89
90
# File 'lib/octarine/path_template.rb', line 87

def without(string)
  part = parse(string).first
  dup.tap {|cp| cp.parts.reject! {|pt| pt == part}}
end