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

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

Instance Method Summary collapse

Instance Method Details

#default_scope(scope) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/massive_record/orm/finders.rb', line 103

def default_scope(scope)
  self.default_scoping =  case scope
                            when Scope, nil
                              scope
                            when Hash
                              Scope.new(self, :find_options => scope)
                            else
                              raise "Don't know how to set scope with #{scope.class}."
                            end
end

#do_find(*args) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


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
66
67
68
69
70
71
72
# File 'lib/massive_record/orm/finders.rb', line 15

def do_find(*args) # :nodoc:
  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

  skip_expected_result_check = options.delete(:skip_expected_result_check)

  args << options

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

  return (find_many ? [] : raise(RecordNotFound.new("Could not find #{model_name} with id=#{args.first}"))) 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.try(: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 && !skip_expected_result_check && 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

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/massive_record/orm/finders.rb', line 94

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

#find_each(*args) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/massive_record/orm/finders.rb', line 85

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

#find_in_batches(*args) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/massive_record/orm/finders.rb', line 74

def find_in_batches(*args)
  return unless table.exists?

  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

#finder_scopeObject



99
100
101
# File 'lib/massive_record/orm/finders.rb', line 99

def finder_scope
  default_scoping || unscoped
end

#unscopedObject



114
115
116
# File 'lib/massive_record/orm/finders.rb', line 114

def unscoped
  Scope.new(self)
end