Class: LightSerializer::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/light_serializer/serializer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, root: nil, context: nil, meta: nil) ⇒ Serializer

Returns a new instance of Serializer.



19
20
21
22
23
24
# File 'lib/light_serializer/serializer.rb', line 19

def initialize(object, root: nil, context: nil, meta: nil)
  @object = object
  @root = root
  @context = context
  @meta = meta
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/light_serializer/serializer.rb', line 6

def context
  @context
end

#metaObject (readonly)

Returns the value of attribute meta.



6
7
8
# File 'lib/light_serializer/serializer.rb', line 6

def meta
  @meta
end

#objectObject (readonly)

Returns the value of attribute object.



6
7
8
# File 'lib/light_serializer/serializer.rb', line 6

def object
  @object
end

#rootObject (readonly)

Returns the value of attribute root.



6
7
8
# File 'lib/light_serializer/serializer.rb', line 6

def root
  @root
end

Class Method Details

.attributes(*new_attributes) ⇒ Object



8
9
10
11
12
# File 'lib/light_serializer/serializer.rb', line 8

def self.attributes(*new_attributes)
  return (@attributes || []) if new_attributes.empty?

  @attributes = @attributes ? @attributes.concat(new_attributes) : new_attributes
end

.inherited(subclass) ⇒ Object



14
15
16
17
# File 'lib/light_serializer/serializer.rb', line 14

def self.inherited(subclass)
  subclass.attributes(*attributes)
  super(subclass)
end

Instance Method Details

#to_hashObject



30
31
32
# File 'lib/light_serializer/serializer.rb', line 30

def to_hash
  meta ? hashed_object.merge!(meta: meta) : hashed_object
end

#to_jsonObject



26
27
28
# File 'lib/light_serializer/serializer.rb', line 26

def to_json
  Oj.dump(to_hash, mode: :compat)
end