acts_as_serializable 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -8,12 +8,18 @@ A gem/rails plugin for handling versioning of serialization methods (to_xml, to_
8
8
  Uses the builder pattern for a one stop multi-format serialization method, which can be
9
9
  versioned in-class, in a module or in seperately managed class files.
10
10
 
11
+ Works with Builder::XmlMarkup <= 2.1.2
12
+
11
13
  == Installation from source
12
14
 
13
15
  git clone git://github.com/birkirb/acts_as_serializable.git
14
16
  cd acts_as_serializable
15
17
  rake install
16
18
 
19
+ === Rails Plugin Installation
20
+
21
+ script/plugin install git://github.com/birkirb/acts_as_serializable.git
22
+
17
23
  === Gem Installation
18
24
 
19
25
  gem install birkirb-acts_as_serializable --source http://gems.github.com
data/Rakefile CHANGED
@@ -12,8 +12,8 @@ begin
12
12
  s.homepage = %q{http://github.com/birkirb/acts_as_serializable}
13
13
  s.rubyforge_project = %q{serializable}
14
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"])
15
+ s.add_dependency(%q<builder>, ["<= 2.1.2"])
16
+ s.add_dependency(%q<jsonbuilder>, [">= 0.1.0"])
17
17
  s.add_dependency(%q<activesupport>, [">= 1.2"])
18
18
  end
19
19
  rescue LoadError
@@ -21,13 +21,12 @@ rescue LoadError
21
21
  end
22
22
 
23
23
  begin
24
- require 'spec/rake/spectask'
24
+ require 'rspec/core/rake_task'
25
25
 
26
- Spec::Rake::SpecTask.new('spec') do |t|
27
- t.spec_opts = ["-f specdoc", "-c"]
28
- t.spec_files = FileList['spec/*_spec.rb']
26
+ RSpec::Core::RakeTask.new('spec') do |t|
27
+ t.rspec_opts = ["-fd", "-c"]
28
+ t.pattern = 'spec/**/*_spec.rb'
29
29
  end
30
-
31
30
  rescue LoadError
32
31
  desc 'Spec rake task not available'
33
32
  task :spec do
@@ -35,44 +34,18 @@ rescue LoadError
35
34
  end
36
35
  end
37
36
 
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
37
  begin
56
38
  require 'spec/rake/spectask'
57
- # require 'cucumber'
58
- # require 'cucumber/rake/task'
59
39
  require 'spec/rake/verify_rcov'
60
40
 
61
41
  task :test do
62
42
  Rake::Task[:spec].invoke
63
- # Rake::Task[:features].invoke
64
43
  end
65
44
 
66
45
  desc "Run tests with RCov"
67
46
  namespace :rcov do
68
47
  rm "coverage.data" if File.exist?("coverage.data")
69
48
 
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
49
 
77
50
  Spec::Rake::SpecTask.new(:spec) do |t|
78
51
  t.spec_opts = ["-f specdoc", "-c"]
@@ -84,7 +57,6 @@ begin
84
57
  desc "Run both specs and features to generate aggregated coverage"
85
58
  task :all do |t|
86
59
  Rake::Task["rcov:spec"].invoke
87
- # Rake::Task["rcov:features"].invoke
88
60
  end
89
61
 
90
62
  RCov::VerifyTask.new(:verify => 'rcov:all') do |t|
@@ -132,4 +104,4 @@ rescue LoadError
132
104
  # Too bad
133
105
  end
134
106
 
135
- task :default => [:test]
107
+ task :default => [:spec]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.2.0
@@ -1,71 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
1
4
  # -*- encoding: utf-8 -*-
2
5
 
3
6
  Gem::Specification.new do |s|
4
7
  s.name = %q{acts_as_serializable}
5
- s.version = "0.1.6"
8
+ s.version = "0.2.0"
6
9
 
7
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
11
  s.authors = ["Birkir A. Barkarson"]
9
- s.date = %q{2009-06-18}
12
+ s.date = %q{2011-09-20}
10
13
  s.description = %q{Easy versioning of serialization methods}
11
14
  s.email = %q{birkirb@stoicviking.net}
12
15
  s.extra_rdoc_files = [
13
16
  "README.rdoc"
14
17
  ]
15
18
  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/serializable/version.rb",
24
- "lib/serializable/versions.rb",
25
- "spec/acts_as_serializable_spec.rb",
26
- "spec/app/serializations/test_rails_model/version_1_0_0.rb",
27
- "spec/app/serializations/test_rails_model/version_1_5.rb",
28
- "spec/app/serializations/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"
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "acts_as_serializable.gemspec",
23
+ "init.rb",
24
+ "lib/acts_as_serializable.rb",
25
+ "lib/serializable/version.rb",
26
+ "lib/serializable/versions.rb",
27
+ "lib/serializable/xbuilder.rb",
28
+ "spec/acts_as_serializable_spec.rb",
29
+ "spec/app/serializations/test_rails_model/version_1_0_0.rb",
30
+ "spec/app/serializations/test_rails_model/version_1_5.rb",
31
+ "spec/app/serializations/test_rails_model/version_2_1.rb",
32
+ "spec/serializations/test_model/version_1_0_0.rb",
33
+ "spec/serializations/test_model/version_1_5.rb",
34
+ "spec/serializations/test_model/version_2_1.rb",
35
+ "spec/spec_helper.rb",
36
+ "spec/version_spec.rb",
37
+ "spec/versions_spec.rb"
35
38
  ]
36
- s.has_rdoc = true
37
39
  s.homepage = %q{http://github.com/birkirb/acts_as_serializable}
38
- s.rdoc_options = ["--charset=UTF-8"]
39
40
  s.require_paths = ["lib"]
40
41
  s.rubyforge_project = %q{serializable}
41
- s.rubygems_version = %q{1.3.1}
42
+ s.rubygems_version = %q{1.3.7}
42
43
  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/version_1_0_0.rb",
52
- "spec/app/serializations/test_rails_model/version_2_1.rb",
53
- "spec/app/serializations/test_rails_model/version_1_5.rb"
54
- ]
55
44
 
56
45
  if s.respond_to? :specification_version then
57
46
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
58
- s.specification_version = 2
47
+ s.specification_version = 3
59
48
 
60
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
61
- s.add_runtime_dependency(%q<jsonbuilder>, [">= 0.0.6"])
49
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
+ s.add_runtime_dependency(%q<builder>, ["<= 2.1.2"])
51
+ s.add_runtime_dependency(%q<jsonbuilder>, [">= 0.1.0"])
62
52
  s.add_runtime_dependency(%q<activesupport>, [">= 1.2"])
63
53
  else
64
- s.add_dependency(%q<jsonbuilder>, [">= 0.0.6"])
54
+ s.add_dependency(%q<builder>, ["<= 2.1.2"])
55
+ s.add_dependency(%q<jsonbuilder>, [">= 0.1.0"])
65
56
  s.add_dependency(%q<activesupport>, [">= 1.2"])
66
57
  end
67
58
  else
68
- s.add_dependency(%q<jsonbuilder>, [">= 0.0.6"])
59
+ s.add_dependency(%q<builder>, ["<= 2.1.2"])
60
+ s.add_dependency(%q<jsonbuilder>, [">= 0.1.0"])
69
61
  s.add_dependency(%q<activesupport>, [">= 1.2"])
70
62
  end
71
63
  end
64
+
data/init.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require 'acts_as_serializable'
2
+ require 'serializable/xbuilder'
2
3
  ActiveRecord::Base.send(:include, Serializable)
@@ -1,6 +1,7 @@
1
1
  require 'jsonbuilder'
2
2
  require 'active_support'
3
3
  require 'find'
4
+ require 'serializable/version'
4
5
 
5
6
  module Serializable
6
7
  SERIALIZE_TO_VERSION_REGEXP = /^serialize_to_version_((:?\d+_?)+)$/
@@ -42,13 +43,16 @@ module Serializable
42
43
  module SingletonMethods
43
44
  def find_project_serialization_classes(project_path)
44
45
  klass_name = self.name
45
- serialization_directory = File.join(project_path, 'serializations', klass_name.underscore)
46
- Find.find(serialization_directory) do |path|
47
- if File.file?(path) && versioned_klass = path.match(SERIALIZED_CLASS_NAME_REGEXP)
48
- require path
49
- klass = Serializations.const_get("#{klass_name}").const_get("Version_#{versioned_klass[1]}")
50
- if klass && klass.respond_to?(:serialize)
51
- define_local_serialization_method(versioned_klass[1])
46
+ serialization_directory = File.expand_path(File.join(project_path, 'serializations', klass_name.underscore))
47
+ puts serialization_directory.inspect
48
+ if File.exist?(serialization_directory)
49
+ Find.find(serialization_directory) do |path|
50
+ if File.file?(path) && versioned_klass = path.match(SERIALIZED_CLASS_NAME_REGEXP)
51
+ require path
52
+ klass = Serializations.const_get("#{klass_name}").const_get("Version_#{versioned_klass[1]}")
53
+ if klass && klass.respond_to?(:serialize)
54
+ define_local_serialization_method(versioned_klass[1])
55
+ end
52
56
  end
53
57
  end
54
58
  end
@@ -75,7 +79,7 @@ module Serializable
75
79
 
76
80
  def find_local_serialization_methods
77
81
  self.instance_methods.each do |method_name|
78
- if method_name = method_name.match(SERIALIZE_TO_VERSION_REGEXP)
82
+ if method_name = SERIALIZE_TO_VERSION_REGEXP.match(method_name)
79
83
  @serialization_versions << Version.new(method_name[1])
80
84
  end
81
85
  end
@@ -83,9 +87,9 @@ module Serializable
83
87
  end
84
88
 
85
89
  def define_local_serialization_method(method_version)
86
- class_eval <<-EOV
87
- def serialize_to_version_#{method_version}(builder, options)
88
- Serializations::#{self.name}::Version_#{method_version}.serialize(self, builder, options)
90
+ class_eval(<<-EOV, __FILE__, __LINE__)
91
+ def serialize_to_version_#{method_version}(builder, options, &block)
92
+ Serializations::#{self.name}::Version_#{method_version}.serialize(self, builder, options, &block)
89
93
  end
90
94
  EOV
91
95
  @serialization_versions << Version.new(method_version)
@@ -94,36 +98,37 @@ module Serializable
94
98
 
95
99
  # This module contains instance methods
96
100
  module InstanceMethods
97
- def to_hash(options = {})
98
- serialize(Builder::HashStructure.new, options)
101
+ def to_hash(options = {}, &block)
102
+ serialize(Builder::HashStructure.new, options, &block)
99
103
  end
100
104
 
101
- def to_json(options = {})
102
- serialize(Builder::JsonFormat.new, options).to_json
105
+ def to_json(options = {}, &block)
106
+ serialize(Builder::HashStructure.new, options, &block).to_json
103
107
  end
104
108
 
105
- def to_xml(options = {})
106
- serialize(Builder::XmlMarkup.new, options)
109
+ def to_xml(options = {}, &block)
110
+ serialize(Builder::XmlMarkup.new, options, &block)
107
111
  end
108
112
 
109
- def serialize(builder, options = {})
113
+ def serialize(builder, options = {}, &block)
110
114
  if version_number = options[:version]
111
- if version = self.class.serialization_versions.find_version(Version.new(version_number))
112
- return self.send("serialize_to_version_#{version.to_s_underscored}", builder, options)
115
+ version = version_number.is_a?(Serializable::Version) ? version_number : Version.new(version_number)
116
+ if found_version = self.class.serialization_versions.find_version(version)
117
+ return self.send("serialize_to_version_#{found_version.to_s_underscored}", builder, options, &block)
113
118
  else
114
- raise "Version #{version_number} given but no serialization method found"
119
+ raise "Version #{version} given but no serialization method found"
115
120
  end
116
121
  else
117
122
  if version = self.class.default_serialization_version
118
- return self.send("serialize_to_version_#{version.to_s_underscored}", builder, options)
123
+ return self.send("serialize_to_version_#{version.to_s_underscored}", builder, options, &block)
119
124
  else
120
125
  raise "No serialization method found"
121
126
  end
122
127
  end
123
128
  end
124
129
 
125
- def serialize_for(builder, options = {})
126
- self.send(builder.serialization_method!, options)
130
+ def serialize_for(builder, options = {}, &block)
131
+ self.send(builder.serialization_method!, options, &block)
127
132
  end
128
133
  end
129
134
 
@@ -1,3 +1,5 @@
1
+ require 'serializable/version'
2
+
1
3
  module Serializable
2
4
  class Versions < Array
3
5
 
@@ -0,0 +1,27 @@
1
+ module Serializable
2
+ class XBuilder < ::ActionView::TemplateHandler
3
+ include ActionView::TemplateHandlers::Compilable
4
+
5
+ def compile(template)
6
+ compiled_code = <<-CODE
7
+ format = params[:format]
8
+
9
+ if format && :json == format.to_sym
10
+ _set_controller_content_type(Mime::JSON)
11
+ xbuilder = ::Builder::HashStructure.new
12
+ #{template.source}
13
+ xbuilder.target!.to_json
14
+ else
15
+ _set_controller_content_type(Mime::XML)
16
+ xbuilder = ::Builder::XmlMarkup.new
17
+ #{template.source}
18
+ xbuilder.target!
19
+ end
20
+ CODE
21
+ compiled_code
22
+ end
23
+
24
+ end
25
+ end
26
+
27
+ ActionView::Template.register_template_handler(:xbuilder, Serializable::XBuilder)
@@ -8,7 +8,10 @@ class SerializableObject
8
8
  end
9
9
 
10
10
  def serialize_to_version_1_5(builder, options)
11
- "This is version 1.5.0"
11
+ postfix = if block_given?
12
+ yield(builder)
13
+ end
14
+ "This is version 1.5.0#{postfix}"
12
15
  end
13
16
 
14
17
  def serialize_to_version_2_1(builder, options)
@@ -133,6 +136,13 @@ describe Serializable, 'when included in a class that has multiple versioned ser
133
136
 
134
137
  klass.to_xml.should == "This is version 1.5.0"
135
138
  end
139
+
140
+ it 'should pass on blocks' do
141
+ SerializableObject.default_serialization_version = Serializable::Version.new('1.5')
142
+ klass = SerializableObject.new
143
+
144
+ klass.to_xml { '.1' }.should == "This is version 1.5.0.1"
145
+ end
136
146
  end
137
147
  end
138
148
 
@@ -184,5 +194,12 @@ describe Serializable, 'when included in a class that has multiple serialization
184
194
  klass.to_json(:version => '1.5.0').should == "\"This is version 1.5.0 for TestRailsModel\""
185
195
  klass.to_hash(:version => '2.1.0').should == "This is version 2.1 for TestRailsModel"
186
196
  end
197
+
198
+ it 'should pass on blocks' do
199
+ TestModel.default_serialization_version = "1.5"
200
+ klass = TestModel.new
201
+
202
+ klass.to_hash { 'BlockedInsertion' }.should == "This is version 1.5.0 for BlockedInsertion"
203
+ end
187
204
  end
188
205
  end
@@ -2,7 +2,12 @@ module Serializations
2
2
  module TestModel
3
3
  class Version_1_5
4
4
  def self.serialize(test_model, builder, options)
5
- "This is version 1.5.0 for #{test_model.class.name}"
5
+ if block_given?
6
+ klass = yield(builder)
7
+ else
8
+ klass = test_model.class.name
9
+ end
10
+ "This is version 1.5.0 for #{klass}"
6
11
  end
7
12
  end
8
13
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'rubygems'
2
- require 'spec'
3
2
 
4
3
  ::RAILS_ROOT = File.join(File.dirname(__FILE__))
5
4
  $LOAD_PATH.push(File.join(::RAILS_ROOT, 'app'))
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_serializable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Birkir A. Barkarson
@@ -9,29 +15,56 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-06-18 00:00:00 +09:00
18
+ date: 2011-09-20 00:00:00 +09:00
13
19
  default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
- name: jsonbuilder
22
+ name: builder
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - <=
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 2
32
+ - 1
33
+ - 2
34
+ version: 2.1.2
17
35
  type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: jsonbuilder
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
20
42
  requirements:
21
43
  - - ">="
22
44
  - !ruby/object:Gem::Version
23
- version: 0.0.6
24
- version:
45
+ hash: 27
46
+ segments:
47
+ - 0
48
+ - 1
49
+ - 0
50
+ version: 0.1.0
51
+ type: :runtime
52
+ version_requirements: *id002
25
53
  - !ruby/object:Gem::Dependency
26
54
  name: activesupport
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
30
58
  requirements:
31
59
  - - ">="
32
60
  - !ruby/object:Gem::Version
61
+ hash: 11
62
+ segments:
63
+ - 1
64
+ - 2
33
65
  version: "1.2"
34
- version:
66
+ type: :runtime
67
+ version_requirements: *id003
35
68
  description: Easy versioning of serialization methods
36
69
  email: birkirb@stoicviking.net
37
70
  executables: []
@@ -41,7 +74,6 @@ extensions: []
41
74
  extra_rdoc_files:
42
75
  - README.rdoc
43
76
  files:
44
- - .gitignore
45
77
  - README.rdoc
46
78
  - Rakefile
47
79
  - VERSION
@@ -50,6 +82,7 @@ files:
50
82
  - lib/acts_as_serializable.rb
51
83
  - lib/serializable/version.rb
52
84
  - lib/serializable/versions.rb
85
+ - lib/serializable/xbuilder.rb
53
86
  - spec/acts_as_serializable_spec.rb
54
87
  - spec/app/serializations/test_rails_model/version_1_0_0.rb
55
88
  - spec/app/serializations/test_rails_model/version_1_5.rb
@@ -62,38 +95,37 @@ files:
62
95
  - spec/versions_spec.rb
63
96
  has_rdoc: true
64
97
  homepage: http://github.com/birkirb/acts_as_serializable
98
+ licenses: []
99
+
65
100
  post_install_message:
66
- rdoc_options:
67
- - --charset=UTF-8
101
+ rdoc_options: []
102
+
68
103
  require_paths:
69
104
  - lib
70
105
  required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
71
107
  requirements:
72
108
  - - ">="
73
109
  - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
74
113
  version: "0"
75
- version:
76
114
  required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
77
116
  requirements:
78
117
  - - ">="
79
118
  - !ruby/object:Gem::Version
119
+ hash: 3
120
+ segments:
121
+ - 0
80
122
  version: "0"
81
- version:
82
123
  requirements: []
83
124
 
84
125
  rubyforge_project: serializable
85
- rubygems_version: 1.3.1
126
+ rubygems_version: 1.3.7
86
127
  signing_key:
87
- specification_version: 2
128
+ specification_version: 3
88
129
  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/version_1_0_0.rb
98
- - spec/app/serializations/test_rails_model/version_2_1.rb
99
- - spec/app/serializations/test_rails_model/version_1_5.rb
130
+ test_files: []
131
+
data/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- coverage*
2
- pkg