Class: Flexserializer::Base

Inherits:
ActiveModel::Serializer
  • Object
show all
Defined in:
lib/flexserializer/base.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
# File 'lib/flexserializer/base.rb', line 16

def initialize(object, options = {})
  self.class.data_default_attributes ||= []
  define_attributes(options[:group])
  super(object, options)
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

Class Method Details

.default_attributes(&block) ⇒ Object



6
7
8
# File 'lib/flexserializer/base.rb', line 6

def default_attributes(&block)
  self.data_default_attributes = block
end

.group(group_name, &block) ⇒ Object



10
11
12
13
# File 'lib/flexserializer/base.rb', line 10

def group(group_name, &block)
  self.groups           ||= {}
  self.groups[group_name] = block
end

Instance Method Details

#clear_data ⇒ Object



29
30
31
# File 'lib/flexserializer/base.rb', line 29

def clear_data
  self.class._attributes_data = {}
end

#define_attributes(group) ⇒ Object



22
23
24
25
26
27
# File 'lib/flexserializer/base.rb', line 22

def define_attributes(group)
  clear_data
  define_default_attrs
  return unless self.class.groups.keys.include?(group)
  define_group_attrs(group)
end

#define_default_attrs ⇒ Object



33
34
35
# File 'lib/flexserializer/base.rb', line 33

def define_default_attrs
  self.class.data_default_attributes.call
end

#define_group_attrs(group) ⇒ Object



37
38
39
# File 'lib/flexserializer/base.rb', line 37

def define_group_attrs(group)
  self.class.groups[group].call
end