Class: Ghaki::Namer::Files::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/ghaki/namer/files/base.rb

Constant Summary

Constants inherited from Base

Base::RAND_STAMP_FMT, Base::RAND_STAMP_MAX, Base::TIME_STAMP_FMT

Instance Attribute Summary collapse

Attributes inherited from Base

#rand_stamp, #rand_stamp_max, #ticket_generator, #time_stamp

Instance Method Summary collapse

Methods inherited from Base

#parse_names, #ticket

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
# File 'lib/ghaki/namer/files/base.rb', line 15

def initialize opts={} ; super opts
  @suffix_map = {}
  @filename_map  = {}
  @path_generator = opts[:path_generator] || []
  @base_generator = opts[:base_generator] || []
end

Instance Attribute Details

#base_generatorObject

Returns the value of attribute base_generator.



11
12
13
# File 'lib/ghaki/namer/files/base.rb', line 11

def base_generator
  @base_generator
end

#filename_mapObject

Returns the value of attribute filename_map.



11
12
13
# File 'lib/ghaki/namer/files/base.rb', line 11

def filename_map
  @filename_map
end

#path_generatorObject

Returns the value of attribute path_generator.



11
12
13
# File 'lib/ghaki/namer/files/base.rb', line 11

def path_generator
  @path_generator
end

#suffix_mapObject

Returns the value of attribute suffix_map.



11
12
13
# File 'lib/ghaki/namer/files/base.rb', line 11

def suffix_map
  @suffix_map
end

Instance Method Details

#assign_file(token, suffix) ⇒ Object

ASSIGNING FILE NAMES



125
126
127
128
# File 'lib/ghaki/namer/files/base.rb', line 125

def assign_file token, suffix
  reserve_file token, suffix
  freeze_file token
end

#assign_files(list) ⇒ Object



131
132
133
134
# File 'lib/ghaki/namer/files/base.rb', line 131

def assign_files list
  list.each do |key,val| assign_file key, val end
  self
end

#create_working_dirs(opts = {}) ⇒ Object



41
42
43
44
45
46
# File 'lib/ghaki/namer/files/base.rb', line 41

def create_working_dirs opts={}
  @filename_map.values.each do |fname|
    ::FileUtils.mkdir_p( File.dirname(fname), opts )
  end
  self
end

#debug_dump(out_file) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/ghaki/namer/files/base.rb', line 23

def debug_dump out_file; super
  out_file.puts( {
    :suffix_map => self.suffix_map,
    :path_generator => self.path_generator,
    :base_generator => self.base_generator,
    :filename_map => self.filename_map,
  }.inspect )
end

#file_named?(token) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/ghaki/namer/files/base.rb', line 137

def file_named? token
  @filename_map[token].nil?
end

#freeze_file(token) ⇒ Object Also known as: get_file




108
109
110
# File 'lib/ghaki/namer/files/base.rb', line 108

def freeze_file token
  @filename_map[token] || put_file( token, generate_filename( token ) )
end

#freeze_files(tokens) ⇒ Object




113
114
115
116
# File 'lib/ghaki/namer/files/base.rb', line 113

def freeze_files tokens
  tokens.each do |token| freeze_file token end
  self
end

#generate_filename(key) ⇒ Object



33
34
35
36
37
38
# File 'lib/ghaki/namer/files/base.rb', line 33

def generate_filename key
  cur_path = parse_names( *@path_generator )
  cur_base = parse_names( *@base_generator ).
    push( @suffix_map[key] ).join('.')
  File.join( cur_path, cur_base )
end

#opt_put_file(opts, token) ⇒ Object




73
74
75
# File 'lib/ghaki/namer/files/base.rb', line 73

def opt_put_file opts, token
  put_file token, opts[token] if opts.has_key?(token)
end

#opt_put_files(opts, *tokens) ⇒ Object




78
79
80
# File 'lib/ghaki/namer/files/base.rb', line 78

def opt_put_files opts, *tokens
  tokens.each do |token| opt_put_file token, opts end
end

#put_file(token, new_fname) ⇒ Object




87
88
89
90
91
92
93
94
95
# File 'lib/ghaki/namer/files/base.rb', line 87

def put_file token, new_fname
  old_fname = @filename_map[token]
  if old_fname.nil?
    @filename_map[token] = new_fname
  elsif old_fname != new_fname
    raise NamerError, "Inconsistent Filenaming: #{token.to_s}"
  end
  new_fname
end

#put_files(list) ⇒ Object




98
99
100
101
# File 'lib/ghaki/namer/files/base.rb', line 98

def put_files list
  list.each do |key,val| put_file key, val end
  self
end

#reserve_file(token, new_suffix) ⇒ Object




53
54
55
56
57
58
59
60
# File 'lib/ghaki/namer/files/base.rb', line 53

def reserve_file token, new_suffix
  old_suffix = @suffix_map[token]
  if old_suffix.nil?
    @suffix_map[token] = new_suffix
  elsif old_suffix != new_suffix
    raise NamerError, "Inconsistent File Extension: #{token.to_s} (#{old_suffix}/#{new_suffix})"
  end
end

#reserve_files(list) ⇒ Object




63
64
65
66
# File 'lib/ghaki/namer/files/base.rb', line 63

def reserve_files list
  list.each do |key,val| reserve_file key, val end
  self
end