Class: Cistern::Model

Inherits:
Object
  • Object
show all
Extended by:
Attributes::ClassMethods
Includes:
Attributes::InstanceMethods
Defined in:
lib/cistern/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Attributes::ClassMethods

_load, aliases, attribute, attributes, identity, ignore_attributes, ignored_attributes

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #attributes=, #changed, #dirty?, #dirty_attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #read_attribute, #requires, #requires_one, #write_attribute

Constructor Details

#initialize(attributes = {}) ⇒ Model

Returns a new instance of Model.



13
14
15
# File 'lib/cistern/model.rb', line 13

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

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



5
6
7
# File 'lib/cistern/model.rb', line 5

def collection
  @collection
end

#connectionObject Also known as: service

Returns the value of attribute connection.



5
6
7
# File 'lib/cistern/model.rb', line 5

def connection
  @connection
end

Instance Method Details

#==(comparison_object) ⇒ Object Also known as: eql?



36
37
38
39
40
41
# File 'lib/cistern/model.rb', line 36

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

#hashObject



45
46
47
48
49
50
51
# File 'lib/cistern/model.rb', line 45

def hash
  if self.identity
    [self.class, self.identity].join(":").hash
  else
    super
  end
end

#inspectObject



9
10
11
# File 'lib/cistern/model.rb', line 9

def inspect
  Cistern.formatter.call(self)
end

#reloadObject



26
27
28
29
30
31
32
33
34
# File 'lib/cistern/model.rb', line 26

def reload
  requires :identity

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

#saveObject

Raises:

  • (NotImplementedError)


22
23
24
# File 'lib/cistern/model.rb', line 22

def save
  raise NotImplementedError
end

#update(attributes) ⇒ Object



17
18
19
20
# File 'lib/cistern/model.rb', line 17

def update(attributes)
  merge_attributes(attributes)
  save
end

#wait_for(timeout = self.service.timeout, interval = self.service.poll_interval, &block) ⇒ Object



57
58
59
# File 'lib/cistern/model.rb', line 57

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

#wait_for!(timeout = self.service.timeout, interval = self.service.poll_interval, &block) ⇒ Object



61
62
63
# File 'lib/cistern/model.rb', line 61

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