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, #changed, #clone_attributes, #dirty?, #dirty_attributes, #dirty_request_attributes, #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.



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

def cistern
  @cistern
end

#collectionObject

Returns the value of attribute collection.



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

def collection
  @collection
end

Class Method Details

.cistern_model(cistern, klass, name) ⇒ Object



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

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



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

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?



84
85
86
87
88
89
# File 'lib/cistern/model.rb', line 84

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

#cistern_classObject



117
118
119
# File 'lib/cistern/model.rb', line 117

def cistern_class
  cistern ? cistern.class : Cistern
end

#hashObject



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

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

#initialize(attributes = {}) ⇒ Object



59
60
61
# File 'lib/cistern/model.rb', line 59

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

#inspectObject



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

def inspect
  Cistern.formatter.call(self)
end

#reloadObject



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

def reload
  requires :identity

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

#saveObject



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

def save
  fail NotImplementedError
end

#serviceObject



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

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

#service=(service) ⇒ Object



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

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

#service_classObject



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

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:



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

def update(attributes)
  stage_attributes(attributes)
  save
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

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



105
106
107
# File 'lib/cistern/model.rb', line 105

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