version-serializer 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10a94eb3cccbbe20cb85b93dded6f33d131ef39f
4
- data.tar.gz: f3fa06eacc12e06e94b64459e4091ae63ab59703
3
+ metadata.gz: 4ed6b796906bba1905dcab7f248a3d57d45ee52b
4
+ data.tar.gz: 6c20492d42f639bbebac50dbfc8df4aaf165ce43
5
5
  SHA512:
6
- metadata.gz: 3fb67fd64dff9ee815c00c937b56324793372005d1b43d09ea9f9a88db4e6a51c365cf734420dae176c2a3f017402cb1bef0fa9204a10b2c695504f71af94d96
7
- data.tar.gz: f94a398b2e2842b0ada602bc5ca8e954eaa3b7eeb20b2a60d988bf725384e2b493aa0c51e986a441726606ab8f3467ced96f2427219d6351726703af6e1446af
6
+ metadata.gz: 707796012d0dd5f87dbc68bbfb2fb72ffc8c2854b5c024be2cb0af1c38b3e71066e48f2c730fbd4fba0c6b5a8bdaf30a0a64367a67f88deeccf93f63d9b979ee
7
+ data.tar.gz: ce6c58f585107e05decc1551ee92242edc7e6437fa49a1e4921568fb92c0d2cfd079156dd647dd14788ee399e4bbc429e1b50d37b7969e5a062147f9d3718edf
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ gemspec
5
5
 
6
6
  group :testing do
7
7
  gem "pry"
8
+ gem "pry-nav"
8
9
  gem "pry-doc"
9
10
 
10
11
  gem "guard"
@@ -15,4 +16,4 @@ group :testing do
15
16
  gem "terminal-notifier-guard"
16
17
 
17
18
  gem 'simplecov', :require => false
