Class: Flexserializer::Base
- Inherits:
-
ActiveModel::Serializer
- Object
- ActiveModel::Serializer
- Flexserializer::Base
show all
- Defined in:
- lib/flexserializer/base.rb
Defined Under Namespace
Classes: Definition, DefinitionBuilder
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil) ⇒ Object
-
#attributes(requested_attrs = nil, reload = false) ⇒ Object
override serializer methods.
-
#define_attribute(attr, options = {}, &block) ⇒ Object
-
#define_attributes(*attrs) ⇒ Object
-
#define_belongs_to(name, options = {}, &block) ⇒ Object
-
#define_default_attrs ⇒ Object
-
#define_group_attrs ⇒ Object
-
#define_has_many(name, options = {}, &block) ⇒ Object
-
#define_has_one(name, options = {}, &block) ⇒ Object
-
#define_options ⇒ Object
-
#initialize(object, options = {}) ⇒ Base
constructor
Constructor Details
#initialize(object, options = {}) ⇒ Base
Returns a new instance of Base.
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/flexserializer/base.rb', line 96
def initialize(object, options = {})
super(object, options)
@group_name = options[:group]
definition = self.class.definition_for(group_name)
extend(definition.methods_module) if definition_module_has_methods?(definition.methods_module)
@_attributes_data = definition.attributes_data
@_reflections = definition.reflections.each_with_object({}) do |(key, reflection), hash|
hash[key] = reflection.dup
end
end
|
Class Attribute Details
.data_default_attributes ⇒ Object
Returns the value of attribute data_default_attributes.
47
48
49
|
# File 'lib/flexserializer/base.rb', line 47
def data_default_attributes
@data_default_attributes
end
|
.definitions ⇒ Object
Returns the value of attribute definitions.
47
48
49
|
# File 'lib/flexserializer/base.rb', line 47
def definitions
@definitions
end
|
.groups ⇒ Object
Returns the value of attribute groups.
47
48
49
|
# File 'lib/flexserializer/base.rb', line 47
def groups
@groups
end
|
Instance Attribute Details
#_attributes_data ⇒ Object
Returns the value of attribute _attributes_data.
94
95
96
|
# File 'lib/flexserializer/base.rb', line 94
def _attributes_data
@_attributes_data
end
|
#_reflections ⇒ Object
Returns the value of attribute _reflections.
94
95
96
|
# File 'lib/flexserializer/base.rb', line 94
def _reflections
@_reflections
end
|
#group_name ⇒ Object
Returns the value of attribute group_name.
94
95
96
|
# File 'lib/flexserializer/base.rb', line 94
def group_name
@group_name
end
|
Class Method Details
.default_attributes(&block) ⇒ Object
55
56
57
58
|
# File 'lib/flexserializer/base.rb', line 55
def default_attributes(&block)
self.data_default_attributes = block
clear_definitions
end
|
.definition_for(group_name) ⇒ Object
68
69
70
71
|
# File 'lib/flexserializer/base.rb', line 68
def definition_for(group_name)
self.definitions ||= {}
definitions[group_name] ||= build_definition(group_name)
end
|
.group(*group_names, &block) ⇒ Object
60
61
62
63
64
65
66
|
# File 'lib/flexserializer/base.rb', line 60
def group(*group_names, &block)
group_names.each do |name_group|
self.groups[name_group] ||= []
self.groups[name_group] << block
end
clear_definitions
end
|
.inherited(base) ⇒ Object
49
50
51
52
53
|
# File 'lib/flexserializer/base.rb', line 49
def inherited(base)
super
base.groups = {}
base.definitions = {}
end
|
Instance Method Details
#associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil) ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/flexserializer/base.rb', line 159
def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
include_slice ||= include_directive
return Enumerator.new unless object
Enumerator.new do |y|
_reflections.each do |key, reflection|
next if reflection.excluded?(self)
next unless include_directive.key?(key)
association = reflection.build_association(self, instance_options, include_slice)
y.yield association
end
end
end
|
#attributes(requested_attrs = nil, reload = false) ⇒ Object
override serializer methods
150
151
152
153
154
155
156
157
|
# File 'lib/flexserializer/base.rb', line 150
def attributes(requested_attrs = nil, reload = false)
@attributes = nil if reload
@attributes ||= _attributes_data.each_with_object({}) do |(key, attr), hash|
next if attr.excluded?(self)
next unless requested_attrs.nil? || requested_attrs.include?(key)
hash[key] = attr.value(self)
end
end
|
#define_attribute(attr, options = {}, &block) ⇒ Object
116
117
118
119
|
# File 'lib/flexserializer/base.rb', line 116
def define_attribute(attr, options = {}, &block)
key = options.fetch(:key, attr)
writable_attributes_data[key] = Attribute.new(attr, options, block)
end
|
#define_attributes(*attrs) ⇒ Object
109
110
111
112
113
114
|
# File 'lib/flexserializer/base.rb', line 109
def define_attributes(*attrs)
attrs = attrs.first if attrs.first.class == Array
attrs.each do |attr|
define_attribute(attr)
end
end
|
#define_belongs_to(name, options = {}, &block) ⇒ Object
136
137
138
|
# File 'lib/flexserializer/base.rb', line 136
def define_belongs_to(name, options = {}, &block)
define_associate(BelongsToReflection.new(name, options, block))
end
|
#define_default_attrs ⇒ Object
121
122
123
124
|
# File 'lib/flexserializer/base.rb', line 121
def define_default_attrs
return unless self.class.data_default_attributes
self.instance_eval &self.class.data_default_attributes
end
|
#define_group_attrs ⇒ Object
126
127
128
129
130
|
# File 'lib/flexserializer/base.rb', line 126
def define_group_attrs
self.class.groups.send(:[], group_name)&.each do |block|
self.instance_eval(&block)
end
end
|
#define_has_many(name, options = {}, &block) ⇒ Object
132
133
134
|
# File 'lib/flexserializer/base.rb', line 132
def define_has_many(name, options = {}, &block)
define_associate(HasManyReflection.new(name, options, block))
end
|
#define_has_one(name, options = {}, &block) ⇒ Object
140
141
142
|
# File 'lib/flexserializer/base.rb', line 140
def define_has_one(name, options = {}, &block)
define_associate(HasOneReflection.new(name, options, block))
end
|
#define_options ⇒ Object
144
145
146
|
# File 'lib/flexserializer/base.rb', line 144
def define_options
instance_options
end
|