class Flexserializer::Base
Attributes
data_default_attributes[RW]
groups[RW]
_attributes_data[R]
_reflections[R]
group_name[R]
Public Class Methods
default_attributes(&block)
click to toggle source
# File lib/flexserializer/base.rb, line 10 def default_attributes(&block) self.data_default_attributes = block end
group(*group_names, &block)
click to toggle source
# File lib/flexserializer/base.rb, line 14 def group(*group_names, &block) group_names.each do |name_group| self.groups[name_group] ||= [] self.groups[name_group] << block end end
inherited(base)
click to toggle source
# File lib/flexserializer/base.rb, line 6 def inherited(base) base.groups = {} end
new(object, options = {})
click to toggle source
Calls superclass method
# File lib/flexserializer/base.rb, line 24 def initialize(object, options = {}) super(object, options) @_attributes_data = {} @_reflections = {} @group_name = options[:group] make_all_attributes end
Public Instance Methods
associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
click to toggle source
# File lib/flexserializer/base.rb, line 82 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)
click to toggle source
override serializer methods
# File lib/flexserializer/base.rb, line 73 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)
click to toggle source
# File lib/flexserializer/base.rb, line 39 def define_attribute(attr, options = {}, &block) key = options.fetch(:key, attr) _attributes_data[key] = Attribute.new(attr, options, block) end
define_attributes(*attrs)
click to toggle source
# File lib/flexserializer/base.rb, line 32 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)
click to toggle source
# File lib/flexserializer/base.rb, line 59 def define_belongs_to(name, options = {}, &block) define_associate(BelongsToReflection.new(name, options, block)) end
define_default_attrs()
click to toggle source
# File lib/flexserializer/base.rb, line 44 def define_default_attrs return unless self.class.data_default_attributes self.instance_eval &self.class.data_default_attributes end
define_group_attrs()
click to toggle source
# File lib/flexserializer/base.rb, line 49 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)
click to toggle source
# File lib/flexserializer/base.rb, line 55 def define_has_many(name, options = {}, &block) define_associate(HasManyReflection.new(name, options, block)) end
define_has_one(name, options = {}, &block)
click to toggle source
# File lib/flexserializer/base.rb, line 63 def define_has_one(name, options = {}, &block) define_associate(HasOneReflection.new(name, options, block)) end
define_options()
click to toggle source
# File lib/flexserializer/base.rb, line 67 def define_options instance_options end
Private Instance Methods
define_associate(reflection)
click to toggle source
# File lib/flexserializer/base.rb, line 104 def define_associate(reflection) key = reflection.options[:key] || reflection.name _reflections[key] = reflection end
make_all_attributes()
click to toggle source
# File lib/flexserializer/base.rb, line 99 def make_all_attributes define_default_attrs define_group_attrs end