Module: Chimera::Finders::ClassMethods

Defined in:
lib/chimera/finders.rb

Instance Method Summary collapse

Instance Method Details

#eachObject



9
10
11
# File 'lib/chimera/finders.rb', line 9

def each
  self.find_with_index(:all) { |obj| yield(obj) }
end

#find(key, opts = {}) ⇒ Object



13
14
15
# File 'lib/chimera/finders.rb', line 13

def find(key,opts={})
  find_many([[key,opts]])[0]
end

#find_many(key_opts_arr) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chimera/finders.rb', line 17

def find_many(key_opts_arr)
  found   = []
  threads = []
  key_opts_arr = Array(key_opts_arr).collect { |e| Array(e) }
  key_opts_arr.each do |key,opts|
    opts ||= {}
    threads << Thread.new do
      if key
        resp = self.connection(:riak_raw).fetch(self.to_s, key, opts)
        case resp.code
        when 300 then
          # siblings
          obj = self.new({},key,false)
          obj.riak_response = resp
          obj.load_sibling_attributes
          found << obj
        when 200 then
          if resp.body and yaml_hash = YAML.load(resp.body)
            hash = {}
            yaml_hash.each { |k,v| hash[k.to_sym] = v }
            obj = self.new(hash,key,false)
            obj.riak_response = resp
            found << obj
          else
            obj = self.new({},key,false)
            obj.riak_response = resp
            found << obj
          end
        when 404 then
          nil
        else
          raise(Chimera::Error::UnhandledRiakResponseCode.new(resp.code.to_s))
        end # case
      end
    end # Thread.new
  end # keys.each
  threads.each { |th| th.join }
  found
end