Sha256: 0f70d948b77b1d863d53c1250c658160f2e0906ffb2386c3fddd2f856e6fbb52

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

require "active_model"

module Duracloud
  class AbstractEntity
    include ActiveModel::Model
    extend ActiveModel::Callbacks

    define_model_callbacks :save, :delete, :load_properties
    after_save :persisted!
    after_save :reset_properties
    after_load_properties :persisted!
    before_delete :reset_properties
    after_delete :deleted!
    after_delete :freeze

    def save
      raise Error, "Cannot save deleted #{self.class}." if deleted?
      run_callbacks :save do
        do_save
      end
    end

    def delete
      raise Error, "Cannot delete, already deleted." if deleted?
      run_callbacks :delete do
        do_delete
      end
    end

    def persisted?
      !!@persisted
    end

    def deleted?
      !!@deleted
    end

    # Return the properties associated with this resource,
    #   loading from Duracloud if necessary.
    # @return [Duracloud::Properties] the properties
    # @raise [Duracloud::NotFoundError] if the resource is marked persisted
    #   but does not exist in Duracloud
    def properties
      load_properties if persisted? && @properties.nil?
      @properties ||= Properties.new
    end


    def load_properties
      run_callbacks :load_properties do
        do_load_properties
      end
    end

    private

    def do_load_properties
      raise NotImplementedError, "Subclasses must implement `#do_load_properties` private method."
    end

    def persisted!
      @persisted = true
    end

    def deleted!
      @deleted = true
      @persisted = false
    end

    def do_delete
      raise NotImplementedError, "Subclasses must implement `do_delete`."
    end

    def do_save
      raise NotImplementedError, "Subclasses must implement `do_save`."
    end

    def properties=(props)
      @properties = Properties.new(props)
    end

    def reset_properties
      @properties = nil
    end

    def properties_class
      Properties
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
duracloud-client-0.9.1 lib/duracloud/abstract_entity.rb
duracloud-client-0.9.0 lib/duracloud/abstract_entity.rb
duracloud-client-0.8.0 lib/duracloud/abstract_entity.rb
duracloud-client-0.7.2 lib/duracloud/abstract_entity.rb
duracloud-client-0.7.1 lib/duracloud/abstract_entity.rb
duracloud-client-0.7.0 lib/duracloud/abstract_entity.rb