Class: Knj::Db::Idquery
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 = Knj::Db::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 |
# File 'lib/knj/knjdb/idquery.rb', line 13 def initialize(args, &block) @args = args @ids = [] @debug = @args[:debug] if @args[:query] @args[:db].q(@args[:query]) do |data| @args[:col] = data.keys.first if !@args[:col] if data.is_a?(Array) @ids << data.first else @ids << data[@args[:col]] end end end @args[:col] = :id if !@args[:col] @args[:size] = 200 if !@args[:size] if block raise "No query was given but a block was." if !@args[:query] self.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/knj/knjdb/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
71 72 73 74 75 |
# File 'lib/knj/knjdb/idquery.rb', line 71 def each while data = self.fetch yield(data) end end |
#fetch ⇒ Object
Fetches results.
Examples
data = idq.fetch #=> Hash
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/knj/knjdb/idquery.rb', line 42 def fetch return nil if !@args if @res data = @res.fetch if @res @res = nil if !data return data if data end @res = new_res if !@res if !@res destroy return nil end data = @res.fetch if !data destroy return nil end return data end |