class CinnamonSerial::Base

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

Attributes

data[R]
klasses[R]
obj[R]
opts[R]

Public Class Methods

map(enumerable, opts = {}) click to toggle source
# File lib/cinnamon_serial/base.rb, line 16
def map(enumerable, opts = {})
  enumerable.map { |e| new(e, opts) }
end
new(obj, opts = {}, klasses = Set.new) click to toggle source
# 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

Public Instance Methods

[](attr) click to toggle source
# File lib/cinnamon_serial/base.rb, line 59
def [](attr)
  send(attr)
end
as_json(_options = {}) click to toggle source
# File lib/cinnamon_serial/base.rb, line 39
def as_json(_options = {})
  data
end
dig_opt(*keys) click to toggle source
# File lib/cinnamon_serial/base.rb, line 35
def dig_opt(*keys)
  opts.dig(*keys)
end
method_missing(method_sym, *arguments, &block) click to toggle source
Calls superclass method
# 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
respond_to_missing?(method_sym, include_private = false) click to toggle source
Calls superclass method
# 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

Private Instance Methods

execute_hydrate_blocks() click to toggle source
# File lib/cinnamon_serial/base.rb, line 83
def execute_hydrate_blocks
  inherited_cinnamon_serial_specification.hydrate_blocks.each do |block|
    if block && block.arity == 1
      block.call(self)
    elsif block
      instance_eval(&block)
    end
  end

  nil
end
inherited_cinnamon_serial_specification() click to toggle source
# File lib/cinnamon_serial/base.rb, line 65
def inherited_cinnamon_serial_specification
  self.class.inherited_cinnamon_serial_specification
end
materialize_data() click to toggle source
# File lib/cinnamon_serial/base.rb, line 69
def materialize_data
  @data = {}

  # Soft dependency on ActiveSupport.
  # If it is understood how to create indifferently accessible hashes, then let's prefer that.
  @data = @data.with_indifferent_access if @data.respond_to?(:with_indifferent_access)

  inherited_cinnamon_serial_specification.attribute_map.each do |key, options|
    @data[key.to_s] = options.resolve(self, key)
  end

  nil
end
normalize_object(obj) click to toggle source

We would like to support hashes, but would rather stay object-based. So what we can do is just simply convert hashes to OpenStruct objects so they behave more like objects.

# File lib/cinnamon_serial/base.rb, line 98
def normalize_object(obj)
  obj.is_a?(Hash) ? OpenStruct.new(obj) : obj
end