Class: SBDB::Cursor

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sbdb/cursor.rb

Constant Summary collapse

NEXT =
Bdb::DB_NEXT
FIRST =
Bdb::DB_FIRST
LAST =
Bdb::DB_LAST
PREV =
Bdb::DB_PREV
SET =
Bdb::DB_SET

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref) ⇒ Cursor

Returns a new instance of Cursor



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sbdb/cursor.rb', line 46

def initialize ref
	@cursor, @db = *case ref
		when Cursor  then [ref.bdb_object.dup, ref.db]
		when Bdb::Db::Cursor  then [ref]
		else [ref.bdb_object.cursor( nil, 0), ref]
		end
	if [Recno, Queue].any? {|dbt| dbt === @db }
		def get *ps
			key, val = @cursor.get( *ps)
			key.nil? ? nil : [key.unpack('I')[0], val]
		end
	end
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db



10
11
12
# File 'lib/sbdb/cursor.rb', line 10

def db
  @db
end

Class Method Details

.new(*ps) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/sbdb/cursor.rb', line 38

def self.new *ps
	ret = obj = super( *ps)
	begin ret = yield obj
	ensure obj.close
	end  if block_given?
	ret
end

Instance Method Details

#bdb_objectObject



13
14
15
# File 'lib/sbdb/cursor.rb', line 13

def bdb_object
	@cursor
end

#closeObject



16
17
18
# File 'lib/sbdb/cursor.rb', line 16

def close
	@cursor.close
end

#countObject



22
23
24
# File 'lib/sbdb/cursor.rb', line 22

def count
	@cursor.count
end

#each(key = nil, val = nil, flg = nil, nxt = nil) {|ent| ... } ⇒ Object

Yields:

  • (ent)


64
65
66
67
68
69
70
71
72
# File 'lib/sbdb/cursor.rb', line 64

def each key = nil, val = nil, flg = nil, nxt = nil
	return Enumerator.new( self, :each, key, val, flg, nxt)  unless block_given?
	nxt ||= NEXT
	ent = get key, val, flg || FIRST
	return  unless ent
	yield *ent
	yield *ent  while ent = get( key, val, nxt)
	nil
end

#first(key = nil, val = nil) ⇒ Object



25
26
27
# File 'lib/sbdb/cursor.rb', line 25

def first key = nil, val = nil
	get key, val, FIRST
end

#get(key, val, flg) ⇒ Object



19
20
21
# File 'lib/sbdb/cursor.rb', line 19

def get key, val, flg
	@cursor.get key, val, flg
end

#last(key = nil, val = nil) ⇒ Object



28
29
30
# File 'lib/sbdb/cursor.rb', line 28

def last key = nil, val = nil
	get key, val, LAST
end

#next(key = nil, val = nil) ⇒ Object



31
32
33
# File 'lib/sbdb/cursor.rb', line 31

def next key = nil, val = nil
	get key, val, NEXT
end

#prev(key = nil, val = nil) ⇒ Object



34
35
36
# File 'lib/sbdb/cursor.rb', line 34

def prev key = nil, val = nil
	get key, val, PREV
end

#reverse(key = nil, val = nil, &exe) ⇒ Object



60
61
62
# File 'lib/sbdb/cursor.rb', line 60

def reverse key = nil, val = nil, &exe
	each key, val, LAST, PREV, &exe
end