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

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

Instance Method Summary collapse

Instance Method Details

#default_scope(scope) ⇒ Object

Sets a default scope which will be used for calls like find, first, all etc. Makes it possible to for instance set default column families to load on all calls to the database.



63
64
65
66
67
68
69
70
71
72
# File 'lib/massive_record/orm/finders.rb', line 63

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

This do_find method is not very nice it’s logic should be re-factored at some point.

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/massive_record/orm/finders.rb', line 88

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
  what_to_find = []
  result_from_table = []
  
  find_many, expected_result_size, what_to_find, result_from_table = query_hbase(type, args, find_many)

  # 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 true if a record do exist

Returns:

  • (Boolean)


44
45
46
# File 'lib/massive_record/orm/finders.rb', line 44

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

#find_each(*args) ⇒ Object

Similar to all, except that this will use find_in_batches behind the scene.



32
33
34
35
36
37
38
# File 'lib/massive_record/orm/finders.rb', line 32

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

#find_in_batches(*args) ⇒ Object

Find records in batches. Makes it easier to work with big data sets where you don’t want to load every record up front.



19
20
21
22
23
24
25
26
# File 'lib/massive_record/orm/finders.rb', line 19

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

#finder_scopeObject

Entry point for method delegation like find, first, all etc.



53
54
55
# File 'lib/massive_record/orm/finders.rb', line 53

def finder_scope
  default_scoping || unscoped
end

#unscopedObject

Returns an fresh scope object with no limitations set by for instance the default scope



78
79
80
# File 'lib/massive_record/orm/finders.rb', line 78

def unscoped
  Scope.new(self)
end