has_versions 0.5.0 → 0.6.0
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/has_versions.gemspec +1 -1
- data/lib/has_versions/record/attributes.rb +28 -0
- data/lib/has_versions/record/configuration.rb +20 -0
- data/lib/has_versions/record/orm/cassandra_object.rb +21 -0
- data/lib/has_versions/record/reset.rb +23 -0
- data/lib/has_versions/record.rb +43 -0
- data/lib/has_versions/{version_methods → version}/diff.rb +1 -1
- data/lib/has_versions/{version_methods → version}/log.rb +1 -1
- data/lib/has_versions/version.rb +10 -0
- data/lib/has_versions.rb +13 -8
- data/test/has_versions/record/attributes_test.rb +41 -0
- data/test/has_versions/record/configuration_test.rb +24 -0
- data/test/has_versions/record/reset_test.rb +31 -0
- data/test/has_versions/record_test.rb +31 -0
- data/test/has_versions/version/diff_test.rb +70 -0
- data/test/has_versions/version/log_test.rb +34 -0
- data/test/has_versions/version_test.rb +10 -0
- data/test/support/test_model.rb +3 -2
- data/test/support/test_version.rb +3 -0
- metadata +24 -27
- data/features/has_versions.feature +0 -9
- data/features/step_definitions/has_versions_steps.rb +0 -0
- data/features/support/env.rb +0 -13
- data/lib/has_versions/attributes.rb +0 -18
- data/lib/has_versions/configuration.rb +0 -14
- data/lib/has_versions/orm/cassandra_object.rb +0 -19
- data/lib/has_versions/reset.rb +0 -21
- data/lib/has_versions/versioned.rb +0 -36
- data/test/has_versions/attributes_test.rb +0 -25
- data/test/has_versions/configuration_test.rb +0 -12
- data/test/has_versions/reset_test.rb +0 -29
- data/test/has_versions/version_methods/diff_test.rb +0 -68
- data/test/has_versions/version_methods/log_test.rb +0 -33
- data/test/has_versions/versioned_test.rb +0 -27
data/has_versions.gemspec
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
module HasVersions
|
2
|
+
module Record
|
3
|
+
module Attributes
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def versioned_attributes
|
8
|
+
versioning_configuration.attributes
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def versioned_attributes
|
13
|
+
self.class.versioned_attributes.inject({}) do |memo, attribute|
|
14
|
+
memo[attribute] = versioning_encode_value(attribute, send(attribute))
|
15
|
+
memo
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def associated_versions
|
20
|
+
if versioning_configuration.associated_versions_callback
|
21
|
+
instance_eval(&versioning_configuration.associated_versions_callback)
|
22
|
+
else
|
23
|
+
nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module HasVersions
|
2
|
+
module Record
|
3
|
+
class Configuration
|
4
|
+
attr_accessor :attributes
|
5
|
+
attr_accessor :associated_versions_callback
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
self.attributes = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def attribute(name)
|
12
|
+
attributes << name.to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
def associated_versions(&block)
|
16
|
+
self.associated_versions_callback = block
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module HasVersions
|
2
|
+
module Record
|
3
|
+
module Orm
|
4
|
+
module CassandraObject
|
5
|
+
def versioning_encode_value(name, value)
|
6
|
+
return if value.nil?
|
7
|
+
attribute_definitions[name.to_sym].coder.encode(value)
|
8
|
+
end
|
9
|
+
|
10
|
+
def versioning_decode_value(name, value)
|
11
|
+
return if value.nil?
|
12
|
+
attribute_definitions[name.to_sym].coder.decode(value)
|
13
|
+
end
|
14
|
+
|
15
|
+
def versioning_codable?(name)
|
16
|
+
attribute_definitions.has_key?(name.to_sym)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module HasVersions
|
2
|
+
module Record
|
3
|
+
module Reset
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def from_version(version)
|
8
|
+
new.tap do |object|
|
9
|
+
object.reset!(version)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def reset!(version)
|
15
|
+
self.class.versioned_attributes.each do |attribute|
|
16
|
+
value = versioning_decode_value(attribute, version.snapshot[attribute])
|
17
|
+
send("#{attribute}=", value)
|
18
|
+
end
|
19
|
+
self
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module HasVersions
|
2
|
+
module Record
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class_attribute :version_class_name
|
7
|
+
self.version_class_name = '::Version'
|
8
|
+
|
9
|
+
include HasVersions::Record::Attributes
|
10
|
+
include HasVersions::Record::Reset
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def version_model
|
15
|
+
@version_model ||= begin
|
16
|
+
version_class.class_eval do
|
17
|
+
end
|
18
|
+
version_class
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def versioning_configuration
|
23
|
+
@versioning_configuration ||= HasVersions::Record::Configuration.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def version_class
|
27
|
+
@version_class ||= version_class_name.constantize
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def versioning_configuration
|
32
|
+
self.class.versioning_configuration
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_version
|
36
|
+
self.class.version_model.new(
|
37
|
+
snapshot: versioned_attributes,
|
38
|
+
associated_versions: associated_versions,
|
39
|
+
target: self
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/has_versions.rb
CHANGED
@@ -3,18 +3,23 @@ require 'active_support/all'
|
|
3
3
|
module HasVersions
|
4
4
|
extend ActiveSupport::Autoload
|
5
5
|
|
6
|
-
autoload :
|
7
|
-
autoload :
|
8
|
-
autoload :Reset
|
9
|
-
autoload :Versioned
|
6
|
+
autoload :Record
|
7
|
+
autoload :Version
|
10
8
|
|
11
|
-
module
|
9
|
+
module Record
|
12
10
|
extend ActiveSupport::Autoload
|
13
11
|
|
14
|
-
autoload :
|
12
|
+
autoload :Attributes
|
13
|
+
autoload :Configuration
|
14
|
+
autoload :Reset
|
15
|
+
|
16
|
+
module Orm
|
17
|
+
extend ActiveSupport::Autoload
|
18
|
+
autoload :CassandraObject
|
19
|
+
end
|
15
20
|
end
|
16
21
|
|
17
|
-
module
|
22
|
+
module Version
|
18
23
|
extend ActiveSupport::Autoload
|
19
24
|
|
20
25
|
autoload :Diff
|
@@ -23,5 +28,5 @@ module HasVersions
|
|
23
28
|
end
|
24
29
|
|
25
30
|
ActiveSupport.on_load :cassandra_object do
|
26
|
-
include HasVersions::Orm::CassandraObject
|
31
|
+
include HasVersions::Record::Orm::CassandraObject
|
27
32
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module HasVersions
|
4
|
+
module Record
|
5
|
+
class AttributesTest < MiniTest::Unit::TestCase
|
6
|
+
class VersionedRecord < TestRecord
|
7
|
+
attr_accessor :name
|
8
|
+
attr_accessor :weight
|
9
|
+
attr_accessor :color
|
10
|
+
|
11
|
+
versioning_configuration.attribute :name
|
12
|
+
versioning_configuration.attribute :weight
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_class_versioned_attributes
|
16
|
+
assert_equal ['name', 'weight'], VersionedRecord.versioned_attributes
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_instance_versioned_attributes
|
20
|
+
record = VersionedRecord.new(name: 'joe', weight: 142.0, color: 'green')
|
21
|
+
expected = {'name' => 'joe', 'weight' => '142.0'}
|
22
|
+
|
23
|
+
assert_equal expected, record.versioned_attributes
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_associated_versions_when_defined
|
27
|
+
model = Class.new(VersionedRecord) do
|
28
|
+
versioning_configuration.associated_versions do
|
29
|
+
"#{name} Hendrix"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_equal "Jimi Hendrix", model.new(name: "Jimi").associated_versions
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_associated_versions_when_not_defined
|
37
|
+
assert_nil VersionedRecord.new.associated_versions
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module HasVersions
|
4
|
+
module Record
|
5
|
+
class ConfigurationTest < MiniTest::Unit::TestCase
|
6
|
+
def test_attributes
|
7
|
+
configuration = HasVersions::Record::Configuration.new
|
8
|
+
configuration.attribute :foo
|
9
|
+
configuration.attribute :bar
|
10
|
+
|
11
|
+
assert_equal %w(foo bar), configuration.attributes
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_associated_versions
|
15
|
+
configuration = HasVersions::Record::Configuration.new
|
16
|
+
configuration.associated_versions do
|
17
|
+
"poop"
|
18
|
+
end
|
19
|
+
|
20
|
+
assert_equal "poop", configuration.associated_versions_callback.call
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module HasVersions
|
4
|
+
module Record
|
5
|
+
class ResetTest < MiniTest::Unit::TestCase
|
6
|
+
class ResetRecord < TestRecord
|
7
|
+
attr_accessor :name
|
8
|
+
attr_accessor :color
|
9
|
+
|
10
|
+
versioning_configuration.attribute :name
|
11
|
+
|
12
|
+
def versioning_decode_value(name, value)
|
13
|
+
"hello, #{value}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_from_version
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_reset
|
21
|
+
reset_model = ResetRecord.new(name: "bob", color: "green")
|
22
|
+
version = ResetRecord.version_model.new(snapshot: {name: "joe", color: "blue"})
|
23
|
+
|
24
|
+
reset_model.reset!(version)
|
25
|
+
|
26
|
+
assert_equal "hello, joe", reset_model.name
|
27
|
+
assert_equal "green", reset_model.color
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module HasVersions
|
4
|
+
class RecordTest < MiniTest::Unit::TestCase
|
5
|
+
def test_version_class
|
6
|
+
assert_equal TestVersion, TestRecord.version_class
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_versioning_configuration
|
10
|
+
assert_kind_of HasVersions::Record::Configuration, TestRecord.versioning_configuration
|
11
|
+
assert_equal TestRecord.versioning_configuration, TestRecord.new.versioning_configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_to_version
|
15
|
+
record = Class.new(TestRecord) do
|
16
|
+
attr_accessor :name
|
17
|
+
versioning_configuration.attribute :name
|
18
|
+
versioning_configuration.associated_versions do
|
19
|
+
"hola"
|
20
|
+
end
|
21
|
+
end.new(name: 'wee')
|
22
|
+
|
23
|
+
version = record.to_version
|
24
|
+
|
25
|
+
assert_kind_of TestVersion, version
|
26
|
+
assert_equal record, version.target
|
27
|
+
assert_equal "hola", version.associated_versions
|
28
|
+
assert_equal record.versioned_attributes, version.snapshot
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module HasVersions
|
4
|
+
module Version
|
5
|
+
class DiffTest < MiniTest::Unit::TestCase
|
6
|
+
class DiffRecord < TestRecord
|
7
|
+
attr_accessor :name
|
8
|
+
attr_accessor :color
|
9
|
+
attr_accessor :weight
|
10
|
+
|
11
|
+
versioning_configuration.attribute :name
|
12
|
+
versioning_configuration.attribute :weight
|
13
|
+
|
14
|
+
def versioning_decode_value(name, value)
|
15
|
+
"decoded: #{value}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_diff_with_parent
|
20
|
+
target = DiffRecord.new
|
21
|
+
parent = DiffRecord.version_model.new(target: target, snapshot: {name: 'foo', color: 'green', weight: '4.3'})
|
22
|
+
child = DiffRecord.version_model.new(target: target, parent: parent, snapshot: {name: 'bar', color: 'orange', weight: '4.3'})
|
23
|
+
|
24
|
+
diff = child.diff
|
25
|
+
|
26
|
+
assert_equal({'name' => ['decoded: foo', 'decoded: bar']}, diff)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_diff_with_parameter
|
30
|
+
target = DiffRecord.new
|
31
|
+
this_version = DiffRecord.version_model.new(target: target, snapshot: {name: 'foo', color: 'green', weight: '4.3'})
|
32
|
+
other_version = DiffRecord.version_model.new(target: target, snapshot: {name: 'bar', color: 'orange', weight: '4.3'})
|
33
|
+
|
34
|
+
diff = this_version.diff(other_version)
|
35
|
+
|
36
|
+
assert_equal({'name' => ['decoded: bar', 'decoded: foo']}, diff)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_diff_with_nil
|
40
|
+
target = DiffRecord.new
|
41
|
+
version = DiffRecord.version_model.new(target: target, snapshot: {name: 'bar', color: 'orange', weight: '4.3'})
|
42
|
+
|
43
|
+
diff = version.diff
|
44
|
+
|
45
|
+
assert_equal({'name' => [nil, 'decoded: bar'], 'weight' => [nil, 'decoded: 4.3']}, diff)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_modified_attributes
|
49
|
+
target = DiffRecord.new
|
50
|
+
version = DiffRecord.version_model.new(target: target, snapshot: {'name' => 'joe', 'weight' => '129'})
|
51
|
+
|
52
|
+
assert_equal version.snapshot.keys, version.modified_attributes
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_different_attributes
|
56
|
+
target = DiffRecord.new(name: 'decoded: sam', color: 'decoded: green', weight: 'decoded: 129')
|
57
|
+
version = DiffRecord.version_model.new(target: target, snapshot: {'name' => 'joe', 'weight' => '129'})
|
58
|
+
|
59
|
+
assert_equal ['name'], version.different_attributes
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_decoded_snapshot
|
63
|
+
target = DiffRecord.new
|
64
|
+
version = DiffRecord.version_model.new(target: target, snapshot: {name: 'foo', color: 'green', weight: '4.3'})
|
65
|
+
|
66
|
+
assert_equal({"name" => "decoded: foo", "weight" => "decoded: 4.3"}, version.decoded_snapshot)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module HasVersions
|
4
|
+
module Version
|
5
|
+
class LogTest < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
def test_walk
|
8
|
+
grandparent = TestVersion.new
|
9
|
+
parent = TestVersion.new parent: grandparent
|
10
|
+
child = TestVersion.new parent: parent
|
11
|
+
|
12
|
+
ancestors = []
|
13
|
+
child.log.walk do |v|
|
14
|
+
ancestors << v
|
15
|
+
end
|
16
|
+
|
17
|
+
assert_equal [child, parent, grandparent], ancestors
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_all
|
21
|
+
grandparent = TestVersion.new
|
22
|
+
parent = TestVersion.new parent: grandparent
|
23
|
+
child = TestVersion.new parent: parent
|
24
|
+
|
25
|
+
assert_equal [child, parent, grandparent], child.log.all
|
26
|
+
assert_equal [child, parent, grandparent], child.log.to_a
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_is_enumerable
|
30
|
+
assert TestVersion.new.log.respond_to?(:collect)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/test/support/test_model.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_versions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -60,24 +60,23 @@ files:
|
|
60
60
|
- LICENSE.txt
|
61
61
|
- README.rdoc
|
62
62
|
- Rakefile
|
63
|
-
- features/has_versions.feature
|
64
|
-
- features/step_definitions/has_versions_steps.rb
|
65
|
-
- features/support/env.rb
|
66
63
|
- has_versions.gemspec
|
67
64
|
- lib/has_versions.rb
|
68
|
-
- lib/has_versions/
|
69
|
-
- lib/has_versions/
|
70
|
-
- lib/has_versions/
|
71
|
-
- lib/has_versions/
|
72
|
-
- lib/has_versions/
|
73
|
-
- lib/has_versions/
|
74
|
-
- lib/has_versions/
|
75
|
-
-
|
76
|
-
- test/has_versions/
|
77
|
-
- test/has_versions/
|
78
|
-
- test/has_versions/
|
79
|
-
- test/has_versions/
|
80
|
-
- test/has_versions/
|
65
|
+
- lib/has_versions/record.rb
|
66
|
+
- lib/has_versions/record/attributes.rb
|
67
|
+
- lib/has_versions/record/configuration.rb
|
68
|
+
- lib/has_versions/record/orm/cassandra_object.rb
|
69
|
+
- lib/has_versions/record/reset.rb
|
70
|
+
- lib/has_versions/version.rb
|
71
|
+
- lib/has_versions/version/diff.rb
|
72
|
+
- lib/has_versions/version/log.rb
|
73
|
+
- test/has_versions/record/attributes_test.rb
|
74
|
+
- test/has_versions/record/configuration_test.rb
|
75
|
+
- test/has_versions/record/reset_test.rb
|
76
|
+
- test/has_versions/record_test.rb
|
77
|
+
- test/has_versions/version/diff_test.rb
|
78
|
+
- test/has_versions/version/log_test.rb
|
79
|
+
- test/has_versions/version_test.rb
|
81
80
|
- test/helper.rb
|
82
81
|
- test/support/test_model.rb
|
83
82
|
- test/support/test_version.rb
|
@@ -107,15 +106,13 @@ signing_key:
|
|
107
106
|
specification_version: 3
|
108
107
|
summary: ActiveModel versioning
|
109
108
|
test_files:
|
110
|
-
-
|
111
|
-
-
|
112
|
-
-
|
113
|
-
- test/has_versions/
|
114
|
-
- test/has_versions/
|
115
|
-
- test/has_versions/
|
116
|
-
- test/has_versions/
|
117
|
-
- test/has_versions/version_methods/log_test.rb
|
118
|
-
- test/has_versions/versioned_test.rb
|
109
|
+
- test/has_versions/record/attributes_test.rb
|
110
|
+
- test/has_versions/record/configuration_test.rb
|
111
|
+
- test/has_versions/record/reset_test.rb
|
112
|
+
- test/has_versions/record_test.rb
|
113
|
+
- test/has_versions/version/diff_test.rb
|
114
|
+
- test/has_versions/version/log_test.rb
|
115
|
+
- test/has_versions/version_test.rb
|
119
116
|
- test/helper.rb
|
120
117
|
- test/support/test_model.rb
|
121
118
|
- test/support/test_version.rb
|
File without changes
|
data/features/support/env.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'bundler'
|
2
|
-
begin
|
3
|
-
Bundler.setup(:default, :development)
|
4
|
-
rescue Bundler::BundlerError => e
|
5
|
-
$stderr.puts e.message
|
6
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
7
|
-
exit e.status_code
|
8
|
-
end
|
9
|
-
|
10
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
|
11
|
-
require 'has_versions'
|
12
|
-
|
13
|
-
require 'rspec/expectations'
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module HasVersions
|
2
|
-
module Attributes
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
module ClassMethods
|
6
|
-
def versioned_attributes
|
7
|
-
versioning_configuration.attributes
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
def versioned_attributes
|
12
|
-
self.class.versioned_attributes.inject({}) do |memo, attribute|
|
13
|
-
memo[attribute] = versioning_encode_value(attribute, send(attribute))
|
14
|
-
memo
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module HasVersions
|
2
|
-
module Orm
|
3
|
-
module CassandraObject
|
4
|
-
def versioning_encode_value(name, value)
|
5
|
-
return if value.nil?
|
6
|
-
attribute_definitions[name.to_sym].coder.encode(value)
|
7
|
-
end
|
8
|
-
|
9
|
-
def versioning_decode_value(name, value)
|
10
|
-
return if value.nil?
|
11
|
-
attribute_definitions[name.to_sym].coder.decode(value)
|
12
|
-
end
|
13
|
-
|
14
|
-
def versioning_codable?(name)
|
15
|
-
attribute_definitions.has_key?(name.to_sym)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
data/lib/has_versions/reset.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
module HasVersions
|
2
|
-
module Reset
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
module ClassMethods
|
6
|
-
def from_version(version)
|
7
|
-
new.tap do |object|
|
8
|
-
object.reset!(version)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def reset!(version)
|
14
|
-
self.class.versioned_attributes.each do |attribute|
|
15
|
-
value = versioning_decode_value(attribute, version.snapshot[attribute])
|
16
|
-
send("#{attribute}=", value)
|
17
|
-
end
|
18
|
-
self
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module HasVersions
|
2
|
-
module Versioned
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
included do
|
6
|
-
class_attribute :versioning_configuration
|
7
|
-
class_attribute :version_class
|
8
|
-
|
9
|
-
include HasVersions::Attributes
|
10
|
-
include HasVersions::Reset
|
11
|
-
end
|
12
|
-
|
13
|
-
module ClassMethods
|
14
|
-
def has_versions(version_class, &block)
|
15
|
-
self.versioning_configuration = HasVersions::Configuration.new(&block)
|
16
|
-
self.version_class = version_class
|
17
|
-
version_class.class_eval do
|
18
|
-
include HasVersions::VersionMethods::Diff
|
19
|
-
include HasVersions::VersionMethods::Log
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def versioned_attribute(name)
|
24
|
-
self.versioning_configuration ||= HasVersions::Configuration.new
|
25
|
-
versioning_configuration.attribute(name)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def to_version
|
30
|
-
version = version_class.new
|
31
|
-
version.snapshot = versioned_attributes
|
32
|
-
version.target = self
|
33
|
-
version
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class HasVersions::AttributesTest < MiniTest::Unit::TestCase
|
4
|
-
class VersionedModel < TestModel
|
5
|
-
attr_accessor :name
|
6
|
-
attr_accessor :weight
|
7
|
-
attr_accessor :color
|
8
|
-
|
9
|
-
has_versions(Class.new) do
|
10
|
-
attribute :name
|
11
|
-
attribute :weight
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_class_versioned_attributes
|
16
|
-
assert_equal ['name', 'weight'], VersionedModel.versioned_attributes
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_instance_versioned_attributes
|
20
|
-
record = VersionedModel.new(name: 'joe', weight: 142.0, color: 'green')
|
21
|
-
expected = {'name' => 'joe', 'weight' => '142.0'}
|
22
|
-
|
23
|
-
assert_equal expected, record.versioned_attributes
|
24
|
-
end
|
25
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class HasVersions::ConfigurationTest < MiniTest::Unit::TestCase
|
4
|
-
def test_configuration
|
5
|
-
configuration = HasVersions::Configuration.new do
|
6
|
-
attribute :foo
|
7
|
-
attribute :bar
|
8
|
-
end
|
9
|
-
|
10
|
-
assert_equal %w(foo bar), configuration.attributes
|
11
|
-
end
|
12
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class HasVersions::ResetTest < MiniTest::Unit::TestCase
|
4
|
-
class ResetModel < TestModel
|
5
|
-
attr_accessor :name
|
6
|
-
attr_accessor :color
|
7
|
-
|
8
|
-
has_versions(TestVersion) do
|
9
|
-
attribute :name
|
10
|
-
end
|
11
|
-
|
12
|
-
def versioning_decode_value(name, value)
|
13
|
-
"hello, #{value}"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_from_version
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_reset
|
21
|
-
reset_model = ResetModel.new(name: "bob", color: "green")
|
22
|
-
version = TestVersion.new(snapshot: {name: "joe", color: "blue"})
|
23
|
-
|
24
|
-
reset_model.reset!(version)
|
25
|
-
|
26
|
-
assert_equal "hello, joe", reset_model.name
|
27
|
-
assert_equal "green", reset_model.color
|
28
|
-
end
|
29
|
-
end
|
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class HasVersions::VersionMethods::DiffTest < MiniTest::Unit::TestCase
|
4
|
-
class DiffModel < TestModel
|
5
|
-
attr_accessor :name
|
6
|
-
attr_accessor :color
|
7
|
-
attr_accessor :weight
|
8
|
-
|
9
|
-
has_versions(TestVersion) do
|
10
|
-
attribute :name
|
11
|
-
attribute :weight
|
12
|
-
end
|
13
|
-
|
14
|
-
def versioning_decode_value(name, value)
|
15
|
-
"decoded: #{value}"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_diff_with_parent
|
20
|
-
target = DiffModel.new
|
21
|
-
parent = TestVersion.new(target: target, snapshot: {name: 'foo', color: 'green', weight: '4.3'})
|
22
|
-
child = TestVersion.new(target: target, parent: parent, snapshot: {name: 'bar', color: 'orange', weight: '4.3'})
|
23
|
-
|
24
|
-
diff = child.diff
|
25
|
-
|
26
|
-
assert_equal({'name' => ['decoded: foo', 'decoded: bar']}, diff)
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_diff_with_parameter
|
30
|
-
target = DiffModel.new
|
31
|
-
this_version = TestVersion.new(target: target, snapshot: {name: 'foo', color: 'green', weight: '4.3'})
|
32
|
-
other_version = TestVersion.new(target: target, snapshot: {name: 'bar', color: 'orange', weight: '4.3'})
|
33
|
-
|
34
|
-
diff = this_version.diff(other_version)
|
35
|
-
|
36
|
-
assert_equal({'name' => ['decoded: bar', 'decoded: foo']}, diff)
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_diff_with_nil
|
40
|
-
target = DiffModel.new
|
41
|
-
version = TestVersion.new(target: target, snapshot: {name: 'bar', color: 'orange', weight: '4.3'})
|
42
|
-
|
43
|
-
diff = version.diff
|
44
|
-
|
45
|
-
assert_equal({'name' => [nil, 'decoded: bar'], 'weight' => [nil, 'decoded: 4.3']}, diff)
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_modified_attributes
|
49
|
-
target = DiffModel.new
|
50
|
-
version = TestVersion.new(target: target, snapshot: {'name' => 'joe', 'weight' => '129'})
|
51
|
-
|
52
|
-
assert_equal version.snapshot.keys, version.modified_attributes
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_different_attributes
|
56
|
-
target = DiffModel.new(name: 'decoded: sam', color: 'decoded: green', weight: 'decoded: 129')
|
57
|
-
version = TestVersion.new(target: target, snapshot: {'name' => 'joe', 'weight' => '129'})
|
58
|
-
|
59
|
-
assert_equal ['name'], version.different_attributes
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_decoded_snapshot
|
63
|
-
target = DiffModel.new
|
64
|
-
version = TestVersion.new(target: target, snapshot: {name: 'foo', color: 'green', weight: '4.3'})
|
65
|
-
|
66
|
-
assert_equal({"name" => "decoded: foo", "weight" => "decoded: 4.3"}, version.decoded_snapshot)
|
67
|
-
end
|
68
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class HasVersions::VersionMethods::LogTest < MiniTest::Unit::TestCase
|
4
|
-
class LogVersion < TestVersion
|
5
|
-
include HasVersions::VersionMethods::Log
|
6
|
-
end
|
7
|
-
|
8
|
-
def test_walk
|
9
|
-
grandparent = LogVersion.new
|
10
|
-
parent = LogVersion.new parent: grandparent
|
11
|
-
child = LogVersion.new parent: parent
|
12
|
-
|
13
|
-
ancestors = []
|
14
|
-
child.log.walk do |v|
|
15
|
-
ancestors << v
|
16
|
-
end
|
17
|
-
|
18
|
-
assert_equal [child, parent, grandparent], ancestors
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_all
|
22
|
-
grandparent = LogVersion.new
|
23
|
-
parent = LogVersion.new parent: grandparent
|
24
|
-
child = LogVersion.new parent: parent
|
25
|
-
|
26
|
-
assert_equal [child, parent, grandparent], child.log.all
|
27
|
-
assert_equal [child, parent, grandparent], child.log.to_a
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_is_enumerable
|
31
|
-
assert LogVersion.new.log.respond_to?(:collect)
|
32
|
-
end
|
33
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class HasVersions::VersionedTest < MiniTest::Unit::TestCase
|
4
|
-
def test_versioned_attribute
|
5
|
-
versioned_model = Class.new(TestModel) do
|
6
|
-
versioned_attribute 'hello'
|
7
|
-
end
|
8
|
-
|
9
|
-
assert_equal ['hello'], versioned_model.versioned_attributes
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_to_version
|
13
|
-
record = Class.new(TestModel) do
|
14
|
-
attr_accessor :name
|
15
|
-
|
16
|
-
has_versions(TestVersion) do
|
17
|
-
attribute :name
|
18
|
-
end
|
19
|
-
end.new(name: 'wee')
|
20
|
-
|
21
|
-
version = record.to_version
|
22
|
-
|
23
|
-
assert_kind_of TestVersion, version
|
24
|
-
assert_equal record, version.target
|
25
|
-
assert_equal record.versioned_attributes, version.snapshot
|
26
|
-
end
|
27
|
-
end
|