Sha256: e1e3b0539c4390e60ca491e4914f21efa43f826f23c24748c964bdca4ab69bc2

Contents?: true

Size: 1.12 KB

Versions: 9

Compression:

Stored size: 1.12 KB

Contents

module ActiveData
  module Model
    module Persistence
      extend ActiveSupport::Concern

      module ClassMethods
        def instantiate(data)
          data = data.stringify_keys
          instance = allocate

          instance.instance_variable_set(:@initial_attributes, data.slice(*attribute_names))
          instance.send(:mark_persisted!)

          instance
        end

        def instantiate_collection(data)
          collection = Array.wrap(data).map { |attrs| instantiate attrs }
          collection = scope(collection, true) if respond_to?(:scope)
          collection
        end
      end

      def persisted?
        !!@persisted
      end

      def destroyed?
        !!@destroyed
      end

      def marked_for_destruction?
        @marked_for_destruction
      end

      def mark_for_destruction
        @marked_for_destruction = true
      end

      def _destroy
        marked_for_destruction?
      end

    private

      def mark_persisted!
        @persisted = true
        @destroyed = false
      end

      def mark_destroyed!
        @persisted = false
        @destroyed = true
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
active_data-1.2.0 lib/active_data/model/persistence.rb
active_data-1.1.7 lib/active_data/model/persistence.rb
active_data-1.1.6 lib/active_data/model/persistence.rb
active_data-1.1.5 lib/active_data/model/persistence.rb
active_data-1.1.4 lib/active_data/model/persistence.rb
active_data-1.1.3 lib/active_data/model/persistence.rb
active_data-1.1.2 lib/active_data/model/persistence.rb
active_data-1.1.1 lib/active_data/model/persistence.rb
active_data-1.1.0 lib/active_data/model/persistence.rb