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.



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

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.



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

def context
  @context
end

#metaObject (readonly)

Returns the value of attribute meta.



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

def meta
  @meta
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

Class Method Details

.attributes(*new_attributes) ⇒ Object



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

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

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

.inherited(subclass) ⇒ Object



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

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

Instance Method Details

#to_hashObject



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

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

#to_jsonObject



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

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