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, #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.



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

def cistern
  @cistern
end

#collectionObject

Returns the value of attribute collection.



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

def collection
  @collection
end

Class Method Details

.cistern_model(cistern, klass, name) ⇒ Object



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

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
# 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)
end

Instance Method Details

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



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

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

#cistern_classObject



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

def cistern_class
  cistern ? cistern.class : Cistern
end

#hashObject



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

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

#initialize(attributes = {}) ⇒ Object



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

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

#inspectObject



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

def inspect
  Cistern.formatter.call(self)
end

#reloadObject



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

def reload
  requires :identity

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

#saveObject



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

def save
  fail NotImplementedError
end

#serviceObject



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

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

#service=(service) ⇒ Object



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

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

#service_classObject



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

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:



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

def update(attributes)
  stage_attributes(attributes)
  save
end

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



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

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



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

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