Module: Cistern::Model

Includes:
Attributes::InstanceMethods, HashSupport
Included in:
Singular
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 HashSupport

#hash_except, #hash_except!, #hash_slice, #hash_stringify_keys

Methods included from Attributes::InstanceMethods

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

Instance Attribute Details

#cisternObject

Returns the value of attribute cistern.



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

def cistern
  @cistern
end

#collectionObject

Returns the value of attribute collection.



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

def collection
  @collection
end

Class Method Details

.cistern_model(cistern, klass, name) ⇒ Object



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

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



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

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

Instance Method Details

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



82
83
84
85
86
87
# File 'lib/cistern/model.rb', line 82

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

#cistern_classObject



115
116
117
# File 'lib/cistern/model.rb', line 115

def cistern_class
  cistern ? cistern.class : Cistern
end

#hashObject



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

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

#initialize(attributes = {}) ⇒ Object



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

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

#inspectObject



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

def inspect
  Cistern.formatter.call(self)
end

#reloadObject



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

def reload
  requires :identity

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

#saveObject



68
69
70
# File 'lib/cistern/model.rb', line 68

def save
  fail NotImplementedError
end

#serviceObject



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

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

#service=(service) ⇒ Object



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

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

#service_classObject



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

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:



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

def update(attributes)
  stage_attributes(attributes)
  save
end

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



99
100
101
# File 'lib/cistern/model.rb', line 99

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



103
104
105
# File 'lib/cistern/model.rb', line 103

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