Class: MongoMapper::Plugins::Serialization::XmlSerializer
- Inherits:
-
Object
- Object
- MongoMapper::Plugins::Serialization::XmlSerializer
show all
- Defined in:
- lib/mongo_mapper/plugins/serialization/xml_serializer.rb
Overview
Defined Under Namespace
Classes: Attribute, MethodAttribute
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(record, options = {}) ⇒ XmlSerializer
Returns a new instance of XmlSerializer.
8
9
10
|
# File 'lib/mongo_mapper/plugins/serialization/xml_serializer.rb', line 8
def initialize(record, options = {})
@record, @options = record, options.dup
end
|
Instance Attribute Details
Returns the value of attribute options.
6
7
8
|
# File 'lib/mongo_mapper/plugins/serialization/xml_serializer.rb', line 6
def options
@options
end
|
Instance Method Details
#add_attributes ⇒ Object
63
64
65
66
67
|
# File 'lib/mongo_mapper/plugins/serialization/xml_serializer.rb', line 63
def add_attributes
(serializable_attributes + serializable_method_attributes).each do |attribute|
add_tag(attribute)
end
end
|
#add_includes ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/mongo_mapper/plugins/serialization/xml_serializer.rb', line 69
def add_includes
if include_associations = options.delete(:include)
root_only_or_except = { :except => options[:except],
:only => options[:only] }
include_has_options = include_associations.is_a?(Hash)
for association in include_has_options ? include_associations.keys : Array(include_associations)
association_options = include_has_options ? include_associations[association] : root_only_or_except
opts = options.merge(association_options)
case @record.class.associations[association].type
when :many, :has_and_belongs_to_many
records = @record.send(association).to_a
unless records.empty?
tag = association.to_s
tag = tag.dasherize if dasherize?
builder.tag!(tag) do
records.each { |r| r.to_xml(opts.merge(:root=>r.class.to_s.underscore)) }
end
end
when :has_one, :belongs_to
if record = @record.send(association)
record.to_xml(opts.merge(:root => association))
end
end
end
options[:include] = include_associations
end
end
|
#add_procs ⇒ Object
103
104
105
106
107
108
109
|
# File 'lib/mongo_mapper/plugins/serialization/xml_serializer.rb', line 103
def add_procs
if procs = options.delete(:procs)
[ *procs ].each do |proc|
proc.call(options)
end
end
end
|
#add_tag(attribute) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/mongo_mapper/plugins/serialization/xml_serializer.rb', line 112
def add_tag(attribute)
if attribute.type == :array
builder.tag!(
dasherize? ? attribute.name.dasherize.pluralize : attribute.name.pluralize,
attribute.decorations(!options[:skip_types])
) do |x|
attribute.value.each do |val|
if val.respond_to? :to_xml
x << val.to_xml(:skip_instruct => true, :root => attribute.name.dasherize.singularize)
else
x.tag!(
dasherize? ? attribute.name.dasherize.singularize : attribute.name.singularize,
val.to_s
)
end
end
end
else
builder.tag!(
dasherize? ? attribute.name.dasherize : attribute.name,
attribute.value.to_s,
attribute.decorations(!options[:skip_types])
)
end
end
|
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/mongo_mapper/plugins/serialization/xml_serializer.rb', line 12
def builder
@builder ||= begin
options[:indent] ||= 2
builder = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
unless options[:skip_instruct]
builder.instruct!
options[:skip_instruct] = true
end
builder
end
end
|
31
32
33
|
# File 'lib/mongo_mapper/plugins/serialization/xml_serializer.rb', line 31
def dasherize?
!options.has_key?(:dasherize) || options[:dasherize]
end
|
26
27
28
29
|
# File 'lib/mongo_mapper/plugins/serialization/xml_serializer.rb', line 26
def root
root = (options[:root] || @record.class.to_s.underscore).to_s
dasherize? ? root.dasherize : root
end
|
#serializable_attributes ⇒ Object
To replicate the behavior in ActiveRecord#attributes, :except takes precedence over :only. If :only is not set for a N level model but is set for the N+1 level models, then because :except is set to a default value, the second level model can have both :except and :only set. So if :only is set, always delete :except.
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/mongo_mapper/plugins/serialization/xml_serializer.rb', line 42
def serializable_attributes
attribute_names = @record.class.keys.keys idex = attribute_names.index("_id")
attribute_names[idex] = "id" if idex
if options[:only]
options.delete(:except)
attribute_names = attribute_names & Array(options[:only]).collect { |n| n.to_s }
else
options[:except] = Array(options[:except])
attribute_names = attribute_names - options[:except].collect { |n| n.to_s }
end
attribute_names.collect { |name| Attribute.new(name, @record) }
end
|
#serializable_method_attributes ⇒ Object
59
60
61
|
# File 'lib/mongo_mapper/plugins/serialization/xml_serializer.rb', line 59
def serializable_method_attributes
Array(options[:methods]).collect { |name| MethodAttribute.new(name.to_s, @record) }
end
|
#serialize ⇒ Object
Also known as:
to_s
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/mongo_mapper/plugins/serialization/xml_serializer.rb', line 138
def serialize
args = [root]
if options[:namespace]
args << {:xmlns=>options[:namespace]}
end
builder.tag!(*args) do
add_attributes
add_includes
add_procs
yield builder if block_given?
end
end
|