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

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

Instance Attribute Details

#cisternObject

Returns the value of attribute cistern.



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

def cistern
  @cistern
end

#collectionObject

Returns the value of attribute collection.



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

def collection
  @collection
end

Class Method Details

.cistern_model(cistern, klass, name) ⇒ Object



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

def self.cistern_model(cistern, klass, name)
  cistern.const_get(:Collections).module_eval "    def \#{name}(attributes={})\n  \#{klass.name}.new(attributes.merge(cistern: self))\n    end\n  EOS\nend\n", __FILE__, __LINE__

.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

Instance Method Details

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



80
81
82
83
84
85
# File 'lib/cistern/model.rb', line 80

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

#cistern_classObject



113
114
115
# File 'lib/cistern/model.rb', line 113

def cistern_class
  cistern ? cistern.class : Cistern
end

#hashObject



89
90
91
92
93
94
95
# File 'lib/cistern/model.rb', line 89

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

#initialize(attributes = {}) ⇒ Object



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

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

#inspectObject



51
52
53
# File 'lib/cistern/model.rb', line 51

def inspect
  Cistern.formatter.call(self)
end

#reloadObject



70
71
72
73
74
75
76
77
78
# File 'lib/cistern/model.rb', line 70

def reload
  requires :identity

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

#saveObject



66
67
68
# File 'lib/cistern/model.rb', line 66

def save
  fail NotImplementedError
end

#serviceObject



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

def service
  Cistern.deprecation(
    '#service is deprecated.  Please use #cistern',
    caller[0]
  )
  @cistern
end

#service=(service) ⇒ Object



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

def service=(service)
  Cistern.deprecation(
    '#service= is deprecated.  Please use #cistern=',
    caller[0]
  )
  @cistern = service
end

#service_classObject



105
106
107
108
109
110
111
# File 'lib/cistern/model.rb', line 105

def service_class
  Cistern.deprecation(
    '#service_class is deprecated.  Please use #cistern_class',
    caller[0]
  )
  cistern ? cistern.class : Cistern
end

#update(attributes) ⇒ Object

Merge #attributes and call #save. Valid and change attributes are available in Attributes::InstanceMethods#dirty_attributes

Parameters:



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

def update(attributes)
  stage_attributes(attributes)
  save
end

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



97
98
99
# File 'lib/cistern/model.rb', line 97

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) ⇒ Object



101
102
103
# 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