Class: Babel::Serializer
- Inherits:
-
Object
show all
- Defined in:
- lib/babel/serializer.rb
Instance Method Summary
collapse
Constructor Details
#initialize(model_or_models) ⇒ Serializer
Returns a new instance of Serializer.
5
6
7
|
# File 'lib/babel/serializer.rb', line 5
def initialize(model_or_models)
@model_or_models = model_or_models
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
13
14
15
|
# File 'lib/babel/serializer.rb', line 13
def method_missing(method, *args, &block)
@model_or_models.send(method, *args, &block)
end
|
Instance Method Details
#respond_to?(method) ⇒ Boolean
9
10
11
|
# File 'lib/babel/serializer.rb', line 9
def respond_to?(method)
@model_or_models.respond_to?(method)
end
|
#to_hash(options = nil) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/babel/serializer.rb', line 55
def to_hash(options = nil)
filter.use(options) if options
case @model_or_models
when Array
@model_or_models.collect do |m|
filter_model(attr(m), m)
end
else
filter_model(attr(@model_or_models), @model_or_models)
end
end
|
#to_json(options = nil) ⇒ Object
67
68
69
|
# File 'lib/babel/serializer.rb', line 67
def to_json(options = nil)
to_hash(options).to_json
end
|
#to_xml(options = nil) ⇒ Object
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/babel/serializer.rb', line 71
def to_xml(options = nil)
opts = fitler.options.dup
root = opts.delete :root
fitler.use(opts)
result = to_hash
if root && result.is_a?(Array) && root.respond_to?(:pluralize)
root = root.to_s.pluralize
end
result.to_xml :root => root
end
|
#to_yaml(options = nil) ⇒ Object
82
83
84
|
# File 'lib/babel/serializer.rb', line 82
def to_yaml(options = nil)
to_hash(options).to_yaml
end
|
#use(context_or_options) ⇒ Object
50
51
52
53
|
# File 'lib/babel/serializer.rb', line 50
def use(context_or_options)
filter.use(context_or_options)
self
end
|