Class: Fairy::VFile

Inherits:
Object
  • Object
show all
Defined in:
lib/fairy/share/vfile.rb

Constant Summary collapse

VFILE_EXT =
".vf"
VFILE_HEADER =
"#!fairy vfile"
VFILE_MAGIC =
/^#{Regexp.escape(VFILE_HEADER)}/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVFile

Returns a new instance of VFile.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fairy/share/vfile.rb', line 46

def initialize
  @vfile_name = nil

  @real_file_names = []
  @real_file_names_mutex = Mutex.new
  @real_file_names_cv = ConditionVariable.new

  @base_name = nil

  @BASE_NAME_CONVERTER = nil
  if CONF.VF_BASE_NAME_CONVERTER
	@BASE_NAME_CONVERTER = eval(CONF.VF_BASE_NAME_CONVERTER)
  end
end

Instance Attribute Details

#base_nameObject (readonly)

Returns the value of attribute base_name.



62
63
64
# File 'lib/fairy/share/vfile.rb', line 62

def base_name
  @base_name
end

#vfile_nameObject

Returns the value of attribute vfile_name.



61
62
63
# File 'lib/fairy/share/vfile.rb', line 61

def vfile_name
  @vfile_name
end

Class Method Details

.real_files(real_files) ⇒ Object



40
41
42
43
44
# File 'lib/fairy/share/vfile.rb', line 40

def VFile.real_files(real_files)
  vfile = new
  vfile.real_file_names = real_files
  vfile
end

.vfile(path) ⇒ Object



34
35
36
37
38
# File 'lib/fairy/share/vfile.rb', line 34

def VFile.vfile(path)
  vfile = new
  vfile.vfile(path)
  vfile
end

.vfile?(path) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fairy/share/vfile.rb', line 16

def VFile.vfile?(path)
  if File.extname(path) == VFILE_EXT
	return true
  end
  if !File.exist?(path)
	return false
  end
	
  File.open(path) do |io|
	l = io.gets
	begin
	  return VFILE_MAGIC =~ l
	rescue ArgumentError
	  return false
	end
  end
end

Instance Method Details

#create_vfileObject



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fairy/share/vfile.rb', line 120

def create_vfile
  File.open(@vfile_name, "w") do |io|
	io.puts VFILE_HEADER
	io.puts
	@real_file_names_mutex.synchronize do
	  @real_file_names.each do |fn|
 io.puts fn
	  end
	end
  end
end

#each_real_file_name(&block) ⇒ Object Also known as: each



115
116
117
# File 'lib/fairy/share/vfile.rb', line 115

def each_real_file_name(&block)
  real_file_names.dup.each &block
end

#marshal_dumpObject

Ruby 1.9 mershal 対応

- Ruby 1.9 では mutex を dump できない


171
172
173
# File 'lib/fairy/share/vfile.rb', line 171

def marshal_dump
  [@vfile_name, @real_file_names]
end

#marshal_load(ary) ⇒ Object



175
176
177
178
179
180
# File 'lib/fairy/share/vfile.rb', line 175

def marshal_load(ary)
  @vfile_name = ary[0]
  @real_file_names = ary[1]
  @real_file_names_mutex = Mutex.new
  @real_file_names_cv = ConditionVariable.new
end

#real_file_namesObject



81
82
83
84
85
# File 'lib/fairy/share/vfile.rb', line 81

def real_file_names
  @real_file_names_mutex.synchronize do
	@real_file_names
  end
end

#real_file_names=(val) ⇒ Object



88
89
90
91
92
# File 'lib/fairy/share/vfile.rb', line 88

def real_file_names=(val)
  @real_file_names_mutex.synchronize do
	@real_file_names=val
  end
end

#set_real_file(no, url) ⇒ Object

base_regexp = /^#Regexp.escape(base)/

fn = nil
@real_file_names_mutex.synchronize do

ary = @real_file_names.select{|e| base_regexp =~ e}.sort if ary.empty? fn = “#base-000” else fn = ary.last.succ end @real_file_names.push fn

  end
  fn
end


163
164
165
166
167
# File 'lib/fairy/share/vfile.rb', line 163

def set_real_file(no, url)
  @real_file_names_mutex.synchronize do
	@real_file_names[no] = url
  end
end

#vfile(path) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fairy/share/vfile.rb', line 94

def vfile(path)
  File.open(path) do |io|
	l = io.gets
	unless VFILE_MAGIC =~ l
	  ERR::Raise NoVFile, path
	end

	files = []
	for l in io
	  l.chomp!
	  next if l =~ /^\s*$/
	  next if l =~ /^\s*#.*$/
	  files.push l
	end
	
	@real_file_names_mutex.synchronize do
	  @real_file_names = files
	end
  end
end