Module: Cistern::Model

Includes:
Attributes::InstanceMethods
Defined in:
lib/cistern/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



24
25
26
# File 'lib/cistern/model.rb', line 24

def collection
  @collection
end

#serviceObject

Returns the value of attribute service.



24
25
26
# File 'lib/cistern/model.rb', line 24

def service
  @service
end

Class Method Details

.included(klass) ⇒ Object



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

def self.included(klass)
  klass.send(:extend, Cistern::Attributes::ClassMethods)
  klass.send(:include, Cistern::Attributes::InstanceMethods)
  klass.send(:extend, Cistern::Model::ClassMethods)
end

.service_model(service, klass, name) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/cistern/model.rb', line 10

def self.service_model(service, klass, name)
  service.const_get(:Collections).module_eval <<-EOS, __FILE__, __LINE__
    def #{name}(attributes={})
      #{klass.name}.new(attributes.merge(service: self))
    end
  EOS
end

Instance Method Details

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



53
54
55
56
57
58
# File 'lib/cistern/model.rb', line 53

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

#hashObject



62
63
64
65
66
67
68
# File 'lib/cistern/model.rb', line 62

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

#initialize(attributes = {}) ⇒ Object



30
31
32
# File 'lib/cistern/model.rb', line 30

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

#inspectObject



26
27
28
# File 'lib/cistern/model.rb', line 26

def inspect
  Cistern.formatter.call(self)
end

#reloadObject



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

def reload
  requires :identity

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

#saveObject

Raises:

  • (NotImplementedError)


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

def save
  raise NotImplementedError
end

#service_classObject



78
79
80
# File 'lib/cistern/model.rb', line 78

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

#update(attributes) ⇒ Object



34
35
36
37
# File 'lib/cistern/model.rb', line 34

def update(attributes)
  merge_attributes(attributes)
  save
end

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



70
71
72
# File 'lib/cistern/model.rb', line 70

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

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



74
75
76
# File 'lib/cistern/model.rb', line 74

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