acts_as_serializable 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ coverage*
2
+ pkg
data/README.rdoc ADDED
@@ -0,0 +1,97 @@
1
+ = acts_as_serializable
2
+
3
+ by Birkir A. Barkarson <birkirb@stoicviking.net>
4
+
5
+ == Description
6
+
7
+ A gem/rails plugin for handling versioning of serialization methods (to_xml, to_json).
8
+ Uses the builder pattern for a one stop multi-format serialization method, which can be
9
+ versioned in-class, in a module or in seperately managed class files.
10
+
11
+ == Installation from source
12
+
13
+ git clone git://github.com/birkirb/acts_as_serializable.git
14
+ cd acts_as_serializable
15
+ rake install
16
+
17
+ === Gem Installation
18
+
19
+ gem install birkirb-acts_as_serializable --source http://gems.github.com
20
+
21
+ == Examples
22
+
23
+ First:
24
+
25
+ require 'rubygems'
26
+ require 'acts_as_serializable'
27
+
28
+
29
+ Define your own serialization method:
30
+
31
+ class SimpleClass
32
+ include Serializable
33
+ acts_as_serializable
34
+
35
+ def serialize(builder, options)
36
+ builder.root do
37
+ builder.text 'hello world'
38
+ end
39
+ end
40
+ end
41
+
42
+ SimpleClass.new.to_xml => "<root><text>hello world</text></root>"
43
+ SimpleClass.new.to_hash => {:text=>"hello world"}
44
+ SimpleClass.new.to_json => "{\"text\": \"hello world\"}"
45
+
46
+ Use versioned serialization methods:
47
+
48
+ class SimpleClass
49
+ include Serializable
50
+
51
+ def serialize_to_version_1_0(builder, options)
52
+ builder.root do
53
+ builder.text 'hello world'
54
+ end
55
+ end
56
+
57
+ def serialize_to_version_2_0(builder, options)
58
+ builder.texts do
59
+ builder.text "It's a brave new world"
60
+ end
61
+ end
62
+
63
+ acts_as_serializable
64
+ end
65
+
66
+ SimpleClass.new.to_xml => "<texts><text>It's a brave new world</text></texts>"
67
+ SimpleClass.new.to_hash(:version => '1.0') => {:text=>"hello world"}
68
+
69
+ Use classes containing versioned serialization methods:
70
+
71
+ Given a model in app/models:
72
+
73
+ class Person < ActiveRecord::Base
74
+ acts_as_serializable
75
+ end
76
+
77
+ And a serialization in app/serializations/person
78
+
79
+ module Serializable
80
+ module Person
81
+ class Version_1_1_0
82
+ def self.serialize(person, builder, options)
83
+ builder.person do
84
+ builder.name(person.name)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ Person.new(:name => "Birkir").to_xml => "<person><name>Birkir</name></person>"
92
+
93
+ == Copyright
94
+
95
+ Author:: Birkir A. Barkarson <birkirb@stoicviking.net>
96
+ Copyright:: Copyright (c) 2009
97
+ License:: MIT License
data/Rakefile ADDED
@@ -0,0 +1,135 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |s|
6
+ s.name = %q{acts_as_serializable}
7
+ s.authors = ["Birkir A. Barkarson"]
8
+ s.description = %q{Easy versioning of serialization methods}
9
+ s.summary = s.description
10
+ s.email = %q{birkirb@stoicviking.net}
11
+ s.has_rdoc = true
12
+ s.homepage = %q{http://github.com/birkirb/acts_as_serializable}
13
+ s.rubyforge_project = %q{serializable}
14
+ s.rubygems_version = %q{1.3.1}
15
+ #s.required_rubygems_version = "1.3.1"
16
+ s.add_dependency(%q<jsonbuilder>, [">= 0.0.6"])
17
+ s.add_dependency(%q<activesupport>, [">= 1.2"])
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
21
+ end
22
+
23
+ begin
24
+ require 'spec/rake/spectask'
25
+
26
+ Spec::Rake::SpecTask.new('spec') do |t|
27
+ t.spec_opts = ["-f specdoc", "-c"]
28
+ t.spec_files = FileList['spec/*_spec.rb']
29
+ end
30
+
31
+ rescue LoadError
32
+ desc 'Spec rake task not available'
33
+ task :spec do
34
+ abort 'Spec rake task is not available. Be sure to install rspec as a gem or plugin'
35
+ end
36
+ end
37
+
38
+ # begin
39
+ # require 'cucumber'
40
+ # require 'cucumber/rake/task'
41
+
42
+
43
+ # desc "Run Cucumber feature tests"
44
+ # Cucumber::Rake::Task.new(:features) do |t|
45
+ # t.cucumber_opts = "--format pretty"
46
+ # end
47
+
48
+ # rescue LoadError
49
+ # desc 'Cucumber rake task not available'
50
+ # task :features do
51
+ # abort 'Cucumber rake task is not available. Be sure to install cucumber as a gem or plugin'
52
+ # end
53
+ # end
54
+
55
+ begin
56
+ require 'spec/rake/spectask'
57
+ # require 'cucumber'
58
+ # require 'cucumber/rake/task'
59
+ require 'spec/rake/verify_rcov'
60
+
61
+ task :test do
62
+ Rake::Task[:spec].invoke
63
+ Rake::Task[:features].invoke
64
+ end
65
+
66
+ desc "Run tests with RCov"
67
+ namespace :rcov do
68
+ rm "coverage.data" if File.exist?("coverage.data")
69
+
70
+ # desc "Run Features with RCov"
71
+ # Cucumber::Rake::Task.new(:features) do |t|
72
+ # t.rcov = true
73
+ # t.rcov_opts = %w{ --exclude osx\/objc,gems\/,spec\/,features\/ --aggregate coverage.data}
74
+ # t.rcov_opts << %[-o "coverage"]
75
+ # end
76
+
77
+ Spec::Rake::SpecTask.new(:spec) do |t|
78
+ t.spec_opts = ["-f specdoc", "-c"]
79
+ t.spec_files = FileList['spec/*_spec.rb']
80
+ t.rcov = true
81
+ t.rcov_opts = %w{--exclude "spec/*,gems/*,features/*" --aggregate "coverage.data"}
82
+ end
83
+
84
+ desc "Run both specs and features to generate aggregated coverage"
85
+ task :all do |t|
86
+ Rake::Task["rcov:spec"].invoke
87
+ # Rake::Task["rcov:features"].invoke
88
+ end
89
+
90
+ RCov::VerifyTask.new(:verify => 'rcov:all') do |t|
91
+ t.threshold = 100.0
92
+ t.index_html = 'coverage/index.html'
93
+ end
94
+ end
95
+ rescue LoadError
96
+ desc 'Rcov rake task not available'
97
+ task :rcov do
98
+ abort 'rcov rake task is not available. Be sure to install rspec, rcov and cucumber as a gem or plugin'
99
+ end
100
+ end
101
+
102
+ begin
103
+ require 'metric_fu'
104
+
105
+ MetricFu::Configuration.run do |config|
106
+ #define which metrics you want to use
107
+ config.metrics = [:churn, :flog, :flay, :reek, :roodi]
108
+ config.flay = { :dirs_to_flay => ['app', 'lib'] }
109
+ config.flog = { :dirs_to_flog => ['app', 'lib'] }
110
+ config.reek = { :dirs_to_reek => ['app', 'lib'] }
111
+ config.roodi = { :dirs_to_roodi => ['app', 'lib'] }
112
+ config.saikuro = { :output_directory => 'scratch_directory/saikuro',
113
+ :input_directory => ['app', 'lib'],
114
+ :cyclo => "",
115
+ :filter_cyclo => "0",
116
+ :warn_cyclo => "5",
117
+ :error_cyclo => "7",
118
+ :formater => "text"} #this needs to be set to "text"
119
+ config.churn = { :start_date => "1 year ago", :minimum_churn_count => 10}
120
+ config.rcov = { :test_files => ['test/**/*_test.rb',
121
+ 'spec/**/*_spec.rb'],
122
+ :rcov_opts => ["--sort coverage",
123
+ "--no-html",
124
+ "--text-coverage",
125
+ "--no-color",
126
+ "--profile",
127
+ "--rails",
128
+ "--exclude /gems/,/Library/,spec"]}
129
+ end
130
+
131
+ rescue LoadError
132
+ # Too bad
133
+ end
134
+
135
+ task :default => [:test]
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,71 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{acts_as_serializable}
5
+ s.version = "0.1.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Birkir A. Barkarson"]
9
+ s.date = %q{2009-06-11}
10
+ s.description = %q{Easy versioning of serialization methods}
11
+ s.email = %q{birkirb@stoicviking.net}
12
+ s.extra_rdoc_files = [
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".gitignore",
17
+ "README.rdoc",
18
+ "Rakefile",
19
+ "VERSION",
20
+ "acts_as_serializable.gemspec",
21
+ "init.rb",
22
+ "lib/acts_as_serializable.rb",
23
+ "lib/version.rb",
24
+ "lib/versions.rb",
25
+ "spec/acts_as_serializable_spec.rb",
26
+ "spec/app/serializations/test_rails_model/test_rails_model/version_1_0_0.rb",
27
+ "spec/app/serializations/test_rails_model/test_rails_model/version_1_5.rb",
28
+ "spec/app/serializations/test_rails_model/test_rails_model/version_2_1.rb",
29
+ "spec/serializations/test_model/version_1_0_0.rb",
30
+ "spec/serializations/test_model/version_1_5.rb",
31
+ "spec/serializations/test_model/version_2_1.rb",
32
+ "spec/spec_helper.rb",
33
+ "spec/version_spec.rb",
34
+ "spec/versions_spec.rb"
35
+ ]
36
+ s.has_rdoc = true
37
+ s.homepage = %q{http://github.com/birkirb/acts_as_serializable}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubyforge_project = %q{serializable}
41
+ s.rubygems_version = %q{1.3.1}
42
+ s.summary = %q{Easy versioning of serialization methods}
43
+ s.test_files = [
44
+ "spec/spec_helper.rb",
45
+ "spec/acts_as_serializable_spec.rb",
46
+ "spec/versions_spec.rb",
47
+ "spec/version_spec.rb",
48
+ "spec/serializations/test_model/version_1_0_0.rb",
49
+ "spec/serializations/test_model/version_2_1.rb",
50
+ "spec/serializations/test_model/version_1_5.rb",
51
+ "spec/app/serializations/test_rails_model/test_rails_model/version_1_0_0.rb",
52
+ "spec/app/serializations/test_rails_model/test_rails_model/version_2_1.rb",
53
+ "spec/app/serializations/test_rails_model/test_rails_model/version_1_5.rb"
54
+ ]
55
+
56
+ if s.respond_to? :specification_version then
57
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
58
+ s.specification_version = 2
59
+
60
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
61
+ s.add_runtime_dependency(%q<jsonbuilder>, [">= 0.0.6"])
62
+ s.add_runtime_dependency(%q<activesupport>, [">= 1.2"])
63
+ else
64
+ s.add_dependency(%q<jsonbuilder>, [">= 0.0.6"])
65
+ s.add_dependency(%q<activesupport>, [">= 1.2"])
66
+ end
67
+ else
68
+ s.add_dependency(%q<jsonbuilder>, [">= 0.0.6"])
69
+ s.add_dependency(%q<activesupport>, [">= 1.2"])
70
+ end
71
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'acts_as_serializable'
2
+ ActiveRecord::Base.send(:include, Serializable)
@@ -0,0 +1,123 @@
1
+ require 'builder/xmlmarkup'
2
+ require 'jsonbuilder'
3
+ require 'active_support'
4
+ require 'find'
5
+
6
+ module Serializable
7
+ SERIALIZE_TO_VERSION_REGEXP = /^serialize_to_version_((:?\d+_?)+)$/
8
+ SERIALIZED_CLASS_NAME_REGEXP = /\/version_((:?\d+_?)+)\.rb$/
9
+
10
+ def self.included(base)
11
+ base.extend(ClassMethods)
12
+ end
13
+
14
+ module ClassMethods
15
+ def acts_as_serializable
16
+ include Serializable::InstanceMethods
17
+ extend Serializable::SingletonMethods
18
+ @serialization_versions = Versions.new
19
+ find_local_serialization_methods
20
+ if defined?(RAILS_ROOT)
21
+ # Rails plugin usage
22
+ scan_rails_app_paths.each do |path|
23
+ find_project_serialization_classes(path)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ module SingletonMethods
30
+ def find_project_serialization_classes(project_path)
31
+ klass_name = self.name
32
+ serialization_directory = File.join(project_path, 'serializations', klass_name.underscore)
33
+ Find.find(serialization_directory) do |path|
34
+ if File.file?(path) && versioned_klass = path.match(SERIALIZED_CLASS_NAME_REGEXP)
35
+ require path
36
+ klass = Serializable.const_get("#{klass_name}").const_get("Version_#{versioned_klass[1]}")
37
+ if klass && klass.respond_to?(:serialize)
38
+ define_local_serialization_method(versioned_klass[1])
39
+ end
40
+ end
41
+ end
42
+ @default_serialization_version = @serialization_versions.last
43
+ end
44
+
45
+ def serialization_versions
46
+ @serialization_versions
47
+ end
48
+
49
+ def default_serialization_version
50
+ @default_serialization_version
51
+ end
52
+
53
+ def default_serialization_version=(new_version)
54
+ if new_version.is_a?(Version)
55
+ @default_serialization_version = new_version
56
+ else
57
+ @default_serialization_version = Version.new(new_version)
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def find_local_serialization_methods
64
+ self.instance_methods.each do |method_name|
65
+ if method_name = method_name.match(SERIALIZE_TO_VERSION_REGEXP)
66
+ @serialization_versions << Version.new(method_name[1])
67
+ end
68
+ end
69
+ @default_serialization_version = @serialization_versions.last
70
+ end
71
+
72
+ def define_local_serialization_method(method_version)
73
+ class_eval <<-EOV
74
+ def serialize_to_version_#{method_version}(builder, options)
75
+ Serializable::#{self.name}::Version_#{method_version}.serialize(self, builder, options)
76
+ end
77
+ EOV
78
+ @serialization_versions << Version.new(method_version)
79
+ end
80
+
81
+ def scan_rails_app_paths
82
+ project_paths = Array.new
83
+ $LOAD_PATH.each do |path|
84
+ if path.match(/#{Regexp.escape(RAILS_ROOT)}.*\/app$/)
85
+ project_paths << path
86
+ end
87
+ end
88
+ project_paths
89
+ end
90
+ end
91
+
92
+ # This module contains instance methods
93
+ module InstanceMethods
94
+ def to_hash(options = {})
95
+ serialize(Builder::Hash.new, options)
96
+ end
97
+
98
+ def to_json(options = {})
99
+ serialize(Builder::Json.new, options)
100
+ end
101
+
102
+ def to_xml(options = {})
103
+ serialize(Builder::XmlMarkup.new, options)
104
+ end
105
+
106
+ def serialize(builder, options = {})
107
+ if version_number = options[:version]
108
+ if version = self.class.serialization_versions.find_version(Version.new(version_number))
109
+ return self.send("serialize_to_version_#{version.to_s_underscored}", builder, options)
110
+ else
111
+ raise "Version #{version_number} given but no serialization method found"
112
+ end
113
+ else
114
+ if version = self.class.default_serialization_version
115
+ return self.send("serialize_to_version_#{version.to_s_underscored}", builder, options)
116
+ else
117
+ raise "No serialization method found"
118
+ end
119
+ end
120
+ end
121
+ end
122
+
123
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,83 @@
1
+ module Serializable
2
+ class Version
3
+
4
+ def initialize(version_string)
5
+ version_string = version_string.gsub('_','.')
6
+ @levels = version_string.split('.')
7
+
8
+ @levels.each_with_index do |number, index|
9
+ @levels[index] = number.to_i
10
+ end
11
+ end
12
+
13
+ def major
14
+ get_level(0)
15
+ end
16
+
17
+ def minor
18
+ get_level(1)
19
+ end
20
+
21
+ def build
22
+ get_level(2)
23
+ end
24
+
25
+ def revision
26
+ get_level(3)
27
+ end
28
+
29
+ def <(rhs)
30
+ (self <=> rhs) < 0
31
+ end
32
+
33
+ def >(rhs)
34
+ (self <=> rhs) > 0
35
+ end
36
+
37
+ def ==(rhs)
38
+ (self <=> rhs) == 0
39
+ end
40
+
41
+ def <=>(rhs)
42
+ max_levels = @levels.size - 1
43
+ max_levels = rhs.levels.size - 1 if rhs.levels.size > max_levels
44
+
45
+ (0..max_levels).each do |level|
46
+ lhs_level = @levels[level] || 0
47
+ rhs_level = rhs.levels[level] || 0
48
+
49
+ if lhs_level == rhs_level
50
+ next
51
+ else
52
+ return lhs_level <=> rhs_level
53
+ end
54
+ end
55
+ 0 # Ended on an equality
56
+ end
57
+
58
+ def to_s(seperator = '.')
59
+ @levels.join(seperator)
60
+ end
61
+
62
+ def to_s_underscored
63
+ to_s('_')
64
+ end
65
+
66
+ protected
67
+
68
+ def levels
69
+ @levels
70
+ end
71
+
72
+ private
73
+
74
+ def get_level(level)
75
+ if @levels.size > level
76
+ @levels[level]
77
+ else
78
+ 0
79
+ end
80
+ end
81
+
82
+ end
83
+ end
data/lib/versions.rb ADDED
@@ -0,0 +1,31 @@
1
+ module Serializable
2
+ class Versions < Array
3
+
4
+ def <<(object)
5
+ super(object)
6
+ self.sort!
7
+ end
8
+
9
+ def push(object)
10
+ super(object)
11
+ self.sort!
12
+ end
13
+
14
+ def find_version(seeking_version)
15
+ previous_version = nil
16
+
17
+ if seeking_version.is_a?(Version)
18
+ self.each do |version|
19
+ if version < seeking_version
20
+ previous_version = version
21
+ elsif version > seeking_version
22
+ return previous_version
23
+ else
24
+ return version
25
+ end
26
+ end
27
+ end
28
+ previous_version
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,184 @@
1
+ require 'spec/spec_helper'
2
+
3
+ class SerializableObject
4
+ include Serializable
5
+
6
+ def serialize_to_version_1_0_0(builder, options)
7
+ "This is version 1.0.0"
8
+ end
9
+
10
+ def serialize_to_version_1_5(builder, options)
11
+ "This is version 1.5.0"
12
+ end
13
+
14
+ def serialize_to_version_2_1(builder, options)
15
+ "This is version 2.1"
16
+ end
17
+
18
+ acts_as_serializable
19
+ end
20
+
21
+ class WithSerialize < SerializableObject
22
+ include Serializable
23
+ acts_as_serializable
24
+
25
+ def serialize(builder, options)
26
+ builder
27
+ end
28
+ end
29
+
30
+ class NoSerializationObject
31
+ include Serializable
32
+ acts_as_serializable
33
+ end
34
+
35
+ class TestModel
36
+ include Serializable
37
+ acts_as_serializable
38
+ find_project_serialization_classes(File.join(File.dirname(__FILE__)))
39
+ end
40
+
41
+ ::RAILS_ROOT = File.join(File.dirname(__FILE__))
42
+ $LOAD_PATH.push(File.join(::RAILS_ROOT, 'app'))
43
+
44
+ class TestRailsModel
45
+ include Serializable
46
+ acts_as_serializable
47
+ end
48
+
49
+ describe Serializable, 'if included in a class, then that class' do
50
+
51
+ it 'should respond to #to_xml(options)' do
52
+ klass = SerializableObject.new
53
+ klass.respond_to?("to_xml").should be_true
54
+ end
55
+
56
+ it 'should respond to #to_json(options)' do
57
+ klass = SerializableObject.new
58
+ klass.respond_to?("to_hash").should be_true
59
+ end
60
+
61
+ it 'should respond to #to_hash(options)' do
62
+ klass = SerializableObject.new
63
+ klass.respond_to?("to_json").should be_true
64
+ end
65
+
66
+ context 'with the serialized method overriden to return the builder' do
67
+
68
+ it '#to_xml should return an Builder::XmlMarkup' do
69
+ klass = WithSerialize.new
70
+ klass.to_xml.test.should == "<test/>"
71
+ end
72
+
73
+ it '#to_hash should return an Builder::Hash' do
74
+ klass = WithSerialize.new
75
+ klass.to_hash.test('value').should == 'value'
76
+ end
77
+
78
+ it '#to_json should return an Builder::Json' do
79
+ klass = WithSerialize.new
80
+ klass.to_json.test('value').should == '"value"'
81
+ end
82
+ end
83
+
84
+ end
85
+
86
+ describe Serializable, 'when included in a class that has multiple versioned serialization methods' do
87
+
88
+ context 'and a to_format method is called with a version option' do
89
+
90
+ it 'should execute that exact versioned serialization assuming it exists' do
91
+ klass = SerializableObject.new
92
+
93
+ klass.to_xml(:version => '1.0.0').should == "This is version 1.0.0"
94
+ klass.to_json(:version => '1.5.0').should == "This is version 1.5.0"
95
+ klass.to_hash(:version => '2.1.0').should == "This is version 2.1"
96
+ end
97
+
98
+ it 'should execute a lower version if an exact one does not exist' do
99
+ klass = SerializableObject.new
100
+
101
+ klass.to_xml(:version => '1.4.0').should == "This is version 1.0.0"
102
+ klass.to_json(:version => '3.1.0').should == "This is version 2.1"
103
+ end
104
+
105
+ it 'should raise an error if no corresponding version exists' do
106
+ klass = SerializableObject.new
107
+
108
+ lambda { klass.to_hash(:version => '0.1.0') }.should raise_error(RuntimeError, "Version 0.1.0 given but no serialization method found")
109
+ end
110
+ end
111
+
112
+ context 'and a to_format method is called with no version option' do
113
+
114
+ it 'should default to the most recent version if no global default is set' do
115
+ klass = SerializableObject.new
116
+
117
+ klass.to_xml.should == "This is version 2.1"
118
+ end
119
+
120
+ it 'should throw an error if no serialization method exists' do
121
+ klass = NoSerializationObject.new
122
+
123
+ lambda { klass.to_xml }.should raise_error(RuntimeError, "No serialization method found")
124
+ end
125
+
126
+ it 'should use the default method if it is set' do
127
+ SerializableObject.default_serialization_version = Serializable::Version.new('1.5')
128
+ klass = SerializableObject.new
129
+
130
+ klass.to_xml.should == "This is version 1.5.0"
131
+ end
132
+ end
133
+ end
134
+
135
+ describe Serializable, 'when included in a class that has multiple serialization classes' do
136
+
137
+ context 'and a to_format method is called with a version option' do
138
+
139
+ it 'should execute that exact versioned serialization assuming it exists' do
140
+ klass = TestModel.new
141
+
142
+ klass.to_xml(:version => '1.0.0').should == "This is version 1.0.0 for TestModel"
143
+ klass.to_json(:version => '1.5.0').should == "This is version 1.5.0 for TestModel"
144
+ klass.to_hash(:version => '2.1.0').should == "This is version 2.1 for TestModel"
145
+ end
146
+
147
+ it 'should execute a lower version if an exact one does not exist' do
148
+ klass = TestModel.new
149
+
150
+ klass.to_xml(:version => '1.4.0').should == "This is version 1.0.0 for TestModel"
151
+ klass.to_json(:version => '3.1.0').should == "This is version 2.1 for TestModel"
152
+ end
153
+
154
+ it 'should raise an error if no corresponding version exists' do
155
+ klass = TestModel.new
156
+
157
+ lambda { klass.to_hash(:version => '0.1.0') }.should raise_error(RuntimeError, "Version 0.1.0 given but no serialization method found")
158
+ end
159
+ end
160
+
161
+ context 'and a to_format method is called with no version option' do
162
+
163
+ it 'should default to the most recent version if no global default is set' do
164
+ klass = TestModel.new
165
+
166
+ klass.to_xml.should == "This is version 2.1 for TestModel"
167
+ end
168
+
169
+ it 'should use the default method if it is set' do
170
+ TestModel.default_serialization_version = "1.5"
171
+ klass = TestModel.new
172
+
173
+ klass.to_xml.should == "This is version 1.5.0 for TestModel"
174
+ end
175
+
176
+ it 'should auto find version classes for Rails applications' do
177
+ klass = TestRailsModel.new
178
+
179
+ klass.to_xml(:version => '1.0.0').should == "This is version 1.0.0 for TestRailsModel"
180
+ klass.to_json(:version => '1.5.0').should == "This is version 1.5.0 for TestRailsModel"
181
+ klass.to_hash(:version => '2.1.0').should == "This is version 2.1 for TestRailsModel"
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,9 @@
1
+ module Serializable
2
+ module TestRailsModel
3
+ class Version_1_0_0
4
+ def self.serialize(test_model, builder, options)
5
+ "This is version 1.0.0 for #{test_model.class.name}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Serializable
2
+ module TestRailsModel
3
+ class Version_1_5
4
+ def self.serialize(test_model, builder, options)
5
+ "This is version 1.5.0 for #{test_model.class.name}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Serializable
2
+ module TestRailsModel
3
+ class Version_2_1
4
+ def self.serialize(test_model, builder, options)
5
+ "This is version 2.1 for #{test_model.class.name}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Serializable
2
+ module TestModel
3
+ class Version_1_0_0
4
+ def self.serialize(test_model, builder, options)
5
+ "This is version 1.0.0 for #{test_model.class.name}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Serializable
2
+ module TestModel
3
+ class Version_1_5
4
+ def self.serialize(test_model, builder, options)
5
+ "This is version 1.5.0 for #{test_model.class.name}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Serializable
2
+ module TestModel
3
+ class Version_2_1
4
+ def self.serialize(test_model, builder, options)
5
+ "This is version 2.1 for #{test_model.class.name}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ require 'lib/version'
5
+ require 'lib/versions'
6
+ require 'lib/acts_as_serializable'
@@ -0,0 +1,75 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe Serializable::Version, 'when created with a version numeric string' do
4
+
5
+ it 'should report the correct major, minor, build and revision numbers.' do
6
+ version = Serializable::Version.new('1.2.3.4')
7
+ version.major.should == 1
8
+ version.minor.should == 2
9
+ version.build.should == 3
10
+ version.revision.should == 4
11
+ end
12
+
13
+ it '#to_s should give the same version string.' do
14
+ version = Serializable::Version.new('2.1.2')
15
+ version.to_s.should == '2.1.2'
16
+ end
17
+
18
+ it '#to_s_underscored should give an underscored version string.' do
19
+ version = Serializable::Version.new('2.1.2')
20
+ version.to_s_underscored.should == '2_1_2'
21
+ end
22
+
23
+ it 'should give 0 for all numbers not specified.' do
24
+ version = Serializable::Version.new('3')
25
+ version.major.should == 3
26
+ version.minor.should == 0
27
+ version.build.should == 0
28
+ version.revision.should == 0
29
+ end
30
+
31
+ it 'should support underscored versions' do
32
+ version = Serializable::Version.new('3_1_2_4')
33
+ version.major.should == 3
34
+ version.minor.should == 1
35
+ version.build.should == 2
36
+ version.revision.should == 4
37
+ end
38
+ end
39
+
40
+ describe Serializable::Version, 'when comparing two instances' do
41
+
42
+ it 'should correctly compare v1.0.0.1 > v.1.0.0.0' do
43
+ Serializable::Version.new('1.0.0.1').should > Serializable::Version.new('1.0')
44
+ Serializable::Version.new('1.0.0.1').should_not < Serializable::Version.new('1.0')
45
+ Serializable::Version.new('1.0.0.1').should_not == Serializable::Version.new('1.0')
46
+ end
47
+
48
+ it 'should correctly compare v1.2.0.3 < v2.2.0.3' do
49
+ Serializable::Version.new('1.2.0.3').should < Serializable::Version.new('2.2.0.3')
50
+ Serializable::Version.new('1.2.0.3').should_not > Serializable::Version.new('2.2.0.3')
51
+ Serializable::Version.new('1.2.0.3').should_not == Serializable::Version.new('2.2.0.3')
52
+ end
53
+
54
+ it 'should correctly compare v2.0.0.0.0.0.0 == v2' do
55
+ Serializable::Version.new('2.0.0.0.0.0.0.0').should == Serializable::Version.new('2')
56
+ Serializable::Version.new('2.0.0.0.0.0.0.0').should_not < Serializable::Version.new('2')
57
+ Serializable::Version.new('2.0.0.0.0.0.0.0').should_not > Serializable::Version.new('2')
58
+ end
59
+ end
60
+
61
+ describe Serializable::Version, 'when in an Array' do
62
+
63
+ it 'should be sortable' do
64
+ version_strings_unordered = ['1.20.0', '1.0.2', '1.0.0', '1.4.0', '1.0.1', '2', '1.0.10', '2.0.1', '1.0']
65
+ version_strings_ordered = ['1.0', '1.0.0', '1.0.1', '1.0.2', '1.0.10', '1.4.0', '1.20.0', '2', '2.0.1']
66
+ versions = Array.new
67
+ version_strings_unordered.each do |s|
68
+ versions << Serializable::Version.new(s)
69
+ end
70
+
71
+ versions.sort.each_with_index do |version, index|
72
+ version.to_s.should == version_strings_ordered[index]
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe Serializable::Versions do
4
+
5
+ it '#find_version should find a Version equal or less than the Version given ' do
6
+ version_strings_unordered = ['1.20.0', '1.0.2', '1.0.0', '1.4.0', '1.0.1', '2', '1.0.10', '2.0.1', '1.0']
7
+ versions = Serializable::Versions.new
8
+ version_strings_unordered.each do |s|
9
+ versions << Serializable::Version.new(s)
10
+ end
11
+
12
+ versions.push(Serializable::Version.new('1.3')) # Hmm..
13
+
14
+ versions.find_version(Serializable::Version.new('1.30')).should == Serializable::Version.new('1.20.0')
15
+ versions.find_version(Serializable::Version.new('1.15')).should == Serializable::Version.new('1.4.0')
16
+ versions.find_version(Serializable::Version.new('1.0.2')).should == Serializable::Version.new('1.0.2')
17
+ versions.find_version(Serializable::Version.new('3.1.5')).should == Serializable::Version.new('2.0.1')
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_serializable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Birkir A. Barkarson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-11 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: jsonbuilder
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.6
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "1.2"
34
+ version:
35
+ description: Easy versioning of serialization methods
36
+ email: birkirb@stoicviking.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ files:
44
+ - .gitignore
45
+ - README.rdoc
46
+ - Rakefile
47
+ - VERSION
48
+ - acts_as_serializable.gemspec
49
+ - init.rb
50
+ - lib/acts_as_serializable.rb
51
+ - lib/version.rb
52
+ - lib/versions.rb
53
+ - spec/acts_as_serializable_spec.rb
54
+ - spec/app/serializations/test_rails_model/test_rails_model/version_1_0_0.rb
55
+ - spec/app/serializations/test_rails_model/test_rails_model/version_1_5.rb
56
+ - spec/app/serializations/test_rails_model/test_rails_model/version_2_1.rb
57
+ - spec/serializations/test_model/version_1_0_0.rb
58
+ - spec/serializations/test_model/version_1_5.rb
59
+ - spec/serializations/test_model/version_2_1.rb
60
+ - spec/spec_helper.rb
61
+ - spec/version_spec.rb
62
+ - spec/versions_spec.rb
63
+ has_rdoc: true
64
+ homepage: http://github.com/birkirb/acts_as_serializable
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --charset=UTF-8
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ requirements: []
83
+
84
+ rubyforge_project: serializable
85
+ rubygems_version: 1.3.1
86
+ signing_key:
87
+ specification_version: 2
88
+ summary: Easy versioning of serialization methods
89
+ test_files:
90
+ - spec/spec_helper.rb
91
+ - spec/acts_as_serializable_spec.rb
92
+ - spec/versions_spec.rb
93
+ - spec/version_spec.rb
94
+ - spec/serializations/test_model/version_1_0_0.rb
95
+ - spec/serializations/test_model/version_2_1.rb
96
+ - spec/serializations/test_model/version_1_5.rb
97
+ - spec/app/serializations/test_rails_model/test_rails_model/version_1_0_0.rb
98
+ - spec/app/serializations/test_rails_model/test_rails_model/version_2_1.rb
99
+ - spec/app/serializations/test_rails_model/test_rails_model/version_1_5.rb