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

#cache_key, #option_to_value

Constructor Details

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

Returns a new instance of Base.



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

def initialize(object, options = {})
  @object = object
  @options = options
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

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

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



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

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

.attributes(*args) ⇒ Object



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

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

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



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

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

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



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

def call(*args)
  new(*args).serialize
end

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



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

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

Instance Method Details

#__cacheObject



59
60
61
# File 'lib/easy_serializer/base.rb', line 59

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

#send_to_serializer(serializer, value) ⇒ Object



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

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



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

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

#to_sObject



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

def to_s; serialize.to_json end