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.
-
#define_options ⇒ Object
readonly
Returns the value of attribute define_options.
-
#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
-
#attributes(requested_attrs = nil, reload = false) ⇒ Object
override serializer methods.
- #define_attribute(attr, options = {}, &block) ⇒ Object
- #define_attributes(*attrs) ⇒ Object
- #define_default_attrs ⇒ Object
- #define_group_attrs ⇒ Object
-
#initialize(object, options = {}) ⇒ Base
constructor
A new instance of Base.
- #make_all_attributes ⇒ Object
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 = {} @group_name = [:group] @define_options = .clone 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 |
#define_options ⇒ Object (readonly)
Returns the value of attribute define_options.
22 23 24 |
# File 'lib/flexserializer/base.rb', line 22 def @define_options 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
#attributes(requested_attrs = nil, reload = false) ⇒ Object
override serializer methods
62 63 64 65 66 67 68 69 |
# File 'lib/flexserializer/base.rb', line 62 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_default_attrs ⇒ Object
49 50 51 52 |
# File 'lib/flexserializer/base.rb', line 49 def define_default_attrs return unless self.class.data_default_attributes self.instance_eval &self.class.data_default_attributes end |
#define_group_attrs ⇒ Object
54 55 56 57 58 |
# File 'lib/flexserializer/base.rb', line 54 def define_group_attrs self.class.groups.send(:[], group_name)&.each do |block| self.instance_eval(&block) end end |
#make_all_attributes ⇒ Object
44 45 46 47 |
# File 'lib/flexserializer/base.rb', line 44 def make_all_attributes define_default_attrs define_group_attrs end |