Module: RoFile

Defined in:
lib/ro_file.rb

Class Method Summary collapse

Class Method Details

.action_out(action, file) ⇒ Object



39
40
41
# File 'lib/ro_file.rb', line 39

def action_out(action, file)
  Out.table "#{action}", "#{File.basename(file)}", "file:#{File.basename __FILE__} line:#{__LINE__}"
end

.basename_ext(file) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/ro_file.rb', line 55

def basename_ext(file)
  basename, ext = nil
  f = File.basename(file)

  f.match(%r{(\w+)(\.\w+)?$}) do |m|
    basename = File.basename $~[1]
    ext = $~[2]
  end
  return [basename, ext].compact
end

.change_recently?(file, internal = 10) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/ro_file.rb', line 14

def change_recently?(file, internal=10)

  Time.now - Time.at(test(?M, file)) < internal
end

.read(file) ⇒ Object



9
10
11
12
# File 'lib/ro_file.rb', line 9

def read(file)
  action_out("read", file)
  File.read(file)
end

.readlines(file) ⇒ Object



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

def readlines(file)
  action_out("readlines", file)
  File.readlines(file)
end

.remove_file(file) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/ro_file.rb', line 30

def remove_file(file)
  if test(?f, file)
    action_out('remove_file', file)
    FileUtils.rm_f(file)
  else
    raise "#{file} is not a file"
  end
end

.template(file, tt, caller, caller_file) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ro_file.rb', line 43

def template(file, tt, caller, caller_file)
  if RoCommands.lib
    template_path = File.join(RoCommands.lib, 'tt', "#{tt}.erb")
  else
    template_path = "#{tt}.erb"
  end

  ctn = File.read(template_path)
  result = ERB.new(ctn).result(caller.send(:binding))
  write(file, result)
end

.write(file, ctn) ⇒ Object



24
25
26
27
28
# File 'lib/ro_file.rb', line 24

def write(file, ctn)
  action_out("write", file)
  FileUtils.mkpath(File.dirname file)
  File.write(file, ctn)
end