Sha256: 1fe771af89995265aa6c52f5c6cbc14d4ddbea0dec89f060b6c2462365500391

Contents?: true

Size: 1.96 KB

Versions: 22

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require_relative '../reference'

# Reference attribute type for has_one
module LedgerSync
  class ResourceAttribute
    class Reference
      class Many < Reference
        class ManyArray < Array
          include ActiveModel::Dirty

          ARRAY_METHODS_TO_OVERRIDE_WITH_DIRTY = %w[<< | []= + -].freeze

          define_attribute_methods :value

          ARRAY_METHODS_TO_OVERRIDE_WITH_DIRTY.each do |array_method|
            define_method(array_method) do |val|
              value_will_change!
              super(val)
            end
          end

          def ==(other)
            return false unless other.is_a?(ManyArray) || other.is_a?(Array)
            return false unless sorted_fingerprints(other) == sorted_fingerprints

            true
          end

          def save
            changes_applied
          end

          def sorted_fingerprints(other = nil)
            (other || self).map(&:fingerprint).sort
          end

          def value
            self
          end
        end

        module Mixin
          def self.included(base)
            base.extend(ClassMethods)
          end

          module ClassMethods
            def references_many(name, to:)
              resource_attribute = ResourceAttribute::Reference::Many.new(
                name: name,
                resource_class: self,
                to: to
              )
              resource_attributes.add resource_attribute
              _define_attribute_methods(name)
            end
          end
        end

        def initialize(args = {})
          to = args.fetch(:to)
          super(
            args.except(:to).merge(
              type: Type::ReferenceMany.new(resource_class: to),
              value: ManyArray.new
            )
          )
        end

        def changed?
          value.changed?
        end

        def changes
          value.changes
        end

        def save
          value.save
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
ledger_sync-2.6.0 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-2.5.0 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-2.4.4 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-2.3.1 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-2.2.3 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-2.2.1 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-2.2.0 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-2.0.2 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-2.0.1 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-2.0.0 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-2.0.0.pre.1 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-1.8.1 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-1.8.0 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-1.7.0 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-1.6.0 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-1.5.2 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-1.5.1 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-1.5.0 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-1.4.4 lib/ledger_sync/resource_attribute/reference/many.rb
ledger_sync-1.4.2 lib/ledger_sync/resource_attribute/reference/many.rb