Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support minimum field width with leading zeros

  • Loading branch information...
commit 4c3f102fb7f08b661c77396623fd67d8c47d026f 1 parent 2ac4091
@dazuma authored
View
4 History.rdoc
@@ -1,3 +1,7 @@
+=== 0.2.5 / 2009-11-24
+
+* Preserve minimum width of a field, if the field has leading zeros. For example, "2.01".
+
=== 0.2.4 / 2009-11-19
* 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".
View
24 lib/versionomy/format/delimiter.rb
@@ -511,7 +511,15 @@ def initialize(recognizers_, field_, default_opts_) # :nodoc:
# Recognize a numeric-formatted integer field.
# Using the opts parameter, you can override any of the field's
- # overall parsing options.
+ # overall parsing options. You may also set the following additional
+ # options:
+ #
+ # <tt>:strip_leading_zeros</tt>::
+ # If false (the default), and a value has leading zeros, it is
+ # assumed that the field has a minimum width, and unparsing will
+ # always pad left with zeros to reach that minimum width. If set
+ # to true, leading zeros are stripped from a value, and this
+ # padding is never done.
def recognize_number(opts_={})
@recognizers << Delimiter::BasicIntegerRecognizer.new(@field, @default_opts.merge(opts_))
@@ -881,15 +889,25 @@ def unparse(value_, style_, unparse_params_, required_for_later_)
class BasicIntegerRecognizer < RecognizerBase #:nodoc:
def initialize(field_, opts_={})
+ @strip_leading_zeros = opts_[:strip_leading_zeros]
+ @width_unparse_param_key = "#{field_.name}_width".to_sym
setup(field_, '\d+', opts_)
end
def parsed_value(value_, parse_params_)
- [value_.to_i, nil]
+ if !@strip_leading_zeros && value_ =~ /^0\d/
+ [value_.to_i, {@width_unparse_param_key => value_.length}]
+ else
+ [value_.to_i, nil]
+ end
end
def unparsed_value(value_, style_, unparse_params_)
- value_.to_s
+ if !@strip_leading_zeros && (width_ = unparse_params_[@width_unparse_param_key])
+ "%0#{width_.to_i}d" % value_
+ else
+ value_.to_s
+ end
end
end
View
2  lib/versionomy/version.rb
@@ -37,7 +37,7 @@
module Versionomy
# Current gem version, as a frozen string.
- VERSION_STRING = '0.2.4'.freeze
+ VERSION_STRING = '0.2.5'.freeze
# Current gem version, as a Versionomy::Value.
VERSION = ::Versionomy.parse(VERSION_STRING, :standard)
View
39 tests/tc_standard_parse.rb
@@ -93,6 +93,45 @@ def test_parsing_trailing_zeros
end
+ # Test parsing with leading zeros on a field.
+
+ def test_parsing_field_leading_zeros
+ value_ = ::Versionomy.parse('2.09')
+ assert_equal(2, value_.major)
+ assert_equal(9, value_.minor)
+ assert_equal(0, value_.tiny)
+ assert_equal('2.09', value_.unparse)
+ value_ = value_.bump(:minor)
+ assert_equal(10, value_.minor)
+ assert_equal('2.10', value_.unparse)
+ value_ = value_.change(:minor => 123)
+ assert_equal(123, value_.minor)
+ assert_equal('2.123', value_.unparse)
+ value_ = value_.change(:minor => 4)
+ assert_equal(4, value_.minor)
+ assert_equal('2.04', value_.unparse)
+ value_ = ::Versionomy.parse('2.00')
+ assert_equal(0, value_.minor)
+ assert_equal('2.00', value_.unparse)
+ end
+
+
+ # Test unparsing with a minimum width.
+
+ def test_unparsing_minimum_width_field
+ value_ = ::Versionomy.parse('2.0')
+ assert_equal('2.0', value_.unparse)
+ assert_equal('2.00', value_.unparse(:minor_width => 2))
+ assert_equal('02.0', value_.unparse(:major_width => 2))
+ value_ = value_.bump(:minor)
+ assert_equal('2.1', value_.unparse)
+ assert_equal('2.01', value_.unparse(:minor_width => 2))
+ value_ = value_.change(:minor => 12)
+ assert_equal('2.12', value_.unparse)
+ assert_equal('2.12', value_.unparse(:minor_width => 1))
+ end
+
+
# Test parsing major version only.
def test_parsing_major_only
Please sign in to comment.
Something went wrong with that request. Please try again.