Class: EasySerializer::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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



9
10
11
# File 'lib/easy_serializer/base.rb', line 9

def initialize(object)
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

Class Method Details

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



22
23
24
25
# File 'lib/easy_serializer/base.rb', line 22

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

.attributes(*args) ⇒ Object



33
34
35
36
37
# File 'lib/easy_serializer/base.rb', line 33

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

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



27
28
29
30
31
# File 'lib/easy_serializer/base.rb', line 27

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



15
16
17
# File 'lib/easy_serializer/base.rb', line 15

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

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



39
40
41
42
# File 'lib/easy_serializer/base.rb', line 39

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

Instance Method Details

#__cacheObject



57
58
59
# File 'lib/easy_serializer/base.rb', line 57

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

#send_to_serializer(serializer, value) ⇒ Object



52
53
54
55
# File 'lib/easy_serializer/base.rb', line 52

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



45
46
47
# File 'lib/easy_serializer/base.rb', line 45

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