versionomy 0.4.1 → 0.4.2

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.
@@ -1,15 +1,15 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # Versionomy exceptions
4
- #
4
+ #
5
5
  # -----------------------------------------------------------------------------
6
6
  # Copyright 2008-2009 Daniel Azuma
7
- #
7
+ #
8
8
  # All rights reserved.
9
- #
9
+ #
10
10
  # Redistribution and use in source and binary forms, with or without
11
11
  # modification, are permitted provided that the following conditions are met:
12
- #
12
+ #
13
13
  # * Redistributions of source code must retain the above copyright notice,
14
14
  # this list of conditions and the following disclaimer.
15
15
  # * Redistributions in binary form must reproduce the above copyright notice,
@@ -18,7 +18,7 @@
18
18
  # * Neither the name of the copyright holder, nor the names of any other
19
19
  # contributors to this software, may be used to endorse or promote products
20
20
  # derived from this software without specific prior written permission.
21
- #
21
+ #
22
22
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
23
  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
24
  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -35,126 +35,126 @@
35
35
 
36
36
 
37
37
  module Versionomy
38
-
39
-
38
+
39
+
40
40
  # This is a namespace for errors that can be thrown by Versionomy.
41
-
41
+
42
42
  module Errors
43
-
44
-
43
+
44
+
45
45
  # Base class for all Versionomy exceptions
46
-
46
+
47
47
  class VersionomyError < ::RuntimeError
48
48
  end
49
-
50
-
49
+
50
+
51
51
  # This exception is raised if parsing failed.
52
-
52
+
53
53
  class ParseError < VersionomyError
54
54
  end
55
-
56
-
55
+
56
+
57
57
  # This exception is raised if unparsing failed.
58
-
58
+
59
59
  class UnparseError < VersionomyError
60
60
  end
61
-
62
-
61
+
62
+
63
63
  # This exception is raised if you try to set a value that is not
64
64
  # allowed by the schema.
65
-
65
+
66
66
  class IllegalValueError < VersionomyError
67
67
  end
68
-
69
-
68
+
69
+
70
70
  # This exception is raised if you try to perform a comparison
71
71
  # between incompatible schemas.
72
-
72
+
73
73
  class SchemaMismatchError < VersionomyError
74
74
  end
75
-
76
-
75
+
76
+
77
77
  # Base class for all Versionomy schema creation exceptions
78
-
78
+
79
79
  class SchemaCreationError < VersionomyError
80
80
  end
81
-
82
-
81
+
82
+
83
83
  # This exception is raised during schema creation if you try to add
84
84
  # the same symbol twice to the same symbolic field.
85
-
85
+
86
86
  class SymbolRedefinedError < SchemaCreationError
87
87
  end
88
-
89
-
88
+
89
+
90
90
  # This exception is raised during schema creation if you try to
91
91
  # create two fields covering overlapping ranges.
92
-
92
+
93
93
  class RangeOverlapError < SchemaCreationError
94
94
  end
95
-
96
-
95
+
96
+
97
97
  # This exception is raised during schema creation if the range
98
98
  # specification cannot be interpreted.
99
-
99
+
100
100
  class RangeSpecificationError < SchemaCreationError
101
101
  end
102
-
103
-
102
+
103
+
104
104
  # This exception is raised during schema creation if you try to
105
105
  # add a symbol to a non-symbolic schema.
106
-
106
+
107
107
  class TypeMismatchError < SchemaCreationError
108
108
  end
109
-
110
-
109
+
110
+
111
111
  # This exception is raised during schema creation if you try to
112
112
  # create a circular graph.
113
-
113
+
114
114
  class CircularDescendantError < SchemaCreationError
115
115
  end
116
-
117
-
116
+
117
+
118
118
  # Base class for all Versionomy format creation exceptions.
119
-
119
+
120
120
  class FormatCreationError < VersionomyError
121
121
  end
122
-
123
-
122
+
123
+
124
124
  # This exception is raised if you try to register a format
125
125
  # with a name that has already been used.
126
-
126
+
127
127
  class FormatRedefinedError < VersionomyError
128
128
  end
129
-
130
-
129
+
130
+
131
131
  # Raised by the Format registry if you try to retrieve a format with
132
132
  # an unrecognized name in strict mode.
133
-
133
+
134
134
  class UnknownFormatError < VersionomyError
135
135
  end
136
-
137
-
136
+
137
+
138
138
  # Raised when a conversion fails.
139
-
139
+
140
140
  class ConversionError < VersionomyError
