Class: RestfulX::Serialization::FXMLSerializer
- Inherits:
-
ActiveRecord::Serialization::Serializer
- Object
- ActiveRecord::Serialization::Serializer
- RestfulX::Serialization::FXMLSerializer
show all
- Defined in:
- lib/restfulx/rx_active_record.rb
Overview
Defined Under Namespace
Classes: Attribute, MethodAttribute
Instance Method Summary
collapse
Instance Method Details
#add_associations(association, records, opts) ⇒ 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
|
# File 'lib/restfulx/rx_active_record.rb', line 69
def add_associations(association, records, opts)
if records.is_a?(Enumerable)
tag = reformat_name(association.to_s)
type = options[:skip_types] ? {} : {:type => "array"}
if records.empty?
builder.tag!(tag, type)
else
builder.tag!(tag, type) do
association_name = association.to_s.singularize
records.each do |record|
if options[:skip_types]
record_type = {}
else
record_class = (record.class.to_s.underscore == association_name) ? nil : record.class.name
record_type = {:type => record_class}
end
record.to_fxml opts.merge(:root => association_name).merge(record_type)
end
end
end
else
if record = @record.send(association)
record.to_fxml(opts.merge(:root => association))
end
end
end
|
#add_attributes ⇒ Object
47
48
49
50
51
|
# File 'lib/restfulx/rx_active_record.rb', line 47
def add_attributes
(serializable_attributes + serializable_method_attributes).each do |attribute|
add_tag(attribute)
end
end
|
#add_procs ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/restfulx/rx_active_record.rb', line 53
def add_procs
if procs = options.delete(:procs)
[ *procs ].each do |proc|
proc.call(options)
end
end
end
|
#add_tag(attribute) ⇒ Object
61
62
63
64
65
66
67
|
# File 'lib/restfulx/rx_active_record.rb', line 61
def add_tag(attribute)
builder.tag!(
reformat_name(attribute.name),
attribute.value.to_s,
attribute.decorations(!options[:skip_types])
)
end
|
#builder ⇒ Object
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/restfulx/rx_active_record.rb', line 4
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
|
#camelize? ⇒ Boolean
27
28
29
|
# File 'lib/restfulx/rx_active_record.rb', line 27
def camelize?
options.has_key?(:camelize) && options[:camelize]
end
|
#dasherize? ⇒ Boolean
23
24
25
|
# File 'lib/restfulx/rx_active_record.rb', line 23
def dasherize?
!options.has_key?(:dasherize) || options[:dasherize]
end
|
31
32
33
34
|
# File 'lib/restfulx/rx_active_record.rb', line 31
def reformat_name(name)
name = name.camelize if camelize?
dasherize? ? name.dasherize : name
end
|
#root ⇒ Object
18
19
20
21
|
# File 'lib/restfulx/rx_active_record.rb', line 18
def root
root = (options[:root] || @record.class.to_s.underscore).to_s
reformat_name(root)
end
|
#serializable_attributes ⇒ Object
36
37
38
|
# File 'lib/restfulx/rx_active_record.rb', line 36
def serializable_attributes
serializable_attribute_names.collect { |name| Attribute.new(name, @record) }
end
|
#serializable_method_attributes ⇒ Object
40
41
42
43
44
45
|
# File 'lib/restfulx/rx_active_record.rb', line 40
def serializable_method_attributes
Array(options[:methods]).inject([]) do |method_attributes, name|
method_attributes << MethodAttribute.new(name.to_s, @record) if @record.respond_to?(name.to_s)
method_attributes
end
end
|
#serialize ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/restfulx/rx_active_record.rb', line 98
def serialize
args = [root]
if options[:namespace]
args << {:xmlns=>options[:namespace]}
end
if options[:type]
args << {:type=>options[:type]}
end
builder.tag!(*args) do
add_attributes
procs = options.delete(:procs)
add_includes { |association, records, opts| add_associations(association, records, opts) }
options[:procs] = procs
add_procs
yield builder if block_given?
end
end
|