Sha256: 631b9b13e4bd9ce044b35e0349eea5223b593f85f23036719540681cade16ad1

Contents?: true

Size: 1.22 KB

Versions: 18

Compression:

Stored size: 1.22 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?(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)
        return nil if args.fetch(:value).nil?

        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

18 entries across 18 versions & 1 rubygems

Version Path
ledger_sync-2.6.0 lib/ledger_sync/type/value_mixin.rb
ledger_sync-2.5.0 lib/ledger_sync/type/value_mixin.rb
ledger_sync-2.4.4 lib/ledger_sync/type/value_mixin.rb
ledger_sync-2.3.1 lib/ledger_sync/type/value_mixin.rb
ledger_sync-2.2.3 lib/ledger_sync/type/value_mixin.rb
ledger_sync-2.2.1 lib/ledger_sync/type/value_mixin.rb
ledger_sync-2.2.0 lib/ledger_sync/type/value_mixin.rb
ledger_sync-2.0.2 lib/ledger_sync/type/value_mixin.rb
ledger_sync-2.0.1 lib/ledger_sync/type/value_mixin.rb
ledger_sync-2.0.0 lib/ledger_sync/type/value_mixin.rb
ledger_sync-2.0.0.pre.1 lib/ledger_sync/type/value_mixin.rb
ledger_sync-1.8.1 lib/ledger_sync/type/value_mixin.rb
ledger_sync-1.8.0 lib/ledger_sync/type/value_mixin.rb
ledger_sync-1.7.0 lib/ledger_sync/type/value_mixin.rb
ledger_sync-1.6.0 lib/ledger_sync/type/value_mixin.rb
ledger_sync-1.5.2 lib/ledger_sync/type/value_mixin.rb
ledger_sync-1.5.1 lib/ledger_sync/type/value_mixin.rb
ledger_sync-1.5.0 lib/ledger_sync/type/value_mixin.rb