versionomy 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ === 0.4.2 / 2012-02-17
2
+
3
+ * Support psych interface for YAML serialization.
4
+
1
5
  === 0.4.1 / 2011-04-26
2
6
 
3
7
  * Support underscore "_" as a delimiter.
data/Version CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.4.2
@@ -1,15 +1,15 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # Versionomy entry point
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
@@ -36,22 +36,16 @@
36
36
 
37
37
  require 'blockenspiel'
38
38
 
39
-
40
- dir_ = ::File.expand_path('versionomy', ::File.dirname(__FILE__))
41
-
42
- includes_ = [
43
- 'errors',
44
- 'schema',
45
- 'schema/field',
46
- 'schema/wrapper',
47
- 'format',
48
- 'format/base',
49
- 'format/delimiter',
50
- 'value',
51
- 'conversion',
52
- 'conversion/base',
53
- 'conversion/parsing',
54
- 'interface',
55
- 'version',
56
- ]
57
- includes_.each{ |file_| require "#{dir_}/#{file_}" }
39
+ require 'versionomy/errors'
40
+ require 'versionomy/schema'
41
+ require 'versionomy/schema/field'
42
+ require 'versionomy/schema/wrapper'
43
+ require 'versionomy/format'
44
+ require 'versionomy/format/base'
45
+ require 'versionomy/format/delimiter'
46
+ require 'versionomy/value'
47
+ require 'versionomy/conversion'
48
+ require 'versionomy/conversion/base'
49
+ require 'versionomy/conversion/parsing'
50
+ require 'versionomy/interface'
51
+ require 'versionomy/version'
@@ -1,15 +1,15 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # Versionomy conversion interface and registry
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
@@ -38,14 +38,14 @@ require 'thread'
38
38
 
39
39
 
40
40
  module Versionomy
41
-
42
-
41
+
42
+
43
43
  # === Conversion between version schemas.
44
- #
44
+ #
45
45
  # Conversions are algorithms for converting from one schema to another.
46
46
  # This is useful for performing conversions as well as comparing version
47
47
  # numbers that use different schemas.
48
- #
48
+ #
49
49
  # To implement a conversion algorithm, implement the API defined by
50
50
  # Versionomy::Conversion::Base. Then, register your conversion by calling
51
51
  # Versionomy::Conversion#register. You will need to specify which schemas
@@ -54,49 +54,49 @@ module Versionomy
54
54
  # it will use your conversion. You can register the same conversion object
55
55
  # for multiple pairs of schemas, but you can register only one conversion
56
56
  # object for any pair.
57
- #
57
+ #
58
58
  # A common technique for doing conversions is to unparse the version to a
59
59
  # string, and then parse it in the new format. Versionomy provides a tool,
60
60
  # Versionomy::Conversion::Parsing, for performing such conversions. The
61
61
  # conversions between the standard and rubygems formats uses this tool.
62
62
  # See Versionomy::Conversion::Rubygems for annotated examples.
63
-
63
+
64
64
  module Conversion
65
-
65
+
66
66
  @registry = {}
67
67
  @mutex = ::Mutex.new
68
-
68
+
69
69
  class << self
70
-
71
-
70
+
71
+
72
72
  # Convert the given value to the given format. This is identical to
73
73
  # calling <tt>value_.convert(format_, convert_params_)</tt>.
74
- #
74
+ #
75
75
  # The format may be specified as a format object or as the name of a
76
76
  # format in the Format registry.
77
- #
77
+ #
78
78
  # Raises Versionomy::Errors::ConversionError if the value could not
79
79
  # be converted.
80
-
80
+
81
81
  def convert(value_, format_, convert_params_=nil)
82
82
  value_.convert(format_, convert_params_)
83
83
  end
84
-
85
-
84
+
85
+
86
86
  # Get a conversion capable of converting between the given schemas.
87
- #
87
+ #
88
88
  # The schemas may be specified as format names, Format objects,
89
89
  # schema wrapper objects, or the root field of the schema.
90
- #
90
+ #
91
91
  # If strict is set to false, returns nil if no such conversion could
92
92
  # be found. If strict is set to true, may raise one of these errors:
93
- #
93
+ #
94
94
  # Raises Versionomy::Errors::UnknownFormatError if a format was
95
95
  # specified by name but the name is not known.
96
- #
96
+ #
97
97
  # Raises Versionomy::Errors::UnknownConversionError if the formats
98
98
  # were recognized but no conversion was found to handle them.
99
-
99
+
100
100
  def get(from_schema_, to_schema_, strict_=false)
101
101
  key_ = _get_key(from_schema_, to_schema_)
102
102
  conversion_ = @mutex.synchronize{ @registry[key_] }
@@ -105,19 +105,19 @@ module Versionomy
105
105
  end
