Class: Ritsu::Utility::FileRobot

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ritsu/utility/file_robot.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileRobot

Returns a new instance of FileRobot.



18
19
20
21
# File 'lib/ritsu/utility/file_robot.rb', line 18

def initialize
  @io = SimpleIO.new
  @force = false
end

Instance Attribute Details

#forceObject

Returns the value of attribute force.



15
16
17
# File 'lib/ritsu/utility/file_robot.rb', line 15

def force
  @force
end

#ioObject (readonly)

Returns the value of attribute io.



16
17
18
# File 'lib/ritsu/utility/file_robot.rb', line 16

def io
  @io
end

Class Method Details

.method_missing(symbol, *args, &block) ⇒ Object



11
12
13
# File 'lib/ritsu/utility/file_robot.rb', line 11

def self.method_missing(symbol, *args, &block)
  instance.send(symbol, *args, &block)
end

.vObject



9
# File 'lib/ritsu/utility/file_robot.rb', line 9

def self.v; instance end

Instance Method Details

#create_dir(dir_name, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ritsu/utility/file_robot.rb', line 51

def create_dir(dir_name, options={})
  options = {:echo_exists=>true}.merge(options)
  if File.exists?(dir_name)
    if options[:echo_exists]
      io.log('exist', dir_name)
    end
  else
    FileUtils.mkdir_p(dir_name)
    io.log('create', dir_name)
  end
end

#create_file(filename, content = "") ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ritsu/utility/file_robot.rb', line 63

def create_file(filename, content="")
  do_create_file = Proc.new do
    File.open(filename, "w") { |f| f.write content }
  end

  do_overwrite = Proc.new do
    do_create_file.call
    io.log('overwrite', filename)
  end

  create_dir(File.dirname(filename), :echo_exists=>false)
  if File.exists?(filename)
    if self.force
      do_overwrite.call
    else
      answer = io.ask_yes_no_all("overwrite #{filename}?")
      case answer
      when :yes
        do_overwrite.call
      when :no
        io.log('exist', filename)
      when :all
        self.force = true
        do_overwrite.call
      end
    end
  else
    do_create_file.call
    io.log('create', filename)
  end
end

#forcefully(&block) ⇒ Object



44
45
46
47
48
49
# File 'lib/ritsu/utility/file_robot.rb', line 44

def forcefully(&block)
  old_force = self.force
  self.force = true
  block.call
  self.force = old_force
end

#inputObject



25
# File 'lib/ritsu/utility/file_robot.rb', line 25

def input; io.input end

#input=(value) ⇒ Object



26
# File 'lib/ritsu/utility/file_robot.rb', line 26

def input=(value); io.input = value end

#outputObject



23
# File 'lib/ritsu/utility/file_robot.rb', line 23

def output; io.output end

#output=(value) ⇒ Object



24
# File 'lib/ritsu/utility/file_robot.rb', line 24

def output=(value); io.output = value end

#quietObject



27
# File 'lib/ritsu/utility/file_robot.rb', line 27

def quiet; io.quiet end

#quiet=(value) ⇒ Object



28
# File 'lib/ritsu/utility/file_robot.rb', line 28

def quiet=(value); io.quiet = value end

#quietly(&block) ⇒ Object



30
31
32
33
34
35
# File 'lib/ritsu/utility/file_robot.rb', line 30

def quietly(&block)
  old_quiet = self.quiet
  self.quiet = true
  block.call
  self.quiet = old_quiet
end

#remove_dir(dirname) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ritsu/utility/file_robot.rb', line 95

def remove_dir(dirname)      
  if !File.exist?(dirname)
    io.log("not exist", dirname)
  elsif !File.directory?(dirname)
    io.log("not dir", dirname)
  else
    begin
      Dir.glob(dirname + "/*") { throw "not empty" }
      is_empty = true
    rescue
      is_empty = false
    end
  
    if !is_empty
      io.log('not empty', dirname)
    elsif
      FileUtils.remove_dir(dirname)
      io.log("remove", dirname)
    end
  end
end

#remove_file(filename) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/ritsu/utility/file_robot.rb', line 117

def remove_file(filename)
  if !File.exists?(filename)
    io.log("not exist", filename)
  elsif !File.file?(filename)
    io.log("not file", filename)
  else
    FileUtils.remove_file(filename)
    io.log("remove", filename)
  end
end

#verbosely(&block) ⇒ Object



37
38
39
40
41
42
# File 'lib/ritsu/utility/file_robot.rb', line 37

def verbosely(&block)
  old_quiet = self.quiet
  self.quiet = false
  block.call
  self.quiet = old_quiet
end