141
141
  end
142
-
143
-
142
+
143
+
144
144
  # Raised when a conversion fails because no conversion implementation
145
145
  # was found.
146
-
146
+
147
147
  class UnknownConversionError < ConversionError
148
148
  end
149
-
150
-
149
+
150
+
151
151
  # Raised when you try to register a conversion when one already
152
152
  # exists for its schemas.
153
-
153
+
154
154
  class ConversionRedefinedError < VersionomyError
155
155
  end
156
-
157
-
156
+
157
+
158
158
  end
159
-
159
+
160
160
  end
@@ -1,15 +1,15 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # Versionomy format namespace
4
- #
4
+ #
5
5
  # -----------------------------------------------------------------------------
6
6
  # Copyright 2008-2009 Daniel Azuma
7
- #
7
+ #
8
8
  # All rights reserved.
9
- #
9
+ #
10
10
  # Redistribution and use in source and binary forms, with or without
11
11
  # modification, are permitted provided that the following conditions are met:
12
- #
12
+ #
13
13
  # * Redistributions of source code must retain the above copyright notice,
14
14
  # this list of conditions and the following disclaimer.
15
15
  # * Redistributions in binary form must reproduce the above copyright notice,
@@ -18,7 +18,7 @@
18
18
  # * Neither the name of the copyright holder, nor the names of any other
19
19
  # contributors to this software, may be used to endorse or promote products
20
20
  # derived from this software without specific prior written permission.
21
- #
21
+ #
22
22
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
23
  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
24
  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -39,30 +39,30 @@ require 'monitor'
39
39
 
40
40
 
41
41
  module Versionomy
42
-
43
-
42
+
43
+
44
44
  # === Version number format.
45
- #
45
+ #
46
46
  # A format controls the parsing and unparsing of a version number.
47
47
  # Any time a version number is parsed from a string, a format is provided
48
48
  # to parse it. Similarly, every version number value references a format
49
49
  # that is used to unparse it back into a string.
50
- #
50
+ #
51
51
  # A format is always tied to a particular schema and knows how to parse
52
52
  # only that schema's version numbers.
53
- #
53
+ #
54
54
  # Under many circumstances, you should use the standard format, which
55
55
  # can be retrieved by calling Versionomy::Format#standard. This format
56
56
  # understands most common version numbers, including prerelease
57
57
  # (e.g. alpha, beta, release candidate, etc.) forms and patchlevels.
58
- #
58
+ #
59
59
  # You may also create your own formats, either by implementing the
60
60
  # format contract (see Versionomy::Format::Base), or by using the
61
61
  # Versionomy::Format::Delimiter tool, which can be used to construct
62
62
  # parsers for many version number formats.
63
- #
63
+ #
64
64
  # === Format registry
65
- #
65
+ #
66
66
  # Formats may be registered with Versionomy and given a name using the
67
67
  # methods of this module. This allows version numbers to be serialized
68
68
  # with their format. When a version number is serialized, its format
@@ -70,14 +70,14 @@ module Versionomy
70
70
  # representation. When the version number is reconstructed, its format
71
71
  # is looked up by name so versionomy can determine how to parse the
72
72
  # string.
73
- #
73
+ #
74
74
  # Format names are strings that may include letters, numerals, dashes,
75
75
  # underscores, and periods. By convention, periods are used as namespace
76
76
  # delimiters. Format names without a namespace (that is, with no periods)
77
77
  # are considered reserved for standard versionomy formats. If you define
78
78
  # your own format, you should use a name that includes a namespace (e.g.
79
79
  # "mycompany.LibraryVersion") to reduce the chance of name collisions.
80
- #
80
+ #
81
81
  # You may register formats directly using the register method, or set it
82
82
  # up to be autoloaded on demand. When a format is requested, if it has
83
83
  # not been registered explicitly, Versionomy looks for a format definition
@@ -91,27 +91,27 @@ module Versionomy
91
91
  # that defines the format and registers it using the correct name. See
92
92
  # the files in the "lib/versionomy/format_definitions/" directory for
93
93
  # examples.
94
-
94
+
95
95
  module Format
96
-
96
+
97
97
  @mutex = ::Mutex.new
98
98
  @load_mutex = ::Monitor.new
99
99
  @directories = [::File.expand_path(::File.dirname(__FILE__)+'/format_definitions')]
100
100
  @names_to_formats = {}
101
101
  @formats_to_names = {}
102
-
102
+
103
103
  class << self
104
-
105
-
104
+
105
+
106
106
  # Add a directory to the format path.
