versionomy 0.2.4 → 0.2.5

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/History.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.2.5 / 2009-11-24
2
+
3
+ * Preserve minimum width of a field, if the field has leading zeros. For example, "2.01".
4
+
1
5
  === 0.2.4 / 2009-11-19
2
6
 
3
7
  * Fixed a regression introduced in 0.2.2 where "1.0a" was being recognized as an alpha version rather than a patchlevel of 1, and similar for "1.0b" and "1.0d".
@@ -511,7 +511,15 @@ module Versionomy
511
511
 
512
512
  # Recognize a numeric-formatted integer field.
513
513
  # Using the opts parameter, you can override any of the field's
514
- # overall parsing options.
514
+ # overall parsing options. You may also set the following additional
515
+ # options:
516
+ #
517
+ # <tt>:strip_leading_zeros</tt>::
518
+ # If false (the default), and a value has leading zeros, it is
519
+ # assumed that the field has a minimum width, and unparsing will
520
+ # always pad left with zeros to reach that minimum width. If set
521
+ # to true, leading zeros are stripped from a value, and this
522
+ # padding is never done.
515
523
 
516
524
  def recognize_number(opts_={})
517
525
  @recognizers << Delimiter::BasicIntegerRecognizer.new(@field, @default_opts.merge(opts_))
@@ -881,15 +889,25 @@ module Versionomy
881
889
  class BasicIntegerRecognizer < RecognizerBase #:nodoc:
882
890
 
883
891
  def initialize(field_, opts_={})
892
+ @strip_leading_zeros = opts_[:strip_leading_zeros]
893
+ @width_unparse_param_key = "#{field_.name}_width".to_sym
884
894
  setup(field_, '\d+', opts_)
885
895
  end
886
896
 
887
897
  def parsed_value(value_, parse_params_)
888
- [value_.to_i, nil]
898
+ if !@strip_leading_zeros && value_ =~ /^0\d/
899
+ [value_.to_i, {@width_unparse_param_key => value_.length}]
900
+ else
901
+ [value_.to_i, nil]
902
+ end
889
903
  end
890
904
 
891
905
  def unparsed_value(value_, style_, unparse_params_)
892
- value_.to_s
906
+ if !@strip_leading_zeros && (width_ = unparse_params_[@width_unparse_param_key])
907
+ "%0#{width_.to_i}d" % value_
908
+ else
909
+ value_.to_s
910
+ end
893
911
  end
894
912
 
895
913
  end
@@ -37,7 +37,7 @@
37
37
  module Versionomy
38
38
 
39
39
  # Current gem version, as a frozen string.
40
- VERSION_STRING = '0.2.4'.freeze
40
+ VERSION_STRING = '0.2.5'.freeze
41
41
 
42
42
  # Current gem version, as a Versionomy::Value.
43
43
  VERSION = ::Versionomy.parse(VERSION_STRING, :standard)
@@ -93,6 +93,45 @@ module Versionomy
93
93
  end
94
94
 
95
95
 
96
+ # Test parsing with leading zeros on a field.
97
+
98
+ def test_parsing_field_leading_zeros
99
+ value_ = ::Versionomy.parse('2.09')
100
+ assert_equal(2, value_.major)
101
+ assert_equal(9, value_.minor)
102
+ assert_equal(0, value_.tiny)
103
+ assert_equal('2.09', value_.unparse)
104
+ value_ = value_.bump(:minor)
105
+ assert_equal(10, value_.minor)
106
+ assert_equal('2.10', value_.unparse)
107
+ value_ = value_.change(:minor => 123)
108
+ assert_equal(123, value_.minor)
109
+ assert_equal('2.123', value_.unparse)
110
+ value_ = value_.change(:minor => 4)
111
+ assert_equal(4, value_.minor)
112
+ assert_equal('2.04', value_.unparse)
113
+ value_ = ::Versionomy.parse('2.00')
114
+ assert_equal(0, value_.minor)
115
+ assert_equal('2.00', value_.unparse)
116
+ end
117
+
118
+
119
+ # Test unparsing with a minimum width.
120
+
121
+ def test_unparsing_minimum_width_field
122
+ value_ = ::Versionomy.parse('2.0')
123
+ assert_equal('2.0', value_.unparse)
124
+ assert_equal('2.00', value_.unparse(:minor_width => 2))
125
+ assert_equal('02.0', value_.unparse(:major_width => 2))
126
+ value_ = value_.bump(:minor)
127
+ assert_equal('2.1', value_.unparse)
128
+ assert_equal('2.01', value_.unparse(:minor_width => 2))
129
+ value_ = value_.change(:minor => 12)
130
+ assert_equal('2.12', value_.unparse)
131
+ assert_equal('2.12', value_.unparse(:minor_width => 1))
132
+ end
133
+
134
+
96
135
  # Test parsing major version only.
97
136
 
98
137
  def test_parsing_major_only
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: versionomy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-19 00:00:00 -08:00
12
+ date: 2009-11-24 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency