Module: MassiveRecord::ORM::Finders::ClassMethods

Defined in:
lib/massive_record/orm/finders.rb

Instance Method Summary collapse

Instance Method Details

#all(*args) ⇒ Object



75
76
77
# File 'lib/massive_record/orm/finders.rb', line 75

def all(*args)
  find(:all, *args)
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/massive_record/orm/finders.rb', line 97

def exists?(id)
  !!find(id) rescue false
end

#find(*args) ⇒ Object

Interface for retrieving objects based on key. Has some convenience behaviour like find :first, :last, :all.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
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
56
57
58
59
60
61
62
63
64
65
# File 'lib/massive_record/orm/finders.rb', line 11

def find(*args)
  options = args.extract_options!.to_options
  raise ArgumentError.new("At least one argument required!") if args.empty?
  raise RecordNotFound.new("Can't find a #{model_name.human} without an ID.") if args.first.nil?
  raise ArgumentError.new("Sorry, conditions are not supported!") if options.has_key? :conditions
  args << options

  type = args.shift if args.first.is_a? Symbol
  find_many = type == :all
  expected_result_size = nil

  return (find_many ? [] : nil) unless table.exists?
  
  result_from_table = if type
                        table.send(type, *args) # first() / all()
                      else
                        options = args.extract_options!
                        what_to_find = args.first
                        expected_result_size = 1

                        if args.first.kind_of?(Array)
                          find_many = true
                        elsif args.length > 1
                          find_many = true
                          what_to_find = args
                        end

                        expected_result_size = what_to_find.length if what_to_find.is_a? Array
                        table.find(what_to_find, options)
                      end

  # Filter out unexpected IDs (unless type is set (all/first), in that case
  # we have no expectations on the returned rows' ids)
  unless type || result_from_table.blank?
    if find_many
      result_from_table.select! { |result| what_to_find.include? result.id }
    else 
      if result_from_table.id != what_to_find
        result_from_table = nil
      end
    end
  end

  raise RecordNotFound.new("Could not find #{model_name} with id=#{what_to_find}") if result_from_table.blank? && type.nil?
  
  if find_many && expected_result_size && expected_result_size != result_from_table.length
    raise RecordNotFound.new("Expected to find #{expected_result_size} records, but found only #{result_from_table.length}")
  end
  
  records = [result_from_table].compact.flatten.collect do |row|
    instantiate(transpose_hbase_columns_to_record_attributes(row))
  end

  find_many ? records : records.first
end

#find_each(*args) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/massive_record/orm/finders.rb', line 88

def find_each(*args)
  find_in_batches(*args) do |rows|
    rows.each do |row|
      yield row
    end
  end
end

#find_in_batches(*args) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/massive_record/orm/finders.rb', line 79

def find_in_batches(*args)
  table.find_in_batches(*args) do |rows|
    records = rows.collect do |row|
      instantiate(transpose_hbase_columns_to_record_attributes(row))
    end    
    yield records
  end
end

#first(*args) ⇒ Object



67
68
69
# File 'lib/massive_record/orm/finders.rb', line 67

def first(*args)
  find(:first, *args)
end

#last(*args) ⇒ Object



71
72
73
# File 'lib/massive_record/orm/finders.rb', line 71

def last(*args)
  raise "Sorry, not implemented!"
end