Module: SimplyCouch::InstanceMethods

Defined in:
lib/simply_couch/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



7
8
9
# File 'lib/simply_couch/instance_methods.rb', line 7

def ==(other)
  other.kind_of?(SimplyCouch::Model) && other._id == _id && other._rev == _rev
end

#attributes=(attr) ⇒ Object



74
75
76
# File 'lib/simply_couch/instance_methods.rb', line 74

def attributes=(attr)
  super(_remove_protected_attributes(attr))
end

#deleted?Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
# File 'lib/simply_couch/instance_methods.rb', line 92

def deleted?
  if self.class.soft_deleting_enabled?
    !send(self.class.soft_delete_attribute).nil?
  else
    false
  end
end

#destroy(override_soft_delete = false) ⇒ Object Also known as: delete



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/simply_couch/instance_methods.rb', line 40

def destroy(override_soft_delete=false)
  check_and_destroy_dependents
  if self.class.soft_deleting_enabled? && !override_soft_delete
    # soft-delete
    _mark_as_deleted
  else
    if self.class.soft_deleting_enabled? && deleted?
      # really deleting a previously soft-deleted object - skipping callbacks
      self.class.database.destroy_document(self, false)
    else # deleting a normal object or a soft-deletable object that was not soft-deleted before
      self.class.database.destroy_document(self, true)
    end
    freeze
  end
  self
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/simply_couch/instance_methods.rb', line 11

def eql?(other)
  self.==(other)
end

#initialize(attributes = {}, &blk) ⇒ Object



3
4
5
6
# File 'lib/simply_couch/instance_methods.rb', line 3

def initialize(attributes = {}, &blk)
  super(_remove_protected_attributes(attributes))
  blk.call(self) if blk
end

#reloadObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/simply_couch/instance_methods.rb', line 78

def reload
  @children = nil
  @descendants = nil
  instance = self.class.find(_id, :with_deleted => true)
  instance.attributes.each do |attribute, value|
    send "#{attribute}=", value
  end
  self._rev = instance._rev
  self._attachments = instance._attachments # CouchDB internal, not a property
  clear_changes_information
  reset_association_caches
  self
end

#save(validate = true) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/simply_couch/instance_methods.rb', line 15

def save(validate = true)
  validate = validate[:validate] if validate.is_a?(Hash) && validate.has_key?(:validate)
  result = retry_on_conflict do
    retry_on_connection_error do
      self.class.database.save_document(self, validate)
    end
  end
  # ActiveModel implementations
  @previously_changed = changes
  #@changed_attributes.clear
  return result
end

#save!Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/simply_couch/instance_methods.rb', line 28

def save!
  result = retry_on_conflict do
    retry_on_connection_error do
      self.class.database.save_document!(self)
    end
  end
  # ActiveModel implementations
  @previously_changed = changes
  #@changed_attributes.clear
  return result
end

#serializable_hashObject



100
101
102
# File 'lib/simply_couch/instance_methods.rb', line 100

def serializable_hash(*)
  {'id' => self.id}.merge attributes
end

#update_attributes(attributes = {}) ⇒ Object Also known as: update



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/simply_couch/instance_methods.rb', line 58

def update_attributes(attributes = {})
  parent_id_present = attributes.delete(:parent_id) || attributes.delete('parent_id')
  self.attributes = attributes
  if parent_id_present
    if valid?
      self.parent_id = parent_id_present
      save # only for return value, revalidate etc.
    else
      return false
    end
  else
    save
  end
end