Class: Smooth::Serializer
- Inherits:
-
ActiveModel::Serializer
- Object
- ActiveModel::Serializer
- Smooth::Serializer
show all
- Includes:
- Documentation
- Defined in:
- lib/smooth/serializer.rb
Class Method Summary
collapse
Instance Method Summary
collapse
#desc, included, #inline_description
Class Method Details
.attribute(attr, options = {}) ⇒ Object
137
138
139
140
141
142
143
144
145
|
# File 'lib/smooth/serializer.rb', line 137
def self.attribute(attr, options = {})
documented = inline_description
if documented
attribute_descriptions[attr.to_sym] = documented
end
super
end
|
.belongs_to_resource(resource) ⇒ Object
113
114
115
|
# File 'lib/smooth/serializer.rb', line 113
def self.belongs_to_resource(resource)
self.parent_resource = resource
end
|
.computed(*args, &block) ⇒ Object
147
148
149
150
151
|
# File 'lib/smooth/serializer.rb', line 147
def self.computed(*args, &block)
property_name = args.first
send(:define_method, property_name, &block)
send(:attribute, *args)
end
|
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/smooth/serializer.rb', line 63
def self.configure(options, resource = nil)
resource ||= Smooth.current_resource
klass = define_or_open(options, resource)
Array(options.blocks).each do |blk|
klass.class_eval(&blk)
end
klass
end
|
.define_or_open(options, resource) ⇒ Object
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/smooth/serializer.rb', line 74
def self.define_or_open(options, resource)
resource_name = resource.name
base = Smooth.serializer
name = options.name
name = nil if name == 'Default'
klass = "#{ resource.model_class }#{ name }".singularize + 'Serializer'
klass = klass.gsub(/\s+/, '')
if serializer_klass = Object.const_get(klass) rescue nil
return serializer_klass
end
parent_klass = Class.new(base)
parent_klass.belongs_to_resource(resource)
begin
Object.const_set(klass, parent_klass)
rescue => ex
puts ex.message
puts "Error setting #{ klass } #{ base }. klass is a #{ klass.class }"
end
parent_klass
end
|
.documentation ⇒ Object
129
130
131
|
# File 'lib/smooth/serializer.rb', line 129
def self.documentation
attribute_descriptions.merge(relationship_descriptions).to_mash
end
|
.documentation_for_association(association) ⇒ Object
125
126
127
|
# File 'lib/smooth/serializer.rb', line 125
def self.documentation_for_association(association)
relationship_descriptions[association.to_sym]
end
|
.documentation_for_attribute(attribute) ⇒ Object
121
122
123
|
# File 'lib/smooth/serializer.rb', line 121
def self.documentation_for_attribute(attribute)
attribute_descriptions[attribute.to_sym]
end
|
.has_many(attr, options = {}) ⇒ Object
163
164
165
166
167
168
169
170
171
|
# File 'lib/smooth/serializer.rb', line 163
def self.has_many(attr, options = {})
documented = inline_description
if documented
relationship_descriptions[attr.to_sym] = documented
end
super
end
|
.has_one(attr, options = {}) ⇒ Object
153
154
155
156
157
158
159
160
161
|
# File 'lib/smooth/serializer.rb', line 153
def self.has_one(attr, options = {})
documented = inline_description
if documented
relationship_descriptions[attr.to_sym] = documented
end
super
end
|
.interface_documentation ⇒ Object
133
134
135
|
# File 'lib/smooth/serializer.rb', line 133
def self.interface_documentation
documentation
end
|
.method_added(method_name) ⇒ Object
49
50
51
52
53
|
# File 'lib/smooth/serializer.rb', line 49
def self.method_added(method_name)
if documented = inline_description
attribute_descriptions[method_name.to_sym] = documented
end
end
|
.parent_api ⇒ Object
117
118
119
|
# File 'lib/smooth/serializer.rb', line 117
def self.parent_api
parent_resource.api
end
|
.return_ids_for_relationships! ⇒ Object
173
174
175
176
|
# File 'lib/smooth/serializer.rb', line 173
def self.return_ids_for_relationships!
@returns_ids_for_relationships = true
embed :ids
end
|
.returns_ids_for_relationships? ⇒ Boolean
178
179
180
|
# File 'lib/smooth/serializer.rb', line 178
def self.returns_ids_for_relationships?
@returns_ids_for_relationships == true
end
|
.route_variables ⇒ Object
45
46
47
|
# File 'lib/smooth/serializer.rb', line 45
def self.route_variables
@resource_route_variables ||= parent_resource.router.route_patterns_table.map { |p| _, h = p; h[:variables] }.flatten.compact.uniq
end
|
.schema_associations ⇒ Object
59
60
61
|
# File 'lib/smooth/serializer.rb', line 59
def self.schema_associations
schema[:associations]
end
|
.schema_attributes ⇒ Object
55
56
57
|
# File 'lib/smooth/serializer.rb', line 55
def self.schema_attributes
schema[:attributes]
end
|
Instance Method Details
#expand_routes(*slice) ⇒ Object
WIP Need to determine how to access the serialized version as it exists in the context of the serializer instance
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/smooth/serializer.rb', line 15
def expand_routes(*slice)
expanded = parent_resource.expand_routes(route_variables)
unless slice.empty?
expanded = expanded.send(:slice, *slice)
end
expanded.transform_keys do |key|
"#{ key }_url"
end
end
|
#parent_api ⇒ Object
109
110
111
|
# File 'lib/smooth/serializer.rb', line 109
def parent_api
self.class.parent_api
end
|
#parent_resource ⇒ Object
105
106
107
|
# File 'lib/smooth/serializer.rb', line 105
def parent_resource
self.class.parent_resource
end
|
#route_variables ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/smooth/serializer.rb', line 27
def route_variables
serializer = self
self.class.route_variables.reduce({}) do |memo, var|
value = case
when serializer.respond_to?(var)
serializer.send(var)
when serializer.object.respond_to?(var)
serializer.object.send(var)
else
serializer.read_attribute_for_serialization(var)
end
memo[var] = value
memo
end
end
|