Class: ClipArray

Inherits:
Array
  • Object
show all
Defined in:
lib/clip_array.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClipArray

Returns a new instance of ClipArray.



34
35
36
37
38
39
40
# File 'lib/clip_array.rb', line 34

def initialize
	clear
	@file = nil
	@filename = nil
	@pref = Preferences.defaults
	print_verbose "initializing clip array"
end

Instance Attribute Details

#current_indexObject

Returns the value of attribute current_index.



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

def current_index
  @current_index
end

#dirnameObject (readonly)

Returns the value of attribute dirname.



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

def dirname
  @dirname
end

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#file_and_pathObject (readonly)

Returns the value of attribute file_and_path.



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

def file_and_path
  @file_and_path
end

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#prefObject

Returns the value of attribute pref.



20
21
22
# File 'lib/clip_array.rb', line 20

def pref
  @pref
end

Class Method Details

.create(file_and_path) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/clip_array.rb', line 177

def ClipArray.create(file_and_path)
	print_verbose "creating file '#{file_and_path}'"

	if File.exists?(file_and_path) then
		raise AppError, "File '#{file_and_path}' already exists"
	end

	begin
		file = File.open(file_and_path,"w+")
		ca = ClipArray.new.clear1
		ca.set_file(file,file_and_path)

		# test if writable
		ClipArray.obtain_exclusive_lock(file)

		ClipArray.obtain_shared_lock(file)
	rescue Exception
		file.close if file
		raise
	end

	return ca
end

.obtain_exclusive_lock(file) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/clip_array.rb', line 153

def ClipArray.obtain_exclusive_lock(file)
	release_lock(file)	# necessary for win32

	begin
		# an exclusive, non-blocking lock
		print_debug "obtaining EXCLUSIVE LOCK on '#{file.path}'"
		if not file.flock(File::LOCK_EX|File::LOCK_NB) then
			print_debug "  cannot lock"
			#file.flock(File::LOCK_EX)		TODO
		end
	rescue Exception => e
		print_debug "#{e}";
	end
end

.obtain_shared_lock(file) ⇒ Object

Locking



142
143
144
145
146
147
148
149
150
151
# File 'lib/clip_array.rb', line 142

def ClipArray.obtain_shared_lock(file)
	release_lock(file)	# necessary for win32

	# a shared, non-blocking lock
	print_debug "obtaining SHARED LOCK on '#{file.path}'"
	if not file.flock(File::LOCK_SH|File::LOCK_NB) then
		print_debug "  cannot lock"
		#file.flock(File::LOCK_SH)		TODO
	end
end

.open(file_and_path) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/clip_array.rb', line 201

def ClipArray.open(file_and_path)
	print_verbose "opening file '#{file_and_path}'"

	begin
		file = File.open(file_and_path,"r+")
		ca = ClipArray.read_mbox(file)
                ca.clear1 if ca == []
		ca.set_file(file,file_and_path)

		# test if writable
		ClipArray.obtain_exclusive_lock(file)

		ClipArray.obtain_shared_lock(file)
	rescue Exception
		file.close if file
		raise
	end

	return ca
end

.release_lock(file) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/clip_array.rb', line 168

def ClipArray.release_lock(file)
        begin
	        print_debug "releasing LOCK from '#{file.path}'"
	        file.flock(File::LOCK_UN)
	rescue Exception => e
		print_debug "#{e}"
        end
end

Instance Method Details

#<<(clip) ⇒ Object



42
43
44
45
46
# File 'lib/clip_array.rb', line 42

def <<(clip)
	push(clip)
	@current_index = length-1
	return self
end

#append(file) ⇒ Object



237
238
239
240
241
242
243
244
245
# File 'lib/clip_array.rb', line 237

def append(file)
	print_verbose "append to mbox '#{file}'"
	File.open(file,"a") do | f |
		f.print "From %s@localhost %s\n" %
			[Version::PROGRAM_NAMELC, Time.now.ctime]
		f.print(escape_from(self[@current_index].to_mbox.chomp))
		f.print("\n")
	end
end

#clearObject

Clear array and reset index



23
24
25
26
# File 'lib/clip_array.rb', line 23

def clear
	super
	@current_index = -1
end

#clear1Object

Clear array and create one empty clip



29
30
31
32
# File 'lib/clip_array.rb', line 29

def clear1
	clear
	self << Clip.new
end

#closeObject



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/clip_array.rb', line 247

def close
	save
	print_verbose "closing file '#{@filename}'"
	if @file then
		ClipArray.release_lock(@file)
		@file.close
	end
	@file = nil
	@filename = nil
	clear
end

#currentObject



134
135
136
# File 'lib/clip_array.rb', line 134

def current
	return self[@current_index]
end

#deleteObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/clip_array.rb', line 104

def delete
	if @current_index == length-1 then
		new_current_index = @current_index - 1
	else
		new_current_index = @current_index
	end

	print_verbose "deleting clip #{@current_index+1}"
	delete_at(@current_index)

	if length == 0 then
		clear1
	else
		@current_index = new_current_index
	end

end

#goto_regexp(re) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/clip_array.rb', line 55

def goto_regexp(re)
	return nil if re.nil?

	md = re.match(/^\s*#(\d+)\s*$/)
	if md then
		index = md[1].to_i
		if index < 1 or index > length then
			return nil
		end
		@current_index = index-1
		return @current_index
	else
		self.each_index do | i |
			if self[i].title.match(re) then
				@current_index = i
				return @current_index
			end
		end
		return nil
	end
end

#newObject



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/clip_array.rb', line 122

def new
	if length > 0 and self[length-1].empty? then
		@current_index = length-1
		print_verbose "last clip alread empty, going to last clip"
	else
		c = Clip.new
		push(c)
		@current_index = length-1
		print_verbose "creating new clip #{@current_index+1}"
	end
end

#nextObject



88
89
90
91
92
93
94
# File 'lib/clip_array.rb', line 88

def next
	if @current_index < length-1 then
		@current_index += 1
		print_verbose "going to next clip #{@current_index+1}"
	end
	return self
end

#prevObject



96
97
98
99
100
101
102
# File 'lib/clip_array.rb', line 96

def prev
	if @current_index > 0 then
		@current_index -= 1
		print_verbose "going to previous clip #{@current_index+1}"
	end
	return self
end

#saveObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/clip_array.rb', line 222

def save
	if @file.nil? then
		return
	end

	print_verbose "saving file '#{@filename}'"

	ClipArray.obtain_exclusive_lock(@file)
	@file.truncate(0)
	@file.rewind
	write_mbox(@file)
	@file.flush
	ClipArray.obtain_shared_lock(@file)
end

#set_file(file, file_and_path) ⇒ Object



48
49
50
51
52
53
# File 'lib/clip_array.rb', line 48

def set_file(file,file_and_path)
	@file = file
	@file_and_path = file_and_path
	@filename = File.basename(file_and_path)
	@dirname = File.dirname(file_and_path)
end