uri_template 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ # 0.1.1 - 4.11.2011
2
+ + added a bunch of useful helper methods
3
+
1
4
  # 0.1.0 - 2.11.2011
2
5
  - Removed Sections. They made too many headaches.
3
6
  + Made draft7 template concatenateable. This should replace sections.
data/lib/uri_template.rb CHANGED
@@ -64,6 +64,78 @@ module URITemplate
64
64
  return klass.new(*rest)
65
65
  end
66
66
 
67
+ # Tries to convert the given argument into an {URITemplate}.
68
+ # Returns nil if this fails.
69
+ #
70
+ # @return [nil|{URITemplate}]
71
+ def self.try_convert(x)
72
+ if x.kind_of? URITemplate
73
+ return x
74
+ elsif x.kind_of? String
75
+ return URITemplate.new(x)
76
+ else
77
+ return nil
78
+ end
79
+ end
80
+
81
+ # Same as {.try_convert} but raises an ArgumentError when the given argument could not be converted.
82
+ #
83
+ # @raise ArgumentError if the argument is unconvertable
84
+ # @return {URITemplate}
85
+ def self.convert(x)
86
+ o = self.try_convert(x)
87
+ if o.nil?
88
+ raise ArgumentError, "Expected to receive something that can be converted to an URITemplate, but got #{x.inspect}"
89
+ end
90
+ return o
91
+ end
92
+
93
+ # Tries to coerce two URITemplates into a common representation.
94
+ # Returns an array with two {URITemplate}s and two booleans indicating which of the two were converted or raises an {ArgumentError}.
95
+ #
96
+ # @example
97
+ # URITemplate.coerce( URITemplate.new(:draft7,'{x}'), '{y}' ) #=> [URITemplate.new(:draft7,'{x}'), URITemplate.new(:draft7,'{y}'), false, true]
98
+ def self.coerce(a,b)
99
+ if a.kind_of? URITemplate
100
+ if a.class == b.class
101
+ return [a,b,false,false]
102
+ end
103
+ b_as_a = a.class.try_convert(b)
104
+ if b_as_a
105
+ return [a,b_as_a,false,true]
106
+ end
107
+ end
108
+ if b.kind_of? URITemplate
109
+ a_as_b = b.class.try_convert(a)
110
+ if a_as_b
111
+ return [a_as_b, b, true, false]
112
+ end
113
+ end
114
+ bc = self.try_convert(b)
115
+ if bc.kind_of? URITemplate
116
+ a_as_b = bc.class.try_convert(a)
117
+ if a_as_b
118
+ return [a_as_b, bc, true, true]
119
+ end
120
+ end
121
+ raise ArgumentError, "Expected at least on URITemplate, but got #{a.inspect} and #{b.inspect}" unless a.kind_of? URITemplate or b.kind_of? URITemplate
122
+ raise ArgumentError, "Cannot coerce #{a.inspect} and #{b.inspect} into a common representation."
123
+ end
124
+
125
+ # Applies a method to a URITemplate with another URITemplate as argument.
126
+ # This is a useful shorthand since both URITemplates are automatically coerced.
127
+ #
128
+ # @example
129
+ # tpl = URITemplate.new('foo')
130
+ # URITemplate.apply( tpl, :/, 'bar' ).pattern #=> 'foo/bar'
131
+ # URITemplate.apply( 'baz', :/, tpl ).pattern #=> 'baz/foo'
132
+ # URITemplate.apply( 'bla', :/, 'blub' ).pattern #=> 'bla/blub'
133
+ #
134
+ def self.apply(a, method, b, *args)
135
+ a,b,_,_ = self.coerce(a,b)
136
+ a.send(method,b,*args)
137
+ end
138
+
67
139
  # A base class for all errors which will be raised upon invalid syntax.
68
140
  module Invalid
69
141
  end
@@ -749,9 +749,12 @@ __REGEXP__
749
749
  alias to_s pattern
750
750
 
751
751
  # Compares two template patterns.
752
- def ==(tpl)
753
- return false if self.class != tpl.class
754
- return self.pattern == tpl.pattern
752
+ def ==(o)
753
+ this, other, this_converted, other_converted = URITemplate.coerce( self, o )
754
+ if this_converted
755
+ return this == other
756
+ end
757
+ return this.pattern == other.pattern
755
758
  end
756
759
 
757
760
  # @method ===(uri)
@@ -811,7 +814,10 @@ __REGEXP__
811
814
  # (tpl / 'a' / 'b' ).pattern #=> '/xy/a/b'
812
815
  #
813
816
  def /(o)
814
- other = self.class.convert(o)
817
+ this, other, this_converted, other_converted = URITemplate.coerce( self, o )
818
+ if this_converted
819
+ return this / other
820
+ end
815
821
 
816
822
  if other.absolute?
817
823
  raise ArgumentError, "Expected to receive a relative template but got an absoulte one: #{other.inspect}. If you think this is a bug, please report it."
data/uri_template.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'uri_template'
3
- s.version = '0.1.0'
4
- s.date = '2011-11-02'
3
+ s.version = '0.1.1'
4
+ s.date = '2011-11-04'
5
5
  s.authors = ["HannesG"]
6
6
  s.email = %q{hannes.georg@googlemail.com}
7
7
  s.summary = 'A templating system for URIs.'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uri_template
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-02 00:00:00.000000000Z
12
+ date: 2011-11-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &14484700 !ruby/object:Gem::Requirement
16
+ requirement: &17225100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *14484700
24
+ version_requirements: *17225100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: yard
27
- requirement: &14482080 !ruby/object:Gem::Requirement
27
+ requirement: &17224520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *14482080
35
+ version_requirements: *17224520
36
36
  description: ! 'A templating system for URIs, which implements http://tools.ietf.org/html/draft-gregorio-uritemplate-07
37
37
  . An implementation of an older version of that spec is known as addressable. This
38
38
  gem however is intended to be extended when newer specs evolve. For now only draft
@@ -43,8 +43,6 @@ extensions: []
43
43
  extra_rdoc_files: []
44
44
  files:
45
45
  - lib/uri_template.rb
46
- - lib/uri_template/colon.rb
47
- - lib/uri_template/draft2.rb
48
46
  - lib/uri_template/draft7.rb
49
47
  - lib/uri_template/utils.rb
50
48
  - uri_template.gemspec
File without changes
File without changes