Sha256: f0459be8b1c55f60225a15d122d0e52198a5473c0b374a8d5ec38336c431e949

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

#
# Copyright (c) 2018-present, Blue Marble Payroll, LLC
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#

module CinnamonSerial
  # This is the main parent class that all serializers must inherit from.
  class Base
    extend Dsl

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

    attr_reader :data,
                :obj,
                :opts,
                :klasses

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

      materialize_data
      execute_hydrate_blocks
    end

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

    def as_json(_options = {})
      data
    end

    def respond_to_missing?(method_sym)
      data.key?(method_sym.to_s) || super
    end

    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

    def [](attr)
      send(attr)
    end

    private

    def inherited_cinnamon_serial_specification
      self.class.inherited_cinnamon_serial_specification
    end

    def materialize_data
      @data = {}

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

      nil
    end

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cinnamon_serial-1.0.0 lib/cinnamon_serial/base.rb