Sha256: 1b70ceba128ebaaed233ea4043d3e762a031c61b36e6510347f178ef11ad6a73

Contents?: true

Size: 1.02 KB

Versions: 14

Compression:

Stored size: 1.02 KB

Contents

module NinjaModel
  class Base
    define_model_callbacks :save, :create, :update, :destroy
  end

  module Persistence

    def save(*)
      run_callbacks :save do
        result = new_record? ? create : update
        changed_attributes.clear if result
        result
      end
    end

    def create
      run_callbacks :create do
        if self.class.adapter.create(self)
          @persisted = true
        end
        @persisted
      end
    end

    def update
      run_callbacks :update do
        self.class.adapter.update(self)
      end
    end

    def new_record?
      !@persisted
    end

    def destroyed?
      @destroyed
    end

    def persisted?
      @persisted && !destroyed?
    end

    def destroy
      run_callbacks :destroy do
        if self.class.adapter.destroy(self)
          @destroyed = true
        end
        @destroyed
      end
    end

    def reload
      self.class.adapter.reload(self)
    end

    def update_attributes(attributes)
      self.attributes = attributes
      save
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
ninja-model-0.7.3 lib/ninja_model/persistence.rb
ninja-model-0.7.2 lib/ninja_model/persistence.rb
ninja-model-0.7.1 lib/ninja_model/persistence.rb
ninja-model-0.7.0 lib/ninja_model/persistence.rb
ninja-model-0.6.2 lib/ninja_model/persistence.rb
ninja-model-0.6.1 lib/ninja_model/persistence.rb
ninja-model-0.6.0 lib/ninja_model/persistence.rb
ninja-model-0.5.7 lib/ninja_model/persistence.rb
ninja-model-0.5.6 lib/ninja_model/persistence.rb
ninja-model-0.5.5 lib/ninja_model/persistence.rb
ninja-model-0.5.4 lib/ninja_model/persistence.rb
ninja-model-0.5.3 lib/ninja_model/persistence.rb
ninja-model-0.5.2 lib/ninja_model/persistence.rb
ninja-model-0.5.1 lib/ninja_model/persistence.rb