Module: Chimera::Persistence::InstanceMethods

Defined in:
lib/chimera/persistence.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroyObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/chimera/persistence.rb', line 102

def destroy
  _run_destroy_callbacks do
    @riak_response = self.class.connection(:riak_raw).delete(self.class.bucket_key, self.id)
    destroy_indexes
    association_memberships.destroy
    destroy_associations
    destroy_redis_objects
    freeze
  end
end

#in_conflict?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/chimera/persistence.rb', line 22

def in_conflict?
  !self.sibling_attributes.nil?
end

#load_sibling_attributesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chimera/persistence.rb', line 26

def load_sibling_attributes
  return nil unless self.riak_response.body =~ /^Sibling/
  vtags = self.riak_response.body.split("\n")[1..-1]
  if vtags.empty?
    self.sibling_attributes = nil
  else
    self.sibling_attributes = {}
    vtags.each do |vtag|
      if resp = self.class.connection(:riak_raw).fetch(self.class.to_s, self.id, {"vtag" => vtag})
        self.sibling_attributes[vtag] = YAML.load(resp.body)
      else
        self.sibling_attributes[vtag] = {}
      end
    end
  end
end

#new?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/chimera/persistence.rb', line 18

def new?
  @new == true
end

#resolve_and_saveObject

load_sibling_attributes



43
44
45
46
47
48
# File 'lib/chimera/persistence.rb', line 43

def resolve_and_save
  if valid?
    self.sibling_attributes = nil
    save
  end
end

#saveObject Also known as: create



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chimera/persistence.rb', line 50

def save
  verify_can_save!
  check_index_constraints
  
  if new?
    _run_create_callbacks do
      _run_save_callbacks do
        save_without_callbacks
        create_indexes
      end
    end
  else
    _run_save_callbacks do
      destroy_indexes
      save_without_callbacks
      create_indexes
    end
  end
  
  true
end

#save_without_callbacksObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/chimera/persistence.rb', line 80

def save_without_callbacks
  verify_can_save!
  
  @riak_response = self.class.connection(:riak_raw).store(
    self.class.bucket_key,
    self.id, 
    YAML.dump(@attributes),
    self.vector_clock)
  
  case @riak_response.code
  when 300 then
    self.load_sibling_attributes
  when 200 then
    # all good
  else
    raise(Chimera::Error::UnhandledRiakResponseCode.new(@riak_response.code))
  end
  
  @orig_attributes = @attributes.clone
  @new = false
end

#vector_clockObject



74
75
76
77
78
# File 'lib/chimera/persistence.rb', line 74

def vector_clock
  if @riak_response and @riak_response.headers_hash
    return @riak_response.headers_hash["X-Riak-Vclock"]
  end; nil
end