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

}}}1



42
43
44
45
46
47
48
49
50
# File 'lib/clip_array.rb', line 42

def initialize
#{{{1

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

Instance Attribute Details

#current_indexObject

Returns the value of attribute current_index.



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

def current_index
  @current_index
end

#dirnameObject (readonly)

Returns the value of attribute dirname.



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

def dirname
  @dirname
end

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#file_and_pathObject (readonly)

Returns the value of attribute file_and_path.



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

def file_and_path
  @file_and_path
end

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#prefObject

Returns the value of attribute pref.



26
27
28
# File 'lib/clip_array.rb', line 26

def pref
  @pref
end

Class Method Details

.create(file_and_path) ⇒ Object

def ClipArray.load_from_yaml(filename) filename = get_filename(filename,:open) ca = self.new

puts “# loading clips from ‘#{filename’, format yaml” if $VERBOSE

begin yaml = YAML::load_file(filename) yaml.each_index do | i | clip = Clip.from_hash(yaml) puts “# ‘#ClipArray.clipclip.title’ loaded” if $VERBOSE ca = clip end rescue SystemCallError puts “# loading failed, continuing with empty clips” if $VERBOSE end

ca.current_index = ca.length - 1 return ca end #}}}1

def ClipArray.load_from_mbox(filename) ca = self.new str = nil

puts “# loading clips from ‘#{filename’, format mbox” if $VERBOSE

begin file = open(get_filename(filename,:open),“r”) ca = ClipArray.read_mbox(file) ensure file.close end

return ca end #}}}1



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/clip_array.rb', line 215

def ClipArray.create(file_and_path)
#{{{1

  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

    obtain_exclusive_lock(file)

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

  return ca
end

.open(file_and_path) ⇒ Object

}}}1



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/clip_array.rb', line 240

def ClipArray.open(file_and_path)
#{{{1

  print_verbose "opening file '#{file_and_path}'"

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

    # test if writable

    obtain_exclusive_lock(file)

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

  return ca
end

Instance Method Details

#<<(clip) ⇒ Object

}}}1



52
53
54
55
56
57
# File 'lib/clip_array.rb', line 52

def <<(clip)
#{{{1

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

#autosave(seconds) ⇒ Object

}}}1



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/clip_array.rb', line 291

def autosave(seconds)
#{{{1

  print_debug "autosave(#{seconds}) called"
  if @autosave_thread then
    print_debug "  killing old thread"
    @autosave_thread.kill
    @autosave_thread = nil
  end

  if seconds == 0 then
    print_debug "  returning because seconds==0"
    return
  end

  print_verbose "starting autosave thread"
  @autosave_thread = Thread.new do
    while true
      print_verbose "autosaving at #{Time.now}"
      save
      sleep(seconds)
    end
  end
end

#clearObject

Clear array and reset index



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

def clear
#{{{1

  super
  @current_index = -1
end

#clear1Object

Clear array and create one empty clip



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

def clear1
#{{{1

  clear
  self << Clip.new
end

#closeObject

}}}1



277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/clip_array.rb', line 277

def close
#{{{1

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

#currentObject

}}}1



171
172
173
174
# File 'lib/clip_array.rb', line 171

def current
#{{{1

  return self[@current_index]
end

#deleteObject

}}}1



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/clip_array.rb', line 144

def delete
#{{{1

  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

#newObject

}}}1



163
164
165
166
167
168
169
# File 'lib/clip_array.rb', line 163

def new
#{{{1

  c = Clip.new
  push(c)
  @current_index = length-1
  print_verbose "creating new clip #{@current_index+1}"
end

#nextObject

}}}1



126
127
128
129
130
131
132
133
# File 'lib/clip_array.rb', line 126

def next
#{{{1

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

#prevObject

}}}1



135
136
137
138
139
140
141
142
# File 'lib/clip_array.rb', line 135

def prev
#{{{1

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

#saveObject

}}}1



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/clip_array.rb', line 261

def save
#{{{1

  if @file.nil? then
    return
  end

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

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

#set_file(file, file_and_path) ⇒ Object

}}}1



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

def set_file(file,file_and_path)
#{{{1

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