Sha256: 442b2e08ca2f9324d1e89a21ea6ec5b58fa74af5f504922e2458d49ec04caea1

Contents?: true

Size: 1.2 KB

Versions: 11

Compression:

Stored size: 1.2 KB

Contents

class Cistern::Model
  extend Cistern::Attributes::ClassMethods
  include Cistern::Attributes::InstanceMethods

  attr_accessor :collection, :connection

  def inspect
    if Cistern.formatter
      Cistern.formatter.call(self)
    else
      "#<#{self.class} #{self.identity}"
    end
  end

  def initialize(attributes={})
    merge_attributes(attributes)
  end

  def save
    raise NotImplementedError
  end

  def reload
    requires :identity

    if data = collection.get(identity)
      new_attributes = data.attributes
      merge_attributes(new_attributes)
      self
    end
  end

  def ==(comparison_object)
    comparison_object.equal?(self) ||
      (comparison_object.is_a?(self.class) &&
       comparison_object.identity == self.identity &&
       !comparison_object.new_record?)
  end

  def service
    self.connection ? self.connection.class : Cistern
  end

  def wait_for(timeout = self.service.timeout, interval = self.service.poll_interval, &block)
    service.wait_for(timeout, interval) { reload && block.call(self) }
  end

  def wait_for!(timeout = self.service.timeout, interval = self.service.poll_interval, &block)
    service.wait_for!(timeout, interval) { reload && block.call(self) }
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
cistern-0.9.0 lib/cistern/model.rb
cistern-0.8.0 lib/cistern/model.rb
cistern-0.7.1 lib/cistern/model.rb
cistern-0.7.0 lib/cistern/model.rb
cistern-0.6.0 lib/cistern/model.rb
cistern-0.5.10 lib/cistern/model.rb
cistern-0.5.9 lib/cistern/model.rb
cistern-0.5.8 lib/cistern/model.rb
cistern-0.5.7 lib/cistern/model.rb
cistern-0.5.6 lib/cistern/model.rb
cistern-0.5.4 lib/cistern/model.rb