Class: CinnamonSerial::Base

Inherits:
Object
  • Object
show all
Extended by:
Dsl
Defined in:
lib/cinnamon_serial/base.rb

Overview

This is the main parent class that all serializers must inherit from.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dsl

cinnamon_serial_specification, hydrate, serialize

Constructor Details

#initialize(obj, opts = {}, klasses = Set.new) ⇒ Base

Returns a new instance of Base.



26
27
28
29
30
31
32
33
# File 'lib/cinnamon_serial/base.rb', line 26

def initialize(obj, opts = {}, klasses = Set.new)
  @obj     = normalize_object(obj)
  @opts    = opts || {}
  @klasses = klasses

  materialize_data
  execute_hydrate_blocks
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cinnamon_serial/base.rb', line 47

def method_missing(method_sym, *arguments, &block)
  key = method_sym.to_s.sub('set_', '')

  if data.key?(method_sym.to_s)
    data[method_sym.to_s]
  elsif data.key?(key)
    @data[key] = arguments[0]
  else
    super
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#klassesObject (readonly)

Returns the value of attribute klasses.



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

def klasses
  @klasses
end

#objObject (readonly)

Returns the value of attribute obj.



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

def obj
  @obj
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

Class Method Details

.map(enumerable, opts = {}) ⇒ Object



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

def map(enumerable, opts = {})
  enumerable.map { |e| new(e, opts) }
end

Instance Method Details

#[](attr) ⇒ Object



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

def [](attr)
  send(attr)
end

#as_json(_options = {}) ⇒ Object



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

def as_json(_options = {})
  data
end

#dig_opt(*keys) ⇒ Object



35
36
37
# File 'lib/cinnamon_serial/base.rb', line 35

def dig_opt(*keys)
  opts.dig(*keys)
end

#respond_to_missing?(method_sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(method_sym, include_private = false)
  data.key?(method_sym.to_s) || super
end