107
- #
107
+ #
108
108
  # The format path is an array of directory paths. These directories
109
109
  # are searched for format definitions if a format name that has not
110
110
  # been registered is requested.
111
- #
111
+ #
112
112
  # If high_priority_ is set to true, the directory is added to the
113
113
  # front of the lookup path; otherwise it is added to the back.
114
-
114
+
115
115
  def add_directory(path_, high_priority_=false)
116
116
  path_ = ::File.expand_path(path_)
117
117
  @mutex.synchronize do
@@ -124,18 +124,18 @@ module Versionomy
124
124
  end
125
125
  end
126
126
  end
127
-
128
-
127
+
128
+
129
129
  # Get the format with the given name.
130
- #
130
+ #
131
131
  # If the given name has not been defined, attempts to autoload it from
132
132
  # a format definition file. See the description of the Format module
133
133
  # for details on this procedure.
134
- #
134
+ #
135
135
  # If the given name still cannot be resolved, and strict is set to
136
136
  # true, raises Versionomy::Errors::UnknownFormatError. If strict is
137
137
  # set to false, returns nil if the given name cannot be resolved.
138
-
138
+
139
139
  def get(name_, strict_=false)
140
140
  name_ = _check_name(name_)
141
141
  format_ = @mutex.synchronize{ @names_to_formats[name_] }
@@ -156,25 +156,25 @@ module Versionomy
156
156
  end
157
157
  format_
158
158
  end
159
-
160
-
159
+
160
+
161
161
  # Determines whether a format with the given name has been registered
162
162
  # explicitly. Does not attempt to autoload the format.
163
-
163
+
164
164
  def registered?(name_)
165
165
  name_ = _check_name(name_)
166
166
  @mutex.synchronize{ @names_to_formats.include?(name_) }
167
167
  end
168
-
169
-
168
+
169
+
170
170
  # Register the given format under the given name.
171
- #
171
+ #
172
172
  # Valid names may contain only letters, digits, underscores, dashes,
173
173
  # and periods.
174
- #
174
+ #
175
175
  # Raises Versionomy::Errors::FormatRedefinedError if the name has
176
176
  # already been defined.
177
-
177
+
178
178
  def register(name_, format_, silent_=false)
179
179
  name_ = _check_name(name_)
180
180
  @mutex.synchronize do
@@ -188,15 +188,15 @@ module Versionomy
188
188
  end
189
189
  end
190
190
  end
191
-
192
-
191
+
192
+
193
193
  # Get the canonical name for the given format, as a string.
194
194
  # This is the first name the format was registered under.
195
- #
195
+ #
196
196
  # If the given format was never registered, and strict is set to true,
197
197
  # raises Versionomy::Errors::UnknownFormatError. If strict is set to
198
198
  # false, returns nil if the given format was never registered.
199
-
199
+
200
200
  def canonical_name_for(format_, strict_=false)
201
201
  name_ = @mutex.synchronize{ @formats_to_names[format_.object_id] }
202
202
  if name_.nil? && strict_
@@ -204,10 +204,10 @@ module Versionomy
204
204
  end
205
205
  name_
206
206
  end
207
-
208
-
207
+
208
+
209
209
  private
210
-
210
+
211
211
  def _check_name(name_) # :nodoc:
212
212
  name_ = name_.to_s
213
213
  unless name_ =~ /\A[\w.-]+\z/
@@ -215,17 +215,17 @@ module Versionomy
215
215
  end
216
216
  name_
217
217
  end
218
-
219
-
218
+
219
+
220
220
  end
221
-
221
+
222
222
  end
223
-
224
-
223
+
224
+
225
225
  # Versionomy::Formats is an alias for Versionomy::Format, for backward
226
226
  # compatibility with version 0.1.0 code. It is deprecated; use
227
227
  # Versionomy::Format instead.
228
228
  Formats = Format
229
-
230
-
229
+
230
+
231
231
  end
@@ -1,15 +1,15 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # Versionomy format base class
4
- #
4
+ #
5
5
  # -----------------------------------------------------------------------------
6
6
  # Copyright 2008-2009 Daniel Azuma
7
- #
7
+ #
8
8
  # All rights reserved.
9
- #
9
+ #
10
10
  # Redistribution and use in source and binary forms, with or without
11
11
  # modification, are permitted provided that the following conditions are met:
12
- #
12
+ #
13
13
  # * Redistributions of source code must retain the above copyright notice,
14
14
  # this list of conditions and the following disclaimer.
