Class: Flexserializer::Base
- Inherits:
-
ActiveModel::Serializer
- Object
- ActiveModel::Serializer
- Flexserializer::Base
- Defined in:
- lib/flexserializer/base.rb
Class Attribute Summary collapse
-
.data_default_attributes ⇒ Object
Returns the value of attribute data_default_attributes.
-
.groups ⇒ Object
Returns the value of attribute groups.
Instance Attribute Summary collapse
-
#_attributes_data ⇒ Object
readonly
Returns the value of attribute _attributes_data.
-
#_reflections ⇒ Object
readonly
Returns the value of attribute _reflections.
-
#group_name ⇒ Object
readonly
Returns the value of attribute group_name.
Class Method Summary collapse
- .default_attributes(&block) ⇒ Object
- .group(*group_names, &block) ⇒ Object
- .inherited(base) ⇒ Object
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
A new instance of Base.
Constructor Details
#initialize(object, options = {}) ⇒ Base
Returns a new instance of Base.
24 25 26 27 28 29 30 |
# File 'lib/flexserializer/base.rb', line 24 def initialize(object, = {}) super(object, ) @_attributes_data = {} @_reflections = {} @group_name = [:group] make_all_attributes end |
Class Attribute Details
.data_default_attributes ⇒ Object
Returns the value of attribute data_default_attributes.
4 5 6 |
# File 'lib/flexserializer/base.rb', line 4 def data_default_attributes @data_default_attributes end |
.groups ⇒ Object
Returns the value of attribute groups.
4 5 6 |
# File 'lib/flexserializer/base.rb', line 4 def groups @groups end |
Instance Attribute Details
#_attributes_data ⇒ Object (readonly)
Returns the value of attribute _attributes_data.
22 23 24 |
# File 'lib/flexserializer/base.rb', line 22 def _attributes_data @_attributes_data end |
#_reflections ⇒ Object (readonly)
Returns the value of attribute _reflections.
22 23 24 |
# File 'lib/flexserializer/base.rb', line 22 def _reflections @_reflections end |
#group_name ⇒ Object (readonly)
Returns the value of attribute group_name.
22 23 24 |
# File 'lib/flexserializer/base.rb', line 22 def group_name @group_name end |
Class Method Details
.default_attributes(&block) ⇒ Object
10 11 12 |
# File 'lib/flexserializer/base.rb', line 10 def default_attributes(&block) self.data_default_attributes = block end |
.group(*group_names, &block) ⇒ Object
14 15 16 17 18 19 |
# 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) ⇒ Object
6 7 8 |
# File 'lib/flexserializer/base.rb', line 6 def inherited(base) base.groups = {} end |
Instance Method Details
#associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# 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, , include_slice) y.yield association end end end |
#attributes(requested_attrs = nil, reload = false) ⇒ Object
override serializer methods
73 74 75 76 77 78 79 80 |
# 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) ⇒ Object
39 40 41 42 |
# File 'lib/flexserializer/base.rb', line 39 def define_attribute(attr, = {}, &block) key = .fetch(:key, attr) _attributes_data[key] = Attribute.new(attr, , block) end |
#define_attributes(*attrs) ⇒ Object
32 33 34 35 36 37 |
# 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) ⇒ Object
59 60 61 |
# File 'lib/flexserializer/base.rb', line 59 def define_belongs_to(name, = {}, &block) define_associate(BelongsToReflection.new(name, , block)) end |
#define_default_attrs ⇒ Object
44 45 46 47 |
# 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 ⇒ Object
49 50 51 52 53 |
# 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) ⇒ Object
55 56 57 |
# File 'lib/flexserializer/base.rb', line 55 def define_has_many(name, = {}, &block) define_associate(HasManyReflection.new(name, , block)) end |
#define_has_one(name, options = {}, &block) ⇒ Object
63 64 65 |
# File 'lib/flexserializer/base.rb', line 63 def define_has_one(name, = {}, &block) define_associate(HasOneReflection.new(name, , block)) end |
#define_options ⇒ Object
67 68 69 |
# File 'lib/flexserializer/base.rb', line 67 def end |