Class: SimpleObjectSerialization::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_object_serialization/entity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Entity.



7
8
9
10
# File 'lib/simple_object_serialization/entity.rb', line 7

def initialize(object, options = {})
  @object = object
  @options = options
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/simple_object_serialization/entity.rb', line 5

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/simple_object_serialization/entity.rb', line 5

def options
  @options
end

Class Method Details

.attributesObject



28
29
30
# File 'lib/simple_object_serialization/entity.rb', line 28

def self.attributes
  @attributes ||= Attributes.new
end

.call(*params, **options, &block) ⇒ Object



12
13
14
# File 'lib/simple_object_serialization/entity.rb', line 12

def self.call(*params, **options, &block)
  new(*params, **options).call(&block)
end

.define_attribute(name, options = {}, &block) ⇒ Object



22
23
24
25
26
# File 'lib/simple_object_serialization/entity.rb', line 22

def self.define_attribute(name, options = {}, &block)
  attribute = Attribute.new(name, options, block)

  attributes.push(attribute)
end

.object_alias(name) ⇒ Object



16
17
18
19
20
# File 'lib/simple_object_serialization/entity.rb', line 16

def self.object_alias(name)
  define_method(name) do
    object
  end
end

.serialize(object, options = {}) ⇒ Object



38
39
40
41
42
# File 'lib/simple_object_serialization/entity.rb', line 38

def self.serialize(object, options = {})
  hash = ObjectSerializer.call(self, object, options)

  JSON.generate(hash)
end

.serialize_collection(collection, options = {}) ⇒ Object



32
33
34
35
36
# File 'lib/simple_object_serialization/entity.rb', line 32

def self.serialize_collection(collection, options = {})
  hash = CollectionSerializer.call(self, collection, options)

  JSON.generate(hash)
end

Instance Method Details

#callObject



44
45
46
# File 'lib/simple_object_serialization/entity.rb', line 44

def call
  self.class.attributes.hash_for(self)
end