18
- end
19
+ end
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- version-serializer (1.0.0)
4
+ version-serializer (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -39,6 +39,8 @@ GEM
39
39
  pry-doc (0.6.0)
40
40
  pry (~> 0.9)
41
41
  yard (~> 0.8)
42
+ pry-nav (0.2.4)
43
+ pry (>= 0.9.10, < 0.11.0)
42
44
  rake (10.3.2)
43
45
  rb-fsevent (0.9.4)
44
46
  rb-inotify (0.9.4)
@@ -64,6 +66,7 @@ DEPENDENCIES
64
66
  guard-yard
65
67
  pry
66
68
  pry-doc
69
+ pry-nav
67
70
  rake
68
71
  rb-fsevent
69
72
  simplecov
@@ -71,4 +74,4 @@ DEPENDENCIES
71
74
  version-serializer!
72
75
 
73
76
  BUNDLED WITH
74
- 1.12.3
77
+ 1.14.3
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # VersionSerializer
2
2
 
3
- TODO: Write a gem description
3
+ Serializes semantic versions and adds compatibility methods to check if a given
4
+ instance is compatible with a certain version by specifying a range.
4
5
 
5
6
  ## Installation
6
7
 
@@ -16,14 +17,19 @@ Or install it yourself as:
16
17
 
17
18
  $ gem install version_serializer
18
19
 
20
+
19
21
  ## Usage
20
22
 
21
- TODO: Write usage instructions here
23
+ ```ruby
22
24
 
23
- ## Contributing
25
+ # Model Definition
26
+ class YourModel < ActiveRecord::Base
27
+ include VersionSerializer::RangeCompatibility
28
+ serialize :min_version, VersionSerializer::SemanticVersion
29
+ serialize :max_version, VersionSerializer::SemanticVersion
30
+ end
24
31
 
25
- 1. Fork it ( http://github.com/<my-github-username>/version_serializer/fork )
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
32
+ # Compatibility
33
+ m = YourModel.create(min_version: "1.0.1", max_version: "2.3.4")
34
+ m.compatible? "2.0"
35
+ ```
@@ -1,53 +1,3 @@
1
1
  require "version_serializer/version"
2
-
3
- module VersionSerializer
4
- class Version
5
- attr_accessor :major, :minor
6
-
7
- def initialize major, minor
8
- @major = major
9
- @minor = minor
10
- end
11
-
12
- def self.load (data)
13
- return nil if data.nil?
14
- self.new(*('%.3f' % data).split('.').map(&:to_i))
15
- end
16
-
17
- def self.dump (object)
18
- return nil if object.nil?
19
-
20
- if object.respond_to?(:major)
21
- ("%d.%03d" % [object.major, object.minor]).to_f
22
- else
23
- object
24
- end
25
- end
26
-
27
- def self.from_string data
28
- return nil if data.nil?
29
- self.new(*data.to_s.split('.').map(&:to_i))
30
- end
31
-
32
- def self.range min, max
33
- self.dump(min) .. self.dump(max)
34
- end
35
-
36
- include Comparable
37
- def <=> other
38
- return nil if other.nil?
39
-
40
- [major, minor] <=> [other.major, other.minor]
41
- end
42
-
43
- def as_json options = {}
44
- to_s
45
- end
46
-
47
- def to_s
48
- return nil if major.nil? || minor.nil?
49
-
50
- "%d.%d" % [major, minor]
51
- end
52
- end
53
- end
2
+ require "version_serializer/semantic_version"
3
+ require "version_serializer/range_compatibility"
@@ -0,0 +1,16 @@
1
+ module VersionSerializer
2
+ module RangeCompatibility
3
+ def has_version?
4
+ min_version || max_version
5
+ end
6
+
7
+ def compatible? version
8
+ return true unless has_version?
9
+
10
+ version = SemanticVersion.new(version) if version.is_a? String
11
+
12
+ (min_version.nil? || min_version && version >= min_version) &&
13
+ (max_version.nil? || max_version && version <= max_version)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ module VersionSerializer
2
+ class SemanticVersion < Gem::Version
3
+ def self.load (data)
4
+ return nil if data.nil?
5
+ self.new(data)
6
+ end
7
+
8
+ def self.dump (object)
9
+ return nil if object.nil?
10
+ object.to_s
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,53 @@
1
1
  module VersionSerializer
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
+
4
+ class Version
5
+ attr_accessor :major, :minor
6
+
7
+ def initialize major, minor
8
+ @major = major
9
+ @minor = minor
10
+ end
11
+
12
+ def self.load (data)
13
+ return nil if data.nil?
14
+ self.new(*('%.3f' % data).split('.').map(&:to_i))
15
+ end
16
+
17
+ def self.dump (object)
18
+ return nil if object.nil?
19
+
20
+ if object.respond_to?(:major)
21
+ ("%d.%03d" % [object.major, object.minor]).to_f
22
+ else
23
+ object
24
+ end
25
+ end
26
+
27
+ def self.from_string data
28
+ return nil if data.nil?
29
+ self.new(*data.to_s.split('.').map(&:to_i))
30
+ end
31
+
32
+ def self.range min, max
33
+ self.dump(min) .. self.dump(max)
34
+ end
35
+
36
+ include Comparable
37
+ def <=> other
38
+ return nil if other.nil?
39
+
40
+ [major, minor] <=> [other.major, other.minor]
41
+ end
42
+
43
+ def as_json options = {}
44
+ to_s
45
+ end
46
+
47
+ def to_s
48
+ return nil if major.nil? || minor.nil?
49
+
50
+ "%d.%d" % [major, minor]
51
+ end
52
+ end
3
53
  end
@@ -0,0 +1,40 @@
1
+ require 'version_serializer'
2
+ require 'test_helper'
3
+
4
+
5
+ class CompatibilityTest < MiniTest::Test
6
+
7
+ class TestResource
8
+ include VersionSerializer::RangeCompatibility
9
+
10
+ attr_accessor :min_version, :max_version
11
+
12
+ def initialize(min_version=nil, max_version=nil)
13
+ @min_version = VersionSerializer::SemanticVersion.new(min_version) if min_version
14
+ @max_version = VersionSerializer::SemanticVersion.new(max_version) if max_version
15
+ end
16
+
17
+ end
18
+
19
+ def test_compatible
20
+ binding.pry
21
+ [
22
+ ["1.2", "1.5"],
23
+ [nil, "1.5"],
24
+ ["1.2.1", nil],
25
+ [nil, nil]
26
+ ].each do |min, max|
27
+ assert TestResource.new(min, max).compatible?("1.2.2")
28
+ end
29
+ end
30
+
31
+ def test_incompatible
32
+ [
33
+ ["1.2", "1.5"],
34
+ [nil, "1.2"],
35
+ ["1.7.7", nil]
36
+ ].each do |min, max|
37
+ assert !TestResource.new(min, max).compatible?("1.6")
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,21 @@
1
+ require 'version_serializer'
2
+ require 'test_helper'
3
+
4
+ class SemanticVersionTest < MiniTest::Test
5
+ def test_load
6
+ assert_equal VersionSerializer::SemanticVersion.new("2.4.0"), VersionSerializer::SemanticVersion.load("2.4.0")
7
+ end
8
+
9
+ def test_dump
10
+ version = VersionSerializer::SemanticVersion.new("2.4.0")
11
+ assert_equal version.to_s, VersionSerializer::SemanticVersion.dump(version)
12
+ end
13
+
14
+ def test_nil_load
15
+ assert_nil VersionSerializer::SemanticVersion.load(nil)
16
+ end
17
+
18
+ def test_nil_dump
19
+ assert_nil VersionSerializer::SemanticVersion.dump(nil)
20
+ end
21
+ end
@@ -2,7 +2,6 @@ require 'version_serializer'
2
2
  require 'test_helper'
3
3
 
4
4
  class VersionTest < MiniTest::Test
5
- # test "Should serialize version" do
6
5
  def test_serialize_version
7
6
  assert_equal 3.002, VersionSerializer::Version.dump(VersionSerializer::Version.new(3, 2))
8
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: version-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - NDrive DevOps Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-05 00:00:00.000000000 Z
11
+ date: 2017-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,8 +52,12 @@ files:
52
52
  - README.md
53
53
  - Rakefile
54
54
  - lib/version_serializer.rb
55
+ - lib/version_serializer/range_compatibility.rb
56
+ - lib/version_serializer/semantic_version.rb
55
57
  - lib/version_serializer/version.rb
56
58
  - test/test_helper.rb
59
+ - test/unit/range_compatibility_test.rb
60
+ - test/unit/semantic_version_test.rb
57
61
  - test/unit/version_test.rb
58
62
  - version_serializer.gemspec
59
63
  homepage: http://www.ndrive.com
@@ -76,11 +80,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
80
  version: '0'
77
81
  requirements: []
78
82
  rubyforge_project:
79
- rubygems_version: 2.5.1
83
+ rubygems_version: 2.6.10
80
84
  signing_key:
81
85
  specification_version: 4
82
86
  summary: Serialize version
83
87
  test_files:
84
88
  - test/test_helper.rb
89
+ - test/unit/range_compatibility_test.rb
90
+ - test/unit/semantic_version_test.rb
85
91
  - test/unit/version_test.rb
86
92
  has_rdoc: