Sha256: a9fc7983d1987f30c35fea439c2454fde326d221676d61256e071ac12c27750f

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require 'ledger_sync/type/value'

Gem.find_files('ledger_sync/type/**/*.rb').each do |path|
  require path
end

Gem.find_files('ledger_sync/resource_attribute/**/*.rb').each do |path|
  require path
end

module LedgerSync
  class ResourceAttribute
    include Fingerprintable::Mixin
    include SimplySerializable::Mixin
    include Util::Mixins::DupableMixin

    attr_reader :name,
                :reference,
                :resource_class,
                :type,
                :value

    def initialize(args = {})
      @name           = args.fetch(:name).to_sym
      @resource_class = args.fetch(:resource_class)
      @type           = args.fetch(:type)
      @value          = args.fetch(:value, nil)

      @type = type.new if type.respond_to?(:new) && !type.is_a?(Type::Value)

      raise "Invalid Type: #{type}" unless type.is_a?(ActiveModel::Type::Value)
    end

    def assert_valid(args = {})
      value = args.fetch(:value)

      return if valid_with?(value: value)

      raise ResourceAttributeError::TypeError.new(
        attribute: self,
        resource_class: resource_class,
        value: value
      )
    end

    def cast(value)
      type.cast(value)
    end

    # This is for ActiveModel::Dirty, since we define @attributes
    # def forgetting_assignment; end

    def reference?
      is_a?(Reference)
    end

    def references_many?
      is_a?(Reference::Many)
    end

    def valid_with?(value:)
      type.valid_without_casting?(value: value)
    end

    def value=(val)
      assert_valid(value: val)
      @value = type.cast(val)
    end

    def will_change?(val)
      assert_valid(value: val)
      value != type.cast(val)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ledger_sync-1.3.5 lib/ledger_sync/resource_attribute.rb