Class: Baza::Idquery
- Inherits:
-
Object
- Object
- Baza::Idquery
- Defined in:
- lib/baza/idquery.rb
Overview
This class takes a lot of IDs and runs a query against them.
Instance Attribute Summary collapse
-
#ids ⇒ Object
readonly
An array containing all the IDs that will be looked up.
Instance Method Summary collapse
-
#each ⇒ Object
Yields a block for every result.
-
#fetch ⇒ Object
Fetches results.
-
#initialize(args, &block) ⇒ Idquery
constructor
Constructor.
Constructor Details
#initialize(args, &block) ⇒ Idquery
Constructor.
Examples
idq = Baza::Idquery(db: db, table: :users) idq.ids + [1, 5, 9] idq.each do |user|
print "Name: #{user[:name]}\n"
end
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 |
# File 'lib/baza/idquery.rb', line 13 def initialize(args, &block) @args = args @db = args.fetch(:db) @ids = [] @debug = @args[:debug] if @args[:query] @db.q(@args.fetch(:query)) do |data| @args[:col] = data.keys.first unless @args[:col] if data.is_a?(Array) @ids << data.first else @ids << data[@args[:col]] end end end @args[:col] = :id unless @args[:col] @args[:size] = 200 unless @args[:size] if block raise "No query was given but a block was." unless @args[:query] each(&block) end end |
Instance Attribute Details
#ids ⇒ Object (readonly)
An array containing all the IDs that will be looked up.
4 5 6 |
# File 'lib/baza/idquery.rb', line 4 def ids @ids end |
Instance Method Details
#each ⇒ Object
Yields a block for every result.
Examples
idq.each do |data|
print "Name: #{data[:name]}\n"
end
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/baza/idquery.rb', line 72 def each loop do data = fetch if data yield data else break end end end |
#fetch ⇒ Object
Fetches results.
Examples
data = idq.fetch #=> Hash
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/baza/idquery.rb', line 43 def fetch return nil unless @args if @res data = @res.fetch if @res @res = nil unless data return data if data end @res = new_res unless @res unless @res destroy return nil end data = @res.fetch unless data destroy return nil end data end |