106
106
  conversion_
107
107
  end
108
-
109
-
108
+
109
+
110
110
  # Register the given conversion as the handler for the given schemas.
111
- #
111
+ #
112
112
  # The schemas may be specified as format names, Format objects,
113
113
  # schema wrapper objects, or the root field of the schema.
114
- #
114
+ #
115
115
  # Raises Versionomy::Errors::ConversionRedefinedError if a conversion
116
116
  # has already been registered for the given schemas.
117
- #
117
+ #
118
118
  # Raises Versionomy::Errors::UnknownFormatError if a format was
119
119
  # specified by name but the name is not known.
120
-
120
+
121
121
  def register(from_schema_, to_schema_, conversion_, silent_=false)
122
122
  key_ = _get_key(from_schema_, to_schema_)
123
123
  @mutex.synchronize do
@@ -130,25 +130,25 @@ module Versionomy
130
130
  end
131
131
  end
132
132
  end
133
-
134
-
133
+
134
+
135
135
  private
136
-
136
+
137
137
  def _get_key(from_schema_, to_schema_) # :nodoc:
138
138
  [_get_schema(from_schema_), _get_schema(to_schema_)]
139
139
  end
140
-
140
+
141
141
  def _get_schema(schema_) # :nodoc:
142
142
  schema_ = Format.get(schema_, true) if schema_.kind_of?(::String) || schema_.kind_of?(::Symbol)
143
143
  schema_ = schema_.schema if schema_.respond_to?(:schema)
144
144
  schema_ = schema_.root_field if schema_.respond_to?(:root_field)
145
145
  schema_
146
146
  end
147
-
148
-
147
+
148
+
149
149
  end
150
-
150
+
151
151
  end
152
-
153
-
152
+
153
+
154
154
  end
@@ -1,15 +1,15 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # Versionomy conversion 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,40 +35,40 @@
35
35
 
36
36
 
37
37
  module Versionomy
38
-
39
-
38
+
39
+
40
40
  module Conversion
41
-
42
-
41
+
42
+
43
43
  # The base conversion class.
44
- #
44
+ #
45
45
  # This base class defines the API for a conversion. All conversions must
46
46
  # define the method <tt>convert_value</tt> documented here. Conversions
47
47
  # need not actually extend this base class, as long as they duck-type
48
48
  # this method. However, this base class does provide a few convenience
49
49
  # methods such as a sane implementation of inspect.
50
-
50
+
51
51
  class Base
52
-
53
-
52
+
53
+
54
54
  # Create a conversion using a simple DSL.
55
55
  # You can pass a block to the initializer that takes the same
56
56
  # parameters as convert_value, and the conversion will use that block
57
57
  # to perform the conversion.
58
-
58
+
59
59
  def initialize(&block_)
60
60
  @_converter = block_
61
61
  end
62
-
63
-
62
+
63
+
64
64
  # Convert the given value to the given format and return the converted
65
65
  # value.
66
- #
66
+ #
67
67
  # The convert_params may be interpreted however the particular
68
68
  # conversion wishes.
69
- #
69
+ #
70
70
  # Raises Versionomy::Errors::ConversionError if the conversion failed.
71
-
71
+
72
72
  def convert_value(value_, format_, convert_params_=nil)
73
73
  if @_converter
74
74
  @_converter.call(value_, format_, convert_params_)
@@ -76,25 +76,25 @@ module Versionomy
76
76
  raise Errors::ConversionError, "Conversion not implemented"
77
77
  end
78
78
  end
79
-
80
-
79
+
80
+
81
81
  # Inspect this conversion.
82
-
82
+
83
83
  def inspect
84
84
  "#<#{self.class}:0x#{object_id.to_s(16)}>"
85
85
  end
86
-
87
-
86
+
87
+
88
88
  # The default to_s implementation just calls inspect.
89
-
89
+
90
90
  def to_s
91
91
  inspect
92
92
  end
93
-
94
-
93
+
94
+
95
95
  end
96
-
97
-
96
+
97
+
98
98
  end
99
-
99
+
100
100
  end
@@ -1,15 +1,15 @@
1
1
  # -----------------------------------------------------------------------------
2
- #
2
+ #
3
3
  # Versionomy conversion 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,27 +35,27 @@
35
35
 
36
36
 
37
37
  module Versionomy
38
-
39
-
38
+
39
+
40
40
  module Conversion
41
-
42
-
41
+
42
+
43
43
  # A conversion strategy that relies on parsing.
44
44
  # Essentially, it unparses the value and then attempts to parse it with
45
45
  # the new format.
46
-
46
+
47
47
  class Parsing < Base
48
-
49
-
48
+
49
+
50
50
  # Create a parsing conversion.
