dm-serializer 1.1.0.rc2 → 1.1.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -5,7 +5,7 @@ source 'http://rubygems.org'
5
5
  SOURCE = ENV.fetch('SOURCE', :git).to_sym
6
6
  REPO_POSTFIX = SOURCE == :path ? '' : '.git'
7
7
  DATAMAPPER = SOURCE == :path ? Pathname(__FILE__).dirname.parent : 'http://github.com/datamapper'
8
- DM_VERSION = '~> 1.1.0.rc2'
8
+ DM_VERSION = '~> 1.1.0.rc3'
9
9
  DO_VERSION = '~> 0.10.2'
10
10
  DM_DO_ADAPTERS = %w[ sqlite postgres mysql oracle sqlserver ]
11
11
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0.rc2
1
+ 1.1.0.rc3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dm-serializer}
8
- s.version = "1.1.0.rc2"
8
+ s.version = "1.1.0.rc3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Guy van den Berg"]
12
- s.date = %q{2011-03-01}
12
+ s.date = %q{2011-03-10}
13
13
  s.description = %q{DataMapper plugin for serializing Resources and Collections}
14
14
  s.email = %q{vandenberg.guy [a] gmail [d] com}
15
15
  s.extra_rdoc_files = [
@@ -33,10 +33,10 @@ Gem::Specification.new do |s|
33
33
  "lib/dm-serializer/to_json.rb",
34
34
  "lib/dm-serializer/to_xml.rb",
35
35
  "lib/dm-serializer/to_yaml.rb",
36
- "lib/dm-serializer/xml_serializers.rb",
37
- "lib/dm-serializer/xml_serializers/libxml.rb",
38
- "lib/dm-serializer/xml_serializers/nokogiri.rb",
39
- "lib/dm-serializer/xml_serializers/rexml.rb",
36
+ "lib/dm-serializer/xml.rb",
37
+ "lib/dm-serializer/xml/libxml.rb",
38
+ "lib/dm-serializer/xml/nokogiri.rb",
39
+ "lib/dm-serializer/xml/rexml.rb",
40
40
  "spec/fixtures/cow.rb",
41
41
  "spec/fixtures/planet.rb",
42
42
  "spec/fixtures/quan_tum_cat.rb",
@@ -57,7 +57,7 @@ Gem::Specification.new do |s|
57
57
  s.homepage = %q{http://github.com/datamapper/dm-serializer}
58
58
  s.require_paths = ["lib"]
59
59
  s.rubyforge_project = %q{datamapper}
60
- s.rubygems_version = %q{1.5.2}
60
+ s.rubygems_version = %q{1.6.2}
61
61
  s.summary = %q{DataMapper plugin for serializing Resources and Collections}
62
62
  s.test_files = [
63
63
  "spec/fixtures/cow.rb",
@@ -77,27 +77,27 @@ Gem::Specification.new do |s|
77
77
  s.specification_version = 3
78
78
 
79
79
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
80
- s.add_runtime_dependency(%q<dm-core>, ["~> 1.1.0.rc2"])
80
+ s.add_runtime_dependency(%q<dm-core>, ["~> 1.1.0.rc3"])
81
81
  s.add_runtime_dependency(%q<fastercsv>, ["~> 1.5.4"])
82
82
  s.add_runtime_dependency(%q<json>, ["~> 1.4.6"])
83
- s.add_development_dependency(%q<dm-validations>, ["~> 1.1.0.rc2"])
83
+ s.add_development_dependency(%q<dm-validations>, ["~> 1.1.0.rc3"])
84
84
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
85
85
  s.add_development_dependency(%q<rake>, ["~> 0.8.7"])
86
86
  s.add_development_dependency(%q<rspec>, ["~> 1.3.1"])
87
87
  else
88
- s.add_dependency(%q<dm-core>, ["~> 1.1.0.rc2"])
88
+ s.add_dependency(%q<dm-core>, ["~> 1.1.0.rc3"])
89
89
  s.add_dependency(%q<fastercsv>, ["~> 1.5.4"])
90
90
  s.add_dependency(%q<json>, ["~> 1.4.6"])
91
- s.add_dependency(%q<dm-validations>, ["~> 1.1.0.rc2"])
91
+ s.add_dependency(%q<dm-validations>, ["~> 1.1.0.rc3"])
92
92
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
93
93
  s.add_dependency(%q<rake>, ["~> 0.8.7"])
94
94
  s.add_dependency(%q<rspec>, ["~> 1.3.1"])
95
95
  end
96
96
  else
97
- s.add_dependency(%q<dm-core>, ["~> 1.1.0.rc2"])
97
+ s.add_dependency(%q<dm-core>, ["~> 1.1.0.rc3"])
98
98
  s.add_dependency(%q<fastercsv>, ["~> 1.5.4"])
99
99
  s.add_dependency(%q<json>, ["~> 1.4.6"])
100
- s.add_dependency(%q<dm-validations>, ["~> 1.1.0.rc2"])
100
+ s.add_dependency(%q<dm-validations>, ["~> 1.1.0.rc3"])
101
101
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
102
102
  s.add_dependency(%q<rake>, ["~> 0.8.7"])
103
103
  s.add_dependency(%q<rspec>, ["~> 1.3.1"])
@@ -2,3 +2,13 @@ require 'dm-serializer/to_json'
2
2
  require 'dm-serializer/to_xml'
3
3
  require 'dm-serializer/to_yaml'
4
4
  require 'dm-serializer/to_csv'
5
+
6
+ module DataMapper
7
+ # Define the `Serialize` constant for backwards compatibility.
8
+ #
9
+ # @note
10
+ # The `Serialize` constant will be removed soon, please use
11
+ # {Serializer} instead.
12
+ #
13
+ Serialize = Serializer
14
+ end
@@ -1,25 +1,16 @@
1
- begin
2
- require 'active_support/ordered_hash'
3
- rescue LoadError
4
- require 'extlib/dictionary'
5
- module ActiveSupport
6
- OrderedHash = Dictionary unless defined?(OrderedHash)
7
- end
8
- end
9
-
10
1
  require 'dm-core'
11
2
 
12
3
  module DataMapper
13
- module Serialize
14
-
4
+ module Serializer
15
5
  def self.dm_validations_loaded?
16
6
  DataMapper.const_defined?("Validations")
17
7
  end
18
8
 
19
- # Returns propreties to serialize based on :only or :exclude arrays, if provided
20
- # :only takes precendence over :exclude
9
+ # Returns propreties to serialize based on :only or :exclude arrays,
10
+ # if provided :only takes precendence over :exclude
21
11
  #
22
- # @return <Array> properties that need to be serialized
12
+ # @return [Array]
13
+ # Properties that need to be serialized.
23
14
  def properties_to_serialize(options)
24
15
  only_properties = Array(options[:only])
25
16
  excluded_properties = Array(options[:exclude])
@@ -28,11 +19,13 @@ module DataMapper
28
19
  if only_properties.include? p.name
29
20
  false
30
21
  else
31
- excluded_properties.include?(p.name) || !(only_properties.empty? || only_properties.include?(p.name))
22
+ excluded_properties.include?(p.name) ||
23
+ !(only_properties.empty? ||
24
+ only_properties.include?(p.name))
32
25
  end
33
26
  end
34
27
  end
35
-
36
28
  end
37
- Model.append_inclusions(Serialize)
29
+
30
+ Model.append_inclusions(Serializer)
38
31
  end
@@ -13,13 +13,17 @@ else
13
13
  end
14
14
 
15
15
  module DataMapper
16
- module Serialize
16
+ module Serializer
17
17
  # Serialize a Resource to comma-separated values (CSV).
18
18
  #
19
19
  # @return <String> a CSV representation of the Resource
20
- def to_csv(writer = '')
21
- CSV.generate(writer) do |csv|
22
- row = model.properties(repository.name).map do |property|
20
+ def to_csv(*args)
21
+ options = args.first || {}
22
+ options = options.to_h if options.respond_to?(:to_h)
23
+ options[:writer] = '' unless options.has_key? :writer
24
+
25
+ CSV.generate(options[:writer]) do |csv|
26
+ row = properties_to_serialize(options).map do |property|
23
27
  __send__(property.name).to_s
24
28
  end
25
29
  csv << row
@@ -28,8 +32,12 @@ module DataMapper
28
32
 
29
33
  module ValidationErrors
30
34
  module ToCsv
31
- def to_csv(writer = '')
32
- CSV.generate(writer) do |csv|
35
+ def to_csv(*args)
36
+ options = args.first || {}
37
+ options = options.to_h if options.respond_to?(:to_h)
38
+ options[:writer] = '' unless options.has_key? :writer
39
+
40
+ CSV.generate(options[:writer]) do |csv|
33
41
  errors.each do |key, value|
34
42
  value.each do |error|
35
43
  row = []
@@ -45,20 +53,20 @@ module DataMapper
45
53
  end
46
54
 
47
55
  class Collection
48
- def to_csv
56
+ def to_csv(*args)
49
57
  result = ''
50
58
  each do |item|
51
- result << item.to_csv + "\n"
59
+ result << item.to_csv(args.first) + "\n"
52
60
  end
53
61
  result
54
62
  end
55
63
  end
56
64
 
57
- if Serialize.dm_validations_loaded?
65
+ if Serializer.dm_validations_loaded?
58
66
 
59
67
  module Validations
60
68
  class ValidationErrors
61
- include DataMapper::Serialize::ValidationErrors::ToCsv
69
+ include DataMapper::Serializer::ValidationErrors::ToCsv
62
70
  end
63
71
  end
64
72
 
@@ -3,7 +3,7 @@ require 'dm-serializer/common'
3
3
  require 'json'
4
4
 
5
5
  module DataMapper
6
- module Serialize
6
+ module Serializer
7
7
  #
8
8
  # Converts the resource into a hash of properties.
9
9
  #
@@ -31,13 +31,19 @@ module DataMapper
31
31
  result[method] = __send__(method)
32
32
  end
33
33
 
34
- # Note: if you want to include a whole other model via relation, use :methods
35
- # comments.to_json(:relationships=>{:user=>{:include=>[:first_name],:methods=>[:age]}})
36
- # add relationships
37
- # TODO: This needs tests and also needs to be ported to #to_xml and #to_yaml
38
- (options[:relationships] || {}).each do |relationship_name, opts|
39
- next unless respond_to?(relationship_name)
40
- result[relationship_name] = __send__(relationship_name).to_json(opts.merge(:to_json => false))
34
+ # Note: if you want to include a whole other model via relation, use
35
+ # :methods:
36
+ #
37
+ # comments.to_json(:relationships=>{:user=>{:include=>[:first_name],:methods=>[:age]}})
38
+ #
39
+ # TODO: This needs tests and also needs to be ported to #to_xml and
40
+ # #to_yaml
41
+ if options[:relationships]
42
+ options[:relationships].each do |relationship_name, opts|
43
+ if respond_to?(relationship_name)
44
+ result[relationship_name] = __send__(relationship_name).to_json(opts.merge(:to_json => false))
45
+ end
46
+ end
41
47
  end
42
48
 
43
49
  result
@@ -63,7 +69,7 @@ module DataMapper
63
69
  module ValidationErrors
64
70
  module ToJson
65
71
  def to_json(*args)
66
- errors.to_hash.to_json
72
+ DataMapper::Ext::Array.to_hash(errors).to_json
67
73
  end
68
74
  end
69
75
  end
@@ -87,11 +93,11 @@ module DataMapper
87
93
  end
88
94
  end
89
95
 
90
- if Serialize.dm_validations_loaded?
96
+ if Serializer.dm_validations_loaded?
91
97
 
92
98
  module Validations
93
99
  class ValidationErrors
94
- include DataMapper::Serialize::ValidationErrors::ToJson
100
+ include DataMapper::Serializer::ValidationErrors::ToJson
95
101
  end
96
102
  end
97
103
 
@@ -1,31 +1,46 @@
1
1
  require 'dm-serializer/common'
2
- require 'dm-serializer/xml_serializers'
3
- require 'rexml/document'
2
+ require 'dm-serializer/xml'
4
3
 
5
4
  module DataMapper
6
- module Serialize
7
- # Serialize a Resource to XML
5
+ module Serializer
6
+ # Serialize a Resource to XML.
7
+ #
8
+ # @return [LibXML::Document, Nokogiri::Document, REXML::Document]
9
+ # An XML representation of this Resource.
8
10
  #
9
- # @return <REXML::Document> an XML representation of this Resource
10
11
  def to_xml(opts = {})
11
- xml = XMLSerializers::SERIALIZER
12
+ xml = XML.serializer
12
13
  xml.output(to_xml_document(opts)).to_s
13
14
  end
14
15
 
15
- # This method requires certain methods to be implemented in the individual
16
- # serializer library subclasses:
17
- # new_document
18
- # root_node
19
- # add_property_node
20
- # add_node
16
+ # This method requires certain methods to be implemented in the
17
+ # individual serializer library subclasses:
18
+ #
19
+ # * new_document
20
+ # * root_node
21
+ # * add_property_node
22
+ # * add_node
21
23
  def to_xml_document(opts={}, doc = nil)
22
- xml = XMLSerializers::SERIALIZER
24
+ xml = XML.serializer
23
25
  doc ||= xml.new_document
24
- default_xml_element_name = lambda { DataMapper::Inflector.underscore(model.name).tr("/", "-") }
25
- root = xml.root_node(doc, opts[:element_name] || default_xml_element_name[])
26
+
27
+ default_xml_element_name = lambda {
28
+ DataMapper::Inflector.underscore(model.name).tr("/", "-")
29
+ }
30
+
31
+ root = xml.root_node(
32
+ doc,
33
+ (opts[:element_name] || default_xml_element_name[])
34
+ )
35
+
26
36
  properties_to_serialize(opts).each do |property|
27
37
  value = __send__(property.name)
28
- attrs = (property.primitive == String) ? {} : {'type' => property.primitive.to_s.downcase}
38
+ attrs = {}
39
+
40
+ unless property.primitive == String
41
+ attrs['type'] = property.primitive.to_s.downcase
42
+ end
43
+
29
44
  xml.add_node(root, property.name.to_s, value, attrs)
30
45
  end
31
46
 
@@ -33,6 +48,7 @@ module DataMapper
33
48
  if self.respond_to?(meth)
34
49
  xml_name = meth.to_s.gsub(/[^a-z0-9_]/, '')
35
50
  value = __send__(meth)
51
+
36
52
  unless value.nil?
37
53
  if value.respond_to?(:to_xml_document)
38
54
  xml.add_xml(root, value.to_xml_document)
@@ -42,6 +58,7 @@ module DataMapper
42
58
  end
43
59
  end
44
60
  end
61
+
45
62
  doc
46
63
  end
47
64
 
@@ -52,13 +69,14 @@ module DataMapper
52
69
  end
53
70
 
54
71
  def to_xml_document(opts = {})
55
- xml = DataMapper::Serialize::XMLSerializers::SERIALIZER
72
+ xml = DataMapper::Serializer::XML.serializer
56
73
  doc = xml.new_document
57
74
  root = xml.root_node(doc, "errors", {'type' => 'hash'})
58
75
 
59
76
  errors.each do |key, value|
60
77
  property = xml.add_node(root, key.to_s, nil, {'type' => 'array'})
61
78
  property.attributes["type"] = 'array'
79
+
62
80
  value.each do |error|
63
81
  xml.add_node(property, "error", error)
64
82
  end
@@ -77,22 +95,32 @@ module DataMapper
77
95
  end
78
96
 
79
97
  def to_xml_document(opts = {})
80
- xml = DataMapper::Serialize::XMLSerializers::SERIALIZER
98
+ xml = DataMapper::Serializer::XML.serializer
81
99
  doc = xml.new_document
82
- default_collection_element_name = lambda {DataMapper::Inflector.pluralize(DataMapper::Inflector.underscore(self.model.to_s)).tr("/", "-")}
83
- root = xml.root_node(doc, opts[:collection_element_name] || default_collection_element_name[], {'type' => 'array'})
100
+
101
+ default_collection_element_name = lambda {
102
+ DataMapper::Inflector.pluralize(DataMapper::Inflector.underscore(self.model.to_s)).tr("/", "-")
103
+ }
104
+
105
+ root = xml.root_node(
106
+ doc,
107
+ opts[:collection_element_name] || default_collection_element_name[],
108
+ {'type' => 'array'}
109
+ )
110
+
84
111
  self.each do |item|
85
112
  item.to_xml_document(opts, doc)
86
113
  end
114
+
87
115
  doc
88
116
  end
89
117
  end
90
118
 
91
- if Serialize.dm_validations_loaded?
119
+ if Serializer.dm_validations_loaded?
92
120
 
93
121
  module Validations
94
122
  class ValidationErrors
95
- include DataMapper::Serialize::ValidationErrors::ToXml
123
+ include DataMapper::Serializer::ValidationErrors::ToXml
96
124
  end
97
125
  end
98
126
 
@@ -1,12 +1,13 @@
1
1
  require 'dm-serializer/common'
2
2
 
3
3
  module DataMapper
4
- module Serialize
4
+ module Serializer
5
5
  # Serialize a Resource to YAML
6
6
  #
7
- # @return <YAML> a YAML representation of this Resource
7
+ # @return [YAML]
8
+ # A YAML representation of this Resource.
8
9
  def to_yaml(opts_or_emitter = {})
9
- if !opts_or_emitter.is_a?(Hash)
10
+ unless opts_or_emitter.is_a?(Hash)
10
11
  emitter = opts_or_emitter
11
12
  opts = {}
12
13
  else
@@ -14,20 +15,22 @@ module DataMapper
14
15
  opts = opts_or_emitter
15
16
  end
16
17
 
17
- YAML::quick_emit(object_id,emitter) do |out|
18
+ YAML.quick_emit(object_id,emitter) do |out|
18
19
  out.map(nil,to_yaml_style) do |map|
19
20
  properties_to_serialize(opts).each do |property|
20
21
  value = __send__(property.name.to_sym)
21
22
  map.add(property.name, value.is_a?(Class) ? value.to_s : value)
22
23
  end
24
+
23
25
  # add methods
24
26
  Array(opts[:methods]).each do |meth|
25
27
  if respond_to?(meth)
26
28
  map.add(meth.to_sym, __send__(meth))
27
29
  end
28
30
  end
29
- (instance_variable_get("@yaml_addes") || []).each do |k,v|
30
- map.add(k.to_s,v)
31
+
32
+ if (additions = instance_variable_get("@yaml_addes"))
33
+ additions.each { |k,v| map.add(k.to_s,v) }
31
34
  end
32
35
  end
33
36
  end
@@ -36,7 +39,7 @@ module DataMapper
36
39
  module ValidationErrors
37
40
  module ToYaml
38
41
  def to_yaml(*args)
39
- errors.to_hash.to_yaml(*args)
42
+ DataMapper::Ext::Array.to_hash(errors).to_yaml(*args)
40
43
  end
41
44
  end
42
45
  end
@@ -45,20 +48,20 @@ module DataMapper
45
48
 
46
49
  class Collection
47
50
  def to_yaml(opts_or_emitter = {})
48
- if !opts_or_emitter.is_a?(Hash)
51
+ unless opts_or_emitter.is_a?(Hash)
49
52
  to_a.to_yaml(opts_or_emitter)
50
53
  else
51
54
  # FIXME: Don't double handle the YAML (remove the YAML.load)
52
- to_a.collect {|x| YAML.load(x.to_yaml(opts_or_emitter)) }.to_yaml
55
+ to_a.collect { |x| YAML.load(x.to_yaml(opts_or_emitter)) }.to_yaml
53
56
  end
54
57
  end
55
58
  end
56
59
 
57
- if Serialize.dm_validations_loaded?
60
+ if Serializer.dm_validations_loaded?
58
61
 
59
62
  module Validations
60
63
  class ValidationErrors
61
- include DataMapper::Serialize::ValidationErrors::ToYaml
64
+ include DataMapper::Serializer::ValidationErrors::ToYaml
62
65
  end
63
66
  end
64
67
 
@@ -0,0 +1,56 @@
1
+ module DataMapper
2
+ module Serializer
3
+ module XML
4
+ # The supported XML Serializers
5
+ SERIALIZERS = {
6
+ :libxml => 'LibXML',
7
+ :nokogiri => 'Nokogiri',
8
+ :rexml => 'REXML'
9
+ }
10
+
11
+ #
12
+ # The current XML Serializer.
13
+ #
14
+ # @return [Module]
15
+ # The module within {DataMapper::Serialize::XML}.
16
+ #
17
+ # @since 1.1.0
18
+ #
19
+ def self.serializer
20
+ @serializer
21
+ end
22
+
23
+ #
24
+ # Sets the XML Serializer to use.
25
+ #
26
+ # @param [Symbol] name
27
+ # The name of the serializer to use. Must be either `:libxml`,
28
+ # `:nokogiri` or `:rexml`.
29
+ #
30
+ # @return [Module]
31
+ # The module within {DataMapper::Serialize::XML}.
32
+ #
33
+ # @since 1.1.0
34
+ #
35
+ def self.serializer=(name)
36
+ serializer_const = SERIALIZERS[name]
37
+
38
+ unless serializer_const
39
+ raise(ArgumentError,"unsupported XML Serializer #{name}")
40
+ end
41
+
42
+ require "dm-serializer/xml/#{name}"
43
+ @serializer = const_get(serializer_const)
44
+ end
45
+
46
+ [:nokogiri, :libxml, :rexml].each do |name|
47
+ # attempt to load the first available XML Serializer
48
+ begin
49
+ self.serializer = name
50
+ break
51
+ rescue LoadError
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,6 +1,8 @@
1
+ require 'libxml'
2
+
1
3
  module DataMapper
2
- module Serialize
3
- module XMLSerializers
4
+ module Serializer
5
+ module XML
4
6
  module LibXML
5
7
  def self.new_document
6
8
  ::LibXML::XML::Document.new
@@ -8,9 +10,11 @@ module DataMapper
8
10
 
9
11
  def self.root_node(doc, name, attrs = {})
10
12
  root = ::LibXML::XML::Node.new(name)
13
+
11
14
  attrs.each do |attr_name, attr_val|
12
15
  root[attr_name] = attr_val
13
16
  end
17
+
14
18
  doc.root.nil? ? doc.root = root : doc.root << root
15
19
  root
16
20
  end
@@ -18,9 +22,11 @@ module DataMapper
18
22
  def self.add_node(parent, name, value, attrs = {})
19
23
  value_str = value.to_s unless value.nil?
20
24
  node = ::LibXML::XML::Node.new(name, value_str)
25
+
21
26
  attrs.each do |attr_name, attr_val|
22
27
  node[attr_name] = attr_val
23
28
  end
29
+
24
30
  parent << node
25
31
  node
26
32
  end
@@ -1,6 +1,8 @@
1
+ require 'nokogiri'
2
+
1
3
  module DataMapper
2
- module Serialize
3
- module XMLSerializers
4
+ module Serializer
5
+ module XML
4
6
  module Nokogiri
5
7
  def self.new_document
6
8
  ::Nokogiri::XML::Document.new
@@ -8,9 +10,11 @@ module DataMapper
8
10
 
9
11
  def self.root_node(doc, name, attrs = {})
10
12
  root = ::Nokogiri::XML::Node.new(name, doc)
13
+
11
14
  attrs.each do |attr_name, attr_val|
12
15
  root[attr_name] = attr_val
13
16
  end
17
+
14
18
  doc.root.nil? ? doc.root = root : doc.root << root
15
19
  root
16
20
  end
@@ -18,7 +22,11 @@ module DataMapper
18
22
  def self.add_node(parent, name, value, attrs = {})
19
23
  node = ::Nokogiri::XML::Node.new(name, parent.document)
20
24
  node << ::Nokogiri::XML::Text.new(value.to_s, parent.document) unless value.nil?
21
- attrs.each {|attr_name, attr_val| node[attr_name] = attr_val }
25
+
26
+ attrs.each do |attr_name, attr_val|
27
+ node[attr_name] = attr_val
28
+ end
29
+
22
30
  parent << node
23
31
  node
24
32
  end
@@ -1,6 +1,8 @@
1
+ require 'rexml/document'
2
+
1
3
  module DataMapper
2
- module Serialize
3
- module XMLSerializers
4
+ module Serializer
5
+ module XML
4
6
  module REXML
5
7
  def self.new_document
6
8
  ::REXML::Document.new
@@ -12,7 +14,11 @@ module DataMapper
12
14
 
13
15
  def self.add_node(parent, name, value, attrs = {})
14
16
  node = parent.add_element(name)
15
- attrs.each {|attr_name, attr_val| node.attributes[attr_name] = attr_val}
17
+
18
+ attrs.each do |attr_name, attr_val|
19
+ node.attributes[attr_name] = attr_val
20
+ end
21
+
16
22
  node << ::REXML::Text.new(value.to_s) unless value.nil?
17
23
  node
18
24
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe DataMapper::Serialize, '#to_json' do
3
+ describe DataMapper::Serializer, '#to_json' do
4
4
  #
5
5
  # ==== ajaxy JSON
6
6
  #
@@ -64,7 +64,7 @@ describe DataMapper::Serialize, '#to_json' do
64
64
 
65
65
  end
66
66
 
67
- describe DataMapper::Serialize, '#as_json' do
67
+ describe DataMapper::Serializer, '#as_json' do
68
68
  it "handles nil for options" do
69
69
  expect { Cow.new.as_json(nil) }.to_not raise_error
70
70
  end
@@ -1,25 +1,20 @@
1
1
  require 'spec_helper'
2
+ require 'rexml/document'
2
3
 
3
- {
4
- 'REXML' => [],
5
- 'LibXML' => [ 'libxml-ruby', 'libxml' ],
6
- 'Nokogiri' => [ 'nokogiri', 'nokogiri' ],
7
- }.each do |lib, (gem_name, file_to_require)|
4
+ [:rexml, :libxml, :nokogiri].each do |lib|
8
5
  begin
9
- require file_to_require if file_to_require
10
- rescue LoadError
11
- warn "[WARNING] Cannot find gem '#{gem_name}', not running #to_xml specs for #{lib}"
6
+ DataMapper::Serializer::XML.serializer = lib
7
+ rescue LoadError => e
8
+ warn "[WARNING] #{e.message}"
9
+ warn "[WARNING] Not running #to_xml specs for #{lib}"
12
10
  next
13
11
  end
14
12
 
15
- describe DataMapper::Serialize, "#to_xml using #{lib}" do
16
- #
17
- # ==== enterprisey XML
18
- #
19
-
13
+ describe DataMapper::Serializer, "#to_xml using #{lib}" do
20
14
  before(:all) do
21
15
  DataMapper.finalize
22
- @harness = Class.new(SerializerTestHarness) do
16
+
17
+ @harness = Class.new(SerializerTestHarness) {
23
18
  def method_name
24
19
  :to_xml
25
20
  end
@@ -58,9 +53,7 @@ require 'spec_helper'
58
53
  value = value.to_i if value && ["integer", "datamapper::types::serial"].include?(type)
59
54
  value
60
55
  end
61
- end.new
62
- DataMapper::Serialize::XMLSerializers.instance_eval { remove_const('SERIALIZER') }
63
- DataMapper::Serialize::XMLSerializers::SERIALIZER = DataMapper::Serialize::XMLSerializers::const_get(lib)
56
+ }.new
64
57
  end
65
58
 
66
59
  it_should_behave_like "A serialization method"
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe DataMapper::Serialize, '#to_yaml' do
3
+ describe DataMapper::Serializer, '#to_yaml' do
4
4
  #
5
5
  # ==== yummy YAML
6
6
  #
@@ -7,6 +7,12 @@ require 'dm-migrations'
7
7
 
8
8
  require File.expand_path('spec/lib/serialization_method_shared_spec')
9
9
 
10
+ class Array
11
+ def to_hash
12
+ DataMapper::Ext::Array.to_hash(self)
13
+ end
14
+ end
15
+
10
16
  # require fixture resources
11
17
  SPEC_ROOT = Pathname(__FILE__).dirname.expand_path
12
18
  Pathname.glob((SPEC_ROOT + 'fixtures/**/*.rb').to_s).each { |file| require file }
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: dm-serializer
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: 6
5
- version: 1.1.0.rc2
5
+ version: 1.1.0.rc3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Guy van den Berg
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-01 00:00:00 -08:00
13
+ date: 2011-03-10 00:00:00 -08:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ~>
22
22
  - !ruby/object:Gem::Version
23
- version: 1.1.0.rc2
23
+ version: 1.1.0.rc3
24
24
  type: :runtime
25
25
  prerelease: false
26
26
  version_requirements: *id001
@@ -53,7 +53,7 @@ dependencies:
53
53
  requirements:
54
54
  - - ~>
55
55
  - !ruby/object:Gem::Version
56
- version: 1.1.0.rc2
56
+ version: 1.1.0.rc3
57
57
  type: :development
58
58
  prerelease: false
59
59
  version_requirements: *id004
@@ -116,10 +116,10 @@ files:
116
116
  - lib/dm-serializer/to_json.rb
117
117
  - lib/dm-serializer/to_xml.rb
118
118
  - lib/dm-serializer/to_yaml.rb
119
- - lib/dm-serializer/xml_serializers.rb
120
- - lib/dm-serializer/xml_serializers/libxml.rb
121
- - lib/dm-serializer/xml_serializers/nokogiri.rb
122
- - lib/dm-serializer/xml_serializers/rexml.rb
119
+ - lib/dm-serializer/xml.rb
120
+ - lib/dm-serializer/xml/libxml.rb
121
+ - lib/dm-serializer/xml/nokogiri.rb
122
+ - lib/dm-serializer/xml/rexml.rb
123
123
  - spec/fixtures/cow.rb
124
124
  - spec/fixtures/planet.rb
125
125
  - spec/fixtures/quan_tum_cat.rb
@@ -160,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
160
  requirements: []
161
161
 
162
162
  rubyforge_project: datamapper
163
- rubygems_version: 1.5.2
163
+ rubygems_version: 1.6.2
164
164
  signing_key:
165
165
  specification_version: 3
166
166
  summary: DataMapper plugin for serializing Resources and Collections
@@ -1,17 +0,0 @@
1
- require 'dm-serializer/xml_serializers/rexml'
2
- require 'dm-serializer/xml_serializers/nokogiri'
3
- require 'dm-serializer/xml_serializers/libxml'
4
-
5
- module DataMapper
6
- module Serialize
7
- module XMLSerializers
8
- SERIALIZER = if defined?(::LibXML)
9
- LibXML
10
- elsif defined?(::Nokogiri)
11
- Nokogiri
12
- else
13
- REXML
14
- end
15
- end
16
- end
17
- end