Sha256: d0eb0fbc8372ede194bf6a4e477451da768b8108949855c541d8dc28ba0bedf6

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

# Mixin for lib-specific Type functions
module LedgerSync
  module Type
    module ValueMixin
      def self.included(base)
        base.include SimplySerializable::Mixin
      end

      def assert_valid(args = {})
        return if valid_class?(args)

        value = args.fetch(:value)

        raise Error::TypeError::ValueClassError.new(
          expected: valid_classes,
          given: value.class
        )
      end

      # Do not override this method.  Override private method cast_value
      def cast(args = {})
        assert_valid(args)
        cast_value(args)
      end

      def valid?(args = {})
        valid_class?(args)
      end

      private

      # Override this method to handle different types of casting.
      def cast_value(args = {})
        value = args.fetch(:value)

        super(value)
      end

      def valid_class?(args = {})
        value = args.fetch(:value)

        return true if value.nil?
        return true if valid_classes.select { |e| value.is_a?(e) }.any?

        false
      end

      def valid_classes
        [
          Object
        ]
      end

      # def cast?
      #   false
      # end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ledger_sync-1.4.4 lib/ledger_sync/type/value_mixin.rb
ledger_sync-1.4.2 lib/ledger_sync/type/value_mixin.rb
ledger_sync-1.4.1 lib/ledger_sync/type/value_mixin.rb
ledger_sync-1.4.0 lib/ledger_sync/type/value_mixin.rb