51
- #
51
+ #
52
52
  # By default, this just unparses and reparses using the default
53
53
  # parse settings. In some cases, this may be enough, but you may
54
54
  # wish to improve the reliability of the conversion by tweaking the
55
55
  # parsing settings. To do so, pass a block to the new method, and
56
56
  # call methods of Versionomy::Conversion::Parsing::Builder in that
57
57
  # block.
58
-
58
+
59
59
  def initialize(&block_)
60
60
  if block_
61
61
  builder_ = Builder.new
@@ -66,16 +66,16 @@ module Versionomy
66
66
  @parse_params_generator ||= builder_._get_parse_params_generator
67
67
  end
68
68
  end
69
-
70
-
69
+
70
+
71
71
  # Returns a value equivalent to the given value in the given format.
72
- #
72
+ #
73
73
  # The convert_params are passed to this conversion's customization
74
74
  # blocks (if any).
75
- #
75
+ #
76
76
  # Raises Versionomy::Errors::ConversionError if the conversion failed.
77
77
  # Typically, this is due to a failure of the parsing or unparsing.
78
-
78
+
79
79
  def convert_value(value_, format_, convert_params_=nil)
80
80
  begin
81
81
  convert_params_ ||= {}
@@ -103,47 +103,47 @@ module Versionomy
103
103
  raise Errors::ConversionError, "Parsing failed: #{ex_.inspect}"
104
104
  end
105
105
  end
106
-
107
-
106
+
107
+
108
108
  # Call methods of this class in the block passed to
109
109
  # Versionomy::Conversion::Parsing#new to fine-tune the behavior of
110
110
  # the converter.
111
-
111
+
112
112
  class Builder
113
-
113
+
114
114
  include ::Blockenspiel::DSL
115
-
116
-
115
+
116
+
117
117
  def initialize # :nodoc:
118
118
  @original_value_modifier = nil
119
119
  @string_modifier = nil
120
120
  @parse_params_generator = nil
121
121
  @unparse_params_modifier = nil
122
122
  end
123
-
124
-
123
+
124
+
125
125
  # Provide a block that generates the params used to parse the new
126
126
  # value. The block should take one parameter, the convert_params
127
127
  # passed to convert_value (which may be nil). It should return the
128
128
  # parse params that should be used.
129
-
129
+
130
130
  def to_generate_parse_params(&block_)
131
131
  @parse_params_generator = block_
132
132
  end
133
-
134
-
133
+
134
+
135
135
  # Provide a block that can modify the params used to unparse the
136
136
  # old value. The block should take two parameters: first, the
137
137
  # original unparse params from the old value (which may be nil),
138
138
  # and second, the convert_params passed to convert_value (which
139
139
  # may also be nil). It should return the unparse params that
140
140
  # should actually be used.
141
-
141
+
142
142
  def to_modify_unparse_params(&block_)
143
143
  @unparse_params_modifier = block_
144
144
  end
145
-
146
-
145
+
146
+
147
147
  # Provide a block that can modify the original value prior to it
148
148
  # being unparsed. The block should take two parameters: first, the
149
149
  # original value to be converted, and second, the convert_params
@@ -152,46 +152,46 @@ module Versionomy
152
152
  # originally passed in. This method may fail-fast by raising a
153
153
  # Versionomy::Errors::ConversionError if it determines that the
154
154
  # value passed in cannot be converted as is.
155
-
155
+
156
156
  def to_modify_original_value(&block_)
157
157
  @original_value_modifier = block_
158
158
  end
159
-
160
-
159
+
160
+
161
161
  # Provide a block that can modify the unparsed string prior to
162
162
  # it being passed to the parser. The block should take two
163
163
  # parameters: first, the string resulting from unparsing the old
164
164
  # value, and second, the convert_params passed to convert_value
165
165
  # (which may be nil). It should return the string to be parsed to
166
166
  # get the new value.
167
-
167
+
168
168
  def to_modify_string(&block_)
169
169
  @string_modifier = block_
170
170
  end
171
-
172
-
171
+
172
+
173
173
  def _get_original_value_modifier # :nodoc:
174
174
  @original_value_modifier
175
175
  end
176
-
176
+
177
177
  def _get_string_modifier # :nodoc:
178
178
  @string_modifier
179
179
  end
180
-
180
+
181
181
  def _get_unparse_params_modifier # :nodoc:
182
182
  @unparse_params_modifier
183
183
  end
184
-
184
+
185
185
  def _get_parse_params_generator # :nodoc:
186
186
  @parse_params_generator
187
187
  end
188
-
188
+
189
189
  end
190
-
191
-
190
+
191
+
192
192
  end
193
-
194
-
193
+
194
+
195
195
  end
196
-
196
+
197
197
  end