Class: Relaxo::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxo/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, branch, metadata = {}) ⇒ Database

Returns a new instance of Database.



31
32
33
34
35
36
37
38
# File 'lib/relaxo/database.rb', line 31

def initialize(path, branch,  = {})
	@path = path
	@metadata = 
	
	@repository = Rugged::Repository.new(path)
	
	@branch = branch
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



45
46
47
# File 'lib/relaxo/database.rb', line 45

def 
  @metadata
end

#pathObject (readonly)

Returns the value of attribute path.



44
45
46
# File 'lib/relaxo/database.rb', line 44

def path
  @path
end

#repositoryObject (readonly)

Returns the value of attribute repository.



46
47
48
# File 'lib/relaxo/database.rb', line 46

def repository
  @repository
end

Instance Method Details

#[](key) ⇒ Object



59
60
61
# File 'lib/relaxo/database.rb', line 59

def [] key
	@metadata[key]
end

#clear!Object

Completely clear out the database.



49
50
51
52
53
# File 'lib/relaxo/database.rb', line 49

def clear!
	if head = @repository.branches[@branch]
		@repository.references.delete(head)
	end
end

#commit(**options) ⇒ Object

During the execution of the block, changes don’t get stored immediately, so reading from the dataset (from outside the block) will continue to return the values that were stored in the configuration when the transaction was started.

Returns:

  • the result of the block.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/relaxo/database.rb', line 65

def commit(**options)
	result = nil
	
	track_time(options[:message]) do
		catch(:abort) do
			begin
				parent, tree = latest_commit
				
				changeset = Changeset.new(@repository, tree)
				
				result = yield changeset
			end until apply(parent, changeset, **options)
		end
	end
	
	return result
end

#configObject



40
41
42
# File 'lib/relaxo/database.rb', line 40

def config
	@repository.config
end

#current {|dataset| ... } ⇒ Object

Efficient point-in-time read-only access.

Yields:

  • (dataset)


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

def current
	_, tree = latest_commit
	
	dataset = Dataset.new(@repository, tree)
	
	yield dataset if block_given?
	
	return dataset
end

#empty?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/relaxo/database.rb', line 55

def empty?
	@repository.empty?
end

#history(path) ⇒ Object

revision history of given object



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
# File 'lib/relaxo/database.rb', line 95

def history(path)
	head, _ = latest_commit
	
	walker = Rugged::Walker.new(@repository) # Sounds like 'Walker, Texas Ranger'...
	walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_REVERSE)
	walker.push(head.oid)
	
	commits = []
	
	old_oid = nil
	
	walker.each do |commit|
		dataset = Dataset.new(@repository, commit.tree)
		oid = dataset.read(path).oid
		
		if oid != old_oid # modified
			yield commit if block_given?
			commits << commit
			old_oid = oid
		end
		
		break if oid.nil? && !old_oid.nil? # deleted or moved
	end
	
	return commits
end