15
15
  # * Redistributions in binary form must reproduce the above copyright notice,
@@ -18,7 +18,7 @@
18
18
  # * Neither the name of the copyright holder, nor the names of any other
19
19
  # contributors to this software, may be used to endorse or promote products
20
20
  # derived from this software without specific prior written permission.
21
- #
21
+ #
22
22
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
23
  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
24
  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -35,74 +35,74 @@
35
35
 
36
36
 
37
37
  module Versionomy
38
-
39
-
38
+
39
+
40
40
  module Format
41
-
42
-
41
+
42
+
43
43
  # The base format.
44
- #
44
+ #
45
45
  # This format doesn't actually do anything useful. It causes all strings
46
46
  # to parse to the schema's default value, and unparses all values to the
47
47
  # empty string. Instead, the purpose here is to define the API for a
48
48
  # format.
49
- #
49
+ #
50
50
  # All formats must define the methods +schema+, +parse+, and +unparse+.
51
51
  # It is also recommended that formats define the <tt>===</tt> method,
52
52
  # though this is not strictly required. Finally, formats may optionally
53
53
  # implement <tt>uparse_for_serialize</tt>.
54
- #
54
+ #
55
55
  # Formats need not extend this base class, as long as they duck-type
56
56
  # these methods.
57
-
57
+
58
58
  class Base
59
-
60
-
59
+
60
+
61
61
  # Create an instance of this base format, with the given schema.
62
-
62
+
63
63
  def initialize(schema_)
64
64
  @_schema = schema_
65
65
  end
66
-
67
-
66
+
67
+
68
68
  def inspect # :nodoc:
69
69
  "#<#{self.class}:0x#{object_id.to_s(16)} schema=#{@_schema.inspect}>"
70
70
  end
71
-
71
+
72
72
  def to_s # :nodoc:
73
73
  inspect
74
74
  end
75
-
76
-
75
+
76
+
77
77
  # Returns the schema understood by this format.
78
-
78
+
79
79
  def schema
80
80
  @_schema
81
81
  end
82
-
83
-
82
+
83
+
84
84
  # Parse the given string and return a value.
85
- #
85
+ #
86
86
  # The optional parameter hash can be used to pass parameters to the
87
87
  # parser to affect its behavior. The exact parameters supported are
88
88
  # defined by the format.
89
-
89
+
90
90
  def parse(string_, params_=nil)
91
91
  Value.new([], self)
92
92
  end
93
-
94
-
93
+
94
+
95
95
  # Unparse the given value and return a string.
96
- #
96
+ #
97
97
  # The optional parameter hash can be used to pass parameters to the
98
98
  # unparser to affect its behavior. The exact parameters supported
99
99
  # are defined by the format.
100
-
100
+
101
101
  def unparse(value_, params_=nil)
102
102
  ''
103
103
  end
104
-
105
-
104
+
105
+
106
106
  # An optional method that does unparsing especially for serialization.
107
107
  # Implement this if normal unparsing is "lossy" and doesn't guarantee
108
108
  # reconstruction of the version number. This method should attempt to
@@ -110,7 +110,7 @@ module Versionomy
110
110
  # reconstructed from the unparsed string. Serialization routines will
111
111
  # first attempt to call this method to unparse for serialization. If
112
112
  # this method is not present, the normal unparse method will be used.
113
- #
113
+ #
114
114
  # Return either the unparsed string, or an array consisting of the
115
115
  # unparsed string and a hash of parse params to pass to the parser
116
116
  # when the string is to be reconstructed. You may also either return
@@ -118,17 +118,17 @@ module Versionomy
118
118
  # cannot be done satisfactorily for serialization. In this case,
119
119
  # serialization will be done using the raw value data rather than an
120
120
  # unparsed string.
121
- #
121
+ #
122
122
  # This default implementation just turns around and calls unparse.
123
123
  # Thus it is equivalent to the method not being present at all.
124
-
124
+
125
125
  def unparse_for_serialization(value_)
126
126
  unparse(value_)
127
127
  end
128
-
129
-
128
+
129
+
130
130
  # Determine whether the given value uses this format.
131
-
131
+
132
132
  def ===(obj_)
133
133
  if obj_.kind_of?(Value)
134
134
  obj_.format == self
@@ -136,11 +136,11 @@ module Versionomy
136
136
  obj_ == self
137
137
  end
138
138
  end
139
-
140
-
139
+
140
+
141
141
  end
142
-
143
-
142
+
143
+
144
144
  end
145
-
145
+
146
146
  end