Class: KeepYourHead::Database::File

Inherits:
Object
  • Object
show all
Includes:
BaseStatistic
Defined in:
lib/Keepyourhead/database/File.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseStatistic

#collectFlashcards, #count, #countActive, #countPassive, #distribution, #removeStatistics

Constructor Details

#initialize(database) ⇒ File

should not be called direct



33
34
35
36
37
38
39
40
41
42
# File 'lib/Keepyourhead/database/File.rb', line 33

def initialize(database)
	@dom = nil
	@styles = {}


	@database = database
	
	@viewers = []
	viewerAdd(self)
end

Instance Attribute Details

#changedObject (readonly)

Returns the value of attribute changed.



28
29
30
# File 'lib/Keepyourhead/database/File.rb', line 28

def changed
  @changed
end

#databaseObject (readonly)

Returns the value of attribute database.



30
31
32
# File 'lib/Keepyourhead/database/File.rb', line 30

def database
  @database
end

#filenameObject

Returns the value of attribute filename.



27
28
29
# File 'lib/Keepyourhead/database/File.rb', line 27

def filename
  @filename
end

#rootObject (readonly)

Returns the value of attribute root.



29
30
31
# File 'lib/Keepyourhead/database/File.rb', line 29

def root
  @root
end

Class Method Details

.createNew(database) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/Keepyourhead/database/File.rb', line 115

def self.createNew(database)
	ret = self.new database
	content = "<flashcard_database version=\"#{Application::FILE_VERSION}\" />"
	ret.loadContent content
	ret.setChanged
	ret
end

.open(database, filename) ⇒ Object



109
110
111
112
113
# File 'lib/Keepyourhead/database/File.rb', line 109

def self.open(database, filename)
	ret = File.new database
	ret.loadFile filename
	ret
end

Instance Method Details

#fileObject



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

def file
	self
end

#itemsObject



22
23
24
# File 'lib/Keepyourhead/database/File.rb', line 22

def items
	root.collections
end

#loadContent(contents) ⇒ Object

should not be called direct



99
100
101
102
103
104
105
106
# File 'lib/Keepyourhead/database/File.rb', line 99

def loadContent(contents)
	assert @dom == nil

	@filename = nil 
	@dom = REXML::Document.new(contents)

	reload
end

#loadFile(filename) ⇒ Object

should not be called direct



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/Keepyourhead/database/File.rb', line 86

def loadFile(filename)
	assert @dom == nil

	@filename = filename 
	
	::File.open( @filename ) { |file|
		@dom = REXML::Document.new file
	}

	reload
end

#normalizeWhitespace(parent) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/Keepyourhead/database/File.rb', line 150

def normalizeWhitespace( parent )
	if parent.kind_of?( REXML::Parent ) and 
		parent.children.select { |c| c.kind_of?(REXML::Element) }.length > 0 then
		
		remove = []
		parent.each { |node|
			remove << node if node.kind_of?(REXML::Text)
		}
		remove.each { |node| parent.delete node }
		
		
		indent = -1 #because the root is also counted
		i = parent.parent
		while i and i.kind_of?(REXML::Element) do
			indent += 1
			i = i.parent
		end

		parent.children.each { |c| parent.insert_before c, REXML::Text.new( "\n" + "  " * (indent+1), true )  }
		parent.insert_after parent.children.last, REXML::Text.new( "\n" + "  " * indent, true )
	end

	if parent.kind_of?( REXML::Parent ) then
		parent.children.each { |node|
			normalizeWhitespace node
		}
	end
end

#objectFromNode(node) ⇒ Object



144
145
146
147
# File 'lib/Keepyourhead/database/File.rb', line 144

def objectFromNode(node)
	@mapObjectFromNode[node] ||= yield
	@mapObjectFromNode[node]
end

#onItemChange(item) ⇒ Object

is his own visitor



71
72
73
# File 'lib/Keepyourhead/database/File.rb', line 71

def onItemChange(item)
	setChanged if item.file == self
end

#onItemInsert(item) ⇒ Object



77
78
79
# File 'lib/Keepyourhead/database/File.rb', line 77

def onItemInsert(item)
	setChanged if item.file == self
end

#onItemRemove(item) ⇒ Object



74
75
76
# File 'lib/Keepyourhead/database/File.rb', line 74

def onItemRemove(item)
	setChanged if item.file == self
end

#onReload(file) ⇒ Object



80
81
82
# File 'lib/Keepyourhead/database/File.rb', line 80

def onReload(file)
	@changed = false
end

#reloadObject



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/Keepyourhead/database/File.rb', line 130

def reload
	@mapObjectFromNode = {}
	
	node = REXML::XPath.first( @dom, "flashcard_database" )

	throw Exception.new("file not a flashcard database") unless node
	
#			@root = FileRoot.new self, node
	@root = objectFromNode(node) { || FileRoot.new self, node }

	viewersSend "onReload", self
end

#saveObject



180
181
182
183
184
185
# File 'lib/Keepyourhead/database/File.rb', line 180

def save
	assert @filename
	
	FileUtils::mv( @filename, @filename + "~" ) if ::File.exist?(@filename)
	saveAs @filename
end

#saveAs(filename) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/Keepyourhead/database/File.rb', line 187

def saveAs(filename)
	assert filename

	self.filename = filename
	
	normalizeWhitespace @dom.root

	::File.open( self.filename, "w" ) { |file|
		@dom.write file
	}
	@changed = false
end

#setChangedObject



200
201
202
# File 'lib/Keepyourhead/database/File.rb', line 200

def setChanged
	@changed = true
end

#style(name) ⇒ Object



208
209
210
211
212
213
214
215
216
217
# File 'lib/Keepyourhead/database/File.rb', line 208

def style(name)
	if not @styles[name] then
		s = KeepYourHead::Database::Style.getStyle(name)
		assert s
		@styles[name] = s.new(self)
	end
	
	assert @styles[name]
	@styles[name]
end

#viewerAdd(v) ⇒ Object



51
52
53
# File 'lib/Keepyourhead/database/File.rb', line 51

def viewerAdd( v )
	@viewers << v unless @viewers.include? v
end

#viewerRemove(v) ⇒ Object



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

def viewerRemove( v )
	@viewers.delete v
end

#viewersSend(name, *args) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/Keepyourhead/database/File.rb', line 59

def viewersSend( name, *args )
	assert name

	@viewers.each{ |v| 
		v.send name, *args 
	}
end