module Cistern::Model

Attributes

cistern[RW]
collection[RW]

Public Class Methods

cistern_model(cistern, klass, name) click to toggle source
# File lib/cistern/model.rb, line 14
  def self.cistern_model(cistern, klass, name)
    cistern.const_get(:Collections).module_eval <<-EOS, __FILE__, __LINE__
      def #{name}(attributes={})
    #{klass.name}.new(attributes.merge(cistern: self))
      end
    EOS
  end
included(klass) click to toggle source
# File lib/cistern/model.rb, line 7
def self.included(klass)
  klass.send(:extend, Cistern::Attributes::ClassMethods)
  klass.send(:include, Cistern::Attributes::InstanceMethods)
  klass.send(:extend, Cistern::Model::ClassMethods)
  klass.send(:extend, Cistern::Associations)
end
new(attributes = {}) click to toggle source
# File lib/cistern/model.rb, line 59
def initialize(attributes = {})
  merge_attributes(attributes)
end

Public Instance Methods

==(comparison_object) click to toggle source
Calls superclass method
# File lib/cistern/model.rb, line 84
def ==(comparison_object)
  super ||
    (comparison_object.is_a?(self.class) &&
     comparison_object.identity == identity &&
     !comparison_object.new_record?)
end
Also aliased as: eql?
cistern_class() click to toggle source
# File lib/cistern/model.rb, line 117
def cistern_class
  cistern ? cistern.class : Cistern
end
eql?(comparison_object)
Alias for: ==
hash() click to toggle source
Calls superclass method
# File lib/cistern/model.rb, line 93
def hash
  if identity
    [self.class, identity].join(':').hash
  else
    super
  end
end
inspect() click to toggle source
# File lib/cistern/model.rb, line 55
def inspect
  Cistern.formatter.call(self)
end
reload() click to toggle source
# File lib/cistern/model.rb, line 74
def reload
  requires :identity

  if data = collection.get(identity)
    new_attributes = data.attributes
    merge_attributes(new_attributes)
    self
  end
end
save() click to toggle source
# File lib/cistern/model.rb, line 70
def save
  fail NotImplementedError
end
service() click to toggle source
# File lib/cistern/model.rb, line 47
def service
  Cistern.deprecation(
    '#service is deprecated.  Please use #cistern',
    caller[0]
  )
  @cistern
end
service=(service) click to toggle source
# File lib/cistern/model.rb, line 39
def service=(service)
  Cistern.deprecation(
    '#service= is deprecated.  Please use #cistern=',
    caller[0]
  )
  @cistern = service
end
service_class() click to toggle source
# File lib/cistern/model.rb, line 109
def service_class
  Cistern.deprecation(
    '#service_class is deprecated.  Please use #cistern_class',
    caller[0]
  )
  cistern ? cistern.class : Cistern
end
update(attributes) click to toggle source

Merge attributes and call {#save}. Valid and change attributes are available in {#dirty_attributes} @param attributes [Hash]

# File lib/cistern/model.rb, line 65
def update(attributes)
  stage_attributes(attributes)
  save
end
wait_for(timeout = cistern_class.timeout, interval = cistern_class.poll_interval, &block) click to toggle source
# File lib/cistern/model.rb, line 101
def wait_for(timeout = cistern_class.timeout, interval = cistern_class.poll_interval, &block)
  cistern_class.wait_for(timeout, interval) { reload && block.call(self) }
end
wait_for!(timeout = cistern_class.timeout, interval = cistern_class.poll_interval, &block) click to toggle source
# File lib/cistern/model.rb, line 105
def wait_for!(timeout = cistern_class.timeout, interval = cistern_class.poll_interval, &block)
  cistern_class.wait_for!(timeout, interval) { reload && block.call(self) }
end