Permalink
Browse files

Clean up version a bit.

  • Loading branch information...
1 parent 59e6310 commit 3b012872f559eddaa3fbf68246098aec62144a8a @jnunemaker committed Jan 4, 2013
Showing with 84 additions and 60 deletions.
  1. +21 −12 lib/user_agent_parser/version.rb
  2. +63 −48 spec/version_spec.rb
@@ -1,28 +1,32 @@
module UserAgentParser
-
class Version
+ # Private: Regex used to split version string into major, minor, patch,
+ # and patch_minor.
SEGMENTS_REGEX = /\d+\-\d+|\d+[a-zA-Z]+$|\d+|[A-Za-z][0-9A-Za-z-]*$/
- attr_accessor :version
+ attr_reader :version
alias :to_s :version
def initialize(version)
- self.version = version.to_s.strip
+ @version = version.to_s.strip
end
- def segments
- version.scan(SEGMENTS_REGEX)
+ def major
+ segments[0]
end
- def [](segment)
- segments[segment]
+ def minor
+ segments[1]
end
- def major; self[0] end
- def minor; self[1] end
- def patch; self[2] end
- def patch_minor; self[3] end
+ def patch
+ segments[2]
+ end
+
+ def patch_minor
+ segments[3]
+ end
def inspect
"#<#{self.class} #{to_s}>"
@@ -32,6 +36,11 @@ def ==(other)
version == other.version
end
- end
+ alias_method :eql?, :==
+ # Private
+ def segments
+ @segments ||= version.scan(SEGMENTS_REGEX)
+ end
+ end
end
View
@@ -1,62 +1,77 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe UserAgentParser::Version do
- describe "#[]" do
- it "should parse '1'" do
- v = UserAgentParser::Version.new("1")
- v[0].must_equal '1'
- end
- it "should parse '1.2'" do
- v = UserAgentParser::Version.new("1.2")
- v[0].must_equal '1'
- v[1].must_equal '2'
- end
- it "should parse '1.2.3'" do
- v = UserAgentParser::Version.new("1.2.3")
- v[0].must_equal '1'
- v[1].must_equal '2'
- v[2].must_equal '3'
- end
- it "should parse '1.2.3b4'" do
- v = UserAgentParser::Version.new("1.2.3b4")
- v[0].must_equal '1'
- v[1].must_equal '2'
- v[2].must_equal '3'
- v[3].must_equal "b4"
- end
- it "should parse '1.2.3-b4'" do
- v = UserAgentParser::Version.new("1.2.3-b4")
- v[0].must_equal '1'
- v[1].must_equal '2'
- v[2].must_equal '3'
- v[3].must_equal "b4"
- end
- it "should parse '1.2.3pre'" do
- v = UserAgentParser::Version.new("1.2.3pre")
- v[0].must_equal '1'
- v[1].must_equal '2'
- v[2].must_equal "3pre"
- end
- it "should parse '1.2.3-45'" do
- v = UserAgentParser::Version.new("1.2.3-45")
- v[0].must_equal '1'
- v[1].must_equal '2'
- v[2].must_equal "3-45"
- end
+ it "parses '1'" do
+ version = UserAgentParser::Version.new("1")
+ version.major.must_equal '1'
+ end
+
+ it "parses '1.2'" do
+ version = UserAgentParser::Version.new("1.2")
+ version.major.must_equal '1'
+ version.minor.must_equal '2'
+ end
+
+ it "parses '1.2.3'" do
+ version = UserAgentParser::Version.new("1.2.3")
+ version.major.must_equal '1'
+ version.minor.must_equal '2'
+ version.patch.must_equal '3'
+ end
+
+ it "parses '1.2.3b4'" do
+ version = UserAgentParser::Version.new("1.2.3b4")
+ version.major.must_equal '1'
+ version.minor.must_equal '2'
+ version.patch.must_equal '3'
+ version.patch_minor.must_equal "b4"
+ end
+
+ it "parses '1.2.3-b4'" do
+ version = UserAgentParser::Version.new("1.2.3-b4")
+ version.major.must_equal '1'
+ version.minor.must_equal '2'
+ version.patch.must_equal '3'
+ version.patch_minor.must_equal "b4"
+ end
+
+ it "parses '1.2.3pre'" do
+ version = UserAgentParser::Version.new("1.2.3pre")
+ version.major.must_equal '1'
+ version.minor.must_equal '2'
+ version.patch.must_equal "3pre"
+ end
+
+ it "parses '1.2.3-45'" do
+ version = UserAgentParser::Version.new("1.2.3-45")
+ version.major.must_equal '1'
+ version.minor.must_equal '2'
+ version.patch.must_equal "3-45"
end
+
describe "#to_s" do
- it "should return the same string as initialized with" do
- UserAgentParser::Version.new("1.2.3b4").to_s.must_equal "1.2.3b4"
+ it "returns the same string as initialized with" do
+ version = UserAgentParser::Version.new("1.2.3b4")
+ version.to_s.must_equal "1.2.3b4"
end
end
+
describe "#==" do
- it "should return true for same versions" do
- UserAgentParser::Version.new("1.2.3").must_equal UserAgentParser::Version.new("1.2.3")
+ it "returns true for same versions" do
+ version = UserAgentParser::Version.new("1.2.3")
+ version.must_equal UserAgentParser::Version.new("1.2.3")
+ end
+
+ it "returns false for different versions" do
+ version = UserAgentParser::Version.new("1.2.3")
+ version.wont_equal UserAgentParser::Version.new("1.2.2")
end
end
+
describe "#inspect" do
- it "should return the class and version" do
- UserAgentParser::Version.new("1.2.3").inspect.must_equal "#<UserAgentParser::Version 1.2.3>"
+ it "returns the class and version" do
+ version = UserAgentParser::Version.new("1.2.3")
+ version.inspect.must_equal "#<UserAgentParser::Version 1.2.3>"
end
end
end

0 comments on commit 3b01287

Please sign in to comment.