Class: TechbookHelper::Helper
- Inherits:
-
Object
- Object
- TechbookHelper::Helper
- Defined in:
- lib/techbook_helper.rb
Constant Summary collapse
- IMAGES_DIR =
'_i'- INDEX_MD =
'index.md'- GIT_KEEP =
'.keep'
Instance Method Summary collapse
- #build_chapter(name) ⇒ Object
- #build_template(name) ⇒ Object
- #check_git_dir ⇒ Object
- #clear_current_dir ⇒ Object
- #create ⇒ Object
- #create_root_chapters ⇒ Object
- #handler_arguments(arg) ⇒ Object
- #is_dir_used? ⇒ Boolean
- #print_help ⇒ Object
- #print_promt ⇒ Object
- #print_unknown_command(command) ⇒ Object
- #puts_line ⇒ Object
- #puts_ok ⇒ Object
- #say_hello ⇒ Object
- #select_book_name ⇒ Object
- #too_many_arg_error ⇒ Object
- #yes_no_choice ⇒ Object
Instance Method Details
#build_chapter(name) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/techbook_helper.rb', line 132 def build_chapter(name) if @current_book_name.nil? puts "Do not know which book to work with".red return end Dir.mkdir(File.join(@current_book_name, name)) puts "Create chapter #{name}" sleep(0.4) File.open(File.join(@current_book_name, name, INDEX_MD), 'w').close puts 'Index page' sleep(0.5) end |
#build_template(name) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/techbook_helper.rb', line 147 def build_template(name) Dir.mkdir(name) puts 'Create root directory' sleep(0.4) File.open(File.join(name, INDEX_MD), 'w').close puts 'Index page' sleep(0.4) Dir.mkdir(File.join(name, IMAGES_DIR)) sleep(0.4) puts 'Create images directory' puts_line puts 'Book template complete. You can edit it, commit and push to the server... manually for now ;)' puts_line sleep(0.5) @current_book_name = name end |
#check_git_dir ⇒ Object
191 192 193 |
# File 'lib/techbook_helper.rb', line 191 def check_git_dir Dir.exist?('.git') end |
#clear_current_dir ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/techbook_helper.rb', line 89 def clear_current_dir puts 'Clear files in current directory'.red Dir['*'].each do |file| if File.directory? file FileUtils.rm_rf file else File.delete file end end 10.times{print '. '; sleep 0.3;} puts ' ' puts 'Done' sleep(0.4) puts_line end |
#create ⇒ Object
57 58 59 60 61 62 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 |
# File 'lib/techbook_helper.rb', line 57 def create if is_dir_used? puts "We find other files in this directory:".red puts "'#{Dir['*'].join("', '")}'".blue puts "One book – one folder." puts "Are you want #{'delete all'.red} and create new empty book?" puts "(yes/no)?" unless yes_no_choice puts_ok return end clear_current_dir end unless check_git_dir puts 'Looks like git not initialized in current directory'.yellow end current_dir = Dir.pwd puts 'Ok, lest do it.' puts "Confirm you wana create TechBook template in directory #{current_dir.green}" print '(yes/no) ' if yes_no_choice select_book_name else puts_ok end end |
#create_root_chapters ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/techbook_helper.rb', line 165 def create_root_chapters puts 'Top level chapters creation' puts_line puts 'For exit type "exit"' puts 'Enter chapter name (ex: "1 Specification", "2 Money and low")' print_promt while user_input = STDIN.gets.chomp case when user_input.nil? || user_input.size == 0 puts "Chapter with empty name? Are you kidding me?" puts "Please, give me something more intelligent" print_promt when @reject_args.include?(user_input) puts_ok break else puts "Create chapter \"#{user_input}\"" build_chapter user_input puts 'For exit type "exit"' puts 'Enter chapter name' print_promt end end end |
#handler_arguments(arg) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/techbook_helper.rb', line 43 def handler_arguments(arg) case when @help_args.include?(arg) puts "Welcome to TechBook helper!".colorize(:color => :white, :background => :black) puts 'type cd path_to_your_book_repository and lets go.' puts_line print_help when @create_args.include?(arg) create else print_unknown_command(arg) end end |
#is_dir_used? ⇒ Boolean
195 196 197 198 199 |
# File 'lib/techbook_helper.rb', line 195 def is_dir_used? files_array = Dir['*'] return true unless files_array.empty? return false end |
#print_help ⇒ Object
201 202 203 204 205 206 207 208 |
# File 'lib/techbook_helper.rb', line 201 def print_help puts 'Command you can use:' puts '%-10s – %10s' % ['create'.green, 'Will create new book template in current directory'] puts '%-10s – %10s' % [' ', 'Aliases c, -c, --c, -create, --create'] puts '%-10s – %10s' % ['check'.green, 'Go through dirs in current folder and check known problems'] puts '%-10s – %10s' % [' ', 'Aliases ch, -ch, --ch, -check, --check'] puts_line end |
#print_promt ⇒ Object
241 242 243 |
# File 'lib/techbook_helper.rb', line 241 def print_promt print ':> '.green end |
#print_unknown_command(command) ⇒ Object
210 211 212 213 214 |
# File 'lib/techbook_helper.rb', line 210 def print_unknown_command(command) puts "Unknown command: #{command}" puts_line print_help end |
#puts_line ⇒ Object
245 246 247 |
# File 'lib/techbook_helper.rb', line 245 def puts_line puts '--------------------------' end |
#puts_ok ⇒ Object
249 250 251 |
# File 'lib/techbook_helper.rb', line 249 def puts_ok puts 'I respect your choice' end |
#say_hello ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/techbook_helper.rb', line 20 def say_hello @help_args = ['-h', 'h', '--h', '-help', 'help', '--help'] @create_args = ['create', '-create', '--create', 'c', '-c', '--c'] @check_args = ['check', '-check', '--check', 'ch', '-ch', '--ch'] @confirm_args = ['y', 'yes', 'ok', 'sure'] @reject_args = ['n', 'no', 'o no', 'do not', 'exit', 'close'] @current_book_name = nil print_help if ARGV.size == 0 if ARGV.size > 1 too_many_arg_error return end ARGV.each do |a| handler_arguments a end end |
#select_book_name ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/techbook_helper.rb', line 107 def select_book_name puts "One more question. What the name of your book?" print_promt while user_input = STDIN.gets.chomp case when user_input.nil? || user_input.size == 0 puts "I am not sure this is good idea to create book with empty name" puts "Please, try again" print_promt else puts "Build template for book name \"#{user_input}\"" build_template user_input puts 'Are you want to create top level chapters? (y/n)' if yes_no_choice create_root_chapters else puts_ok end break end end end |
#too_many_arg_error ⇒ Object
216 217 218 219 220 |
# File 'lib/techbook_helper.rb', line 216 def too_many_arg_error puts 'Too many arguments.'.red.on_black puts_line print_help end |
#yes_no_choice ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/techbook_helper.rb', line 222 def yes_no_choice print_promt answer = false while user_input = STDIN.gets.chomp case when @confirm_args.include?(user_input) answer = true break when @reject_args.include?(user_input) answer = false break else puts "Please, answer the question – yes or no" print_promt end end answer end |