Class: EasySerializer::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/easy_serializer/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#option_to_value

Constructor Details

#initialize(object) ⇒ Base

Returns a new instance of Base.



7
8
9
# File 'lib/easy_serializer/base.rb', line 7

def initialize(object)
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

Class Method Details

.attribute(name, opts = {}, &block) ⇒ Object



20
21
22
23
# File 'lib/easy_serializer/base.rb', line 20

def attribute(name, opts = {}, &block)
  @__serializable_attributes ||= []
  @__serializable_attributes << Attribute.new(name, opts, block)
end

.attributes(*args) ⇒ Object



31
32
33
34
35
# File 'lib/easy_serializer/base.rb', line 31

def attributes(*args)
  args.each do |input|
    attribute(*input)
  end
end

.cache(bool, opts = {}, &block) ⇒ Object



25
26
27
28
29
# File 'lib/easy_serializer/base.rb', line 25

def cache(bool, opts = {}, &block)
  if bool
    @__cache = opts.merge(block: block)
  end
end

.call(obj) ⇒ Object Also known as: serialize, to_hash, to_h



13
14
15
# File 'lib/easy_serializer/base.rb', line 13

def call(obj)
  new(obj).serialize
end

.collection(name, opts = {}, &block) ⇒ Object



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

def collection(name, opts = {}, &block)
  @__serializable_attributes ||= []
  @__serializable_attributes << Collection.new(name, opts, block)
end

Instance Method Details

#__cacheObject



55
56
57
# File 'lib/easy_serializer/base.rb', line 55

def __cache
  self.class.instance_variable_get(:@__cache)
end

#send_to_serializer(serializer, value) ⇒ Object



50
51
52
53
# File 'lib/easy_serializer/base.rb', line 50

def send_to_serializer(serializer, value)
  return unless value
  option_to_value(serializer, value).call(value)
end

#serializeObject Also known as: to_h, to_hash



43
44
45
# File 'lib/easy_serializer/base.rb', line 43

def serialize
  @serialize ||= serialized_from_cache || _serialize
end