Class: Glyr::Database

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/glyr/database.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer) ⇒ Database

Returns a new instance of Database.



39
40
41
# File 'lib/glyr/database.rb', line 39

def initialize (pointer)
	@internal = pointer.is_a?(FFI::Pointer) ? C::Database.new(pointer) : pointer
end

Class Method Details

.create(path) ⇒ Object

create a Query object with the passed options



15
16
17
18
19
20
21
22
23
# File 'lib/glyr/database.rb', line 15

def self.create (path)
	pointer = C.glyr_db_init(path)

	if pointer.null?
		raise ArgumentError, "could not open/create database at #{path}"
	end

	wrap(pointer)
end

.finalizer(pointer) ⇒ Object

:nodoc:



31
32
33
34
35
# File 'lib/glyr/database.rb', line 31

def self.finalizer (pointer) # :nodoc:
	proc {
		C.glyr_db_destroy(pointer)
	}
end

.wrap(pointer) ⇒ Object



25
26
27
28
29
# File 'lib/glyr/database.rb', line 25

def self.wrap (pointer)
	new(pointer).tap {|x|
		ObjectSpace.define_finalizer x, finalizer(pointer)
	}
end

Instance Method Details

#delete(query) ⇒ Object



72
73
74
# File 'lib/glyr/database.rb', line 72

def delete (query)
	C.glyr_db_delete(to_native, query.to_native)
end

#each(&block) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/glyr/database.rb', line 86

def each (&block)
	C.glyr_db_foreach(to_native, proc {|query, item|
		block.call [Query.new(query), Result.new(item)]
	}, nil)

	self
end

#insert(query, results) ⇒ Object



66
67
68
69
70
# File 'lib/glyr/database.rb', line 66

def insert (query, results)
	C.glyr_db_insert(to_native, query.to_native, results.to_native)

	self
end

#lookup(query) ⇒ Object



62
63
64
# File 'lib/glyr/database.rb', line 62

def lookup (query)
	Results.wrap(C.glyr_db_lookup(to_native, query.to_native))
end

#pathObject



43
44
45
# File 'lib/glyr/database.rb', line 43

def path
	to_native[:root_path]
end

#read!Object



56
57
58
59
60
# File 'lib/glyr/database.rb', line 56

def read!
	@read = true

	self
end

#read?Boolean

Returns:

  • (Boolean)


48
# File 'lib/glyr/database.rb', line 48

def read?;  !!@read;  end

#replace(query, results) ⇒ Object



76
77
78
# File 'lib/glyr/database.rb', line 76

def replace (query, results)
	C.glyr_db_edit(to_native, query.to_native, results.to_native)
end

#swap(query, a, b) ⇒ Object



80
81
82
83
84
# File 'lib/glyr/database.rb', line 80

def swap (query, a, b)
	C.glyr_db_replace(to_native, a.respond_to?(:md5) ? a.md5 : a.to_s, query.to_native, b.to_native)

	self
end

#to_nativeObject



94
95
96
# File 'lib/glyr/database.rb', line 94

def to_native
	@internal
end

#write!Object



50
51
52
53
54
# File 'lib/glyr/database.rb', line 50

def write!
	@write = true

	self
end

#write?Boolean

Returns:

  • (Boolean)


47
# File 'lib/glyr/database.rb', line 47

def write?; !!@write; end