Class: BatchFile

Inherits:
Object
  • Object
show all
Defined in:
lib/pik/batch_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name, mode = :new) {|_self| ... } ⇒ BatchFile

Returns a new instance of BatchFile.

Yields:

  • (_self)

Yield Parameters:

  • _self (BatchFile)

    the object that the method was called on



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pik/batch_file.rb', line 12

def initialize(file_name, mode=:new)
	@rubyw_exe = 'rubyw.exe'
	@ruby_exe  = 'ruby.exe'
	@file_name = file_name 
	case mode
	when :open
		@file_data = File.read(@file_name).split("\n")
		@fmode        = 'r+'
	when :new
		@file_data = [header]
		@fmode        = 'w+'
	end
	yield self if block_given?
end

Instance Attribute Details

#file_dataObject

Returns the value of attribute file_data.



10
11
12
# File 'lib/pik/batch_file.rb', line 10

def file_data
  @file_data
end

#file_nameObject

Returns the value of attribute file_name.



10
11
12
# File 'lib/pik/batch_file.rb', line 10

def file_name
  @file_name
end

#ruby_dirObject

Returns the value of attribute ruby_dir.



10
11
12
# File 'lib/pik/batch_file.rb', line 10

def ruby_dir
  @ruby_dir
end

Class Method Details

.open(file_name) {|bf| ... } ⇒ Object

Yields:

  • (bf)


4
5
6
7
8
# File 'lib/pik/batch_file.rb', line 4

def self.open(file_name)
	bf = new(file_name, :open)
	yield bf if block_given?
	bf
end

Instance Method Details

#bin_dirObject



27
28
29
# File 'lib/pik/batch_file.rb', line 27

def bin_dir
	WindowsFile.join(File.dirname(@ruby_exe))
end

#call(bat) ⇒ Object



44
45
46
47
# File 'lib/pik/batch_file.rb', line 44

def call(bat)
	@file_data << "CALL #{bat}\n"
	self
end

#echo(string) ⇒ Object



54
55
56
57
# File 'lib/pik/batch_file.rb', line 54

def echo(string)
	string = ' ' + string unless string == '.'
	@file_data << "ECHO#{string}"
end

#ftype(files = { 'rbfile' => @ruby_exe, 'rbwfile' => @rubyw_exe }) ⇒ Object



37
38
39
40
41
42
# File 'lib/pik/batch_file.rb', line 37

def ftype(files={ 'rbfile' => @ruby_exe, 'rbwfile' => @rubyw_exe })
	files.sort.each do |filetype, open_with|
		@file_data << 	"FTYPE #{filetype}=#{open_with} \"%1\" %*\n"
	end
	self
end

#headerObject



31
32
33
34
35
# File 'lib/pik/batch_file.rb', line 31

def header
	string =  "@ECHO OFF\n\n" 
	string << "::  This batch file generated by Pik, the\n"
	string << "::  Ruby Manager for Windows\n"	
end

#set(items) ⇒ Object



49
50
51
52
# File 'lib/pik/batch_file.rb', line 49

def set(items)
	items.each{|k,v| 	@file_data << "SET #{k}=#{v}" }
	self
end

#to_sObject



59
60
61
# File 'lib/pik/batch_file.rb', line 59

def to_s
	@file_data.join("\n")
end

#writeObject



63
64
65
# File 'lib/pik/batch_file.rb', line 63

def write
	File.open(@file_name, @fmode){|f| f.puts self.to_s }
end