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..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) else
options = args.
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
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
|