Class: Cheatly::CLI
Instance Method Summary collapse
- #create(handle) ⇒ Object
- #edit(handle) ⇒ Object
- #editor ⇒ Object
-
#initialize(args) ⇒ CLI
constructor
A new instance of CLI.
- #list ⇒ Object
- #model ⇒ Object
- #process ⇒ Object
- #sheet(handle) ⇒ Object
- #write_to_tempfile(title, body = nil) ⇒ Object
Constructor Details
#initialize(args) ⇒ CLI
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cheatly.rb', line 16 def initialize(args) @command = args.shift || "help" @handle = args.first if "help" == @command @handle = @command @command = "show" end = {} op_parser = OptionParser.new do |opts| opts.on("-l", "--local", "Run using local file system") do |v| [:local] = v end end op_parser.parse! args end |
Instance Method Details
#create(handle) ⇒ Object
62 63 64 65 66 |
# File 'lib/cheatly.rb', line 62 def create(handle) sheet = Sheet.with_file_adapter.new(handle) sheet.body = write_to_tempfile(handle) sheet.save end |
#edit(handle) ⇒ Object
68 69 70 71 72 |
# File 'lib/cheatly.rb', line 68 def edit(handle) sheet = Sheet.with_file_adapter.find(handle) sheet.body = write_to_tempfile(handle, sheet.body) sheet.save end |
#editor ⇒ Object
85 86 87 |
# File 'lib/cheatly.rb', line 85 def editor ENV['VISUAL'] || ENV['EDITOR'] || "nano" end |
#list ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/cheatly.rb', line 53 def list sheets = model.all page puts "List of available cheat-sheets:" sheets.each do |sheet| puts " #{sheet.title}" end end |
#model ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/cheatly.rb', line 34 def model if [:local] Sheet.with_file_adapter else Sheet end end |
#process ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/cheatly.rb', line 89 def process case @command when "show" sheet(@handle) when "list" list when "new" create(@handle) when "edit" edit(@handle) else puts "Command [#{@command}] not found :-( . You can try running 'cheatly list' to check the available commands. " end end |
#sheet(handle) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cheatly.rb', line 42 def sheet(handle) sheet = model.find(handle) unless sheet puts "Sheet not found, run 'cheatly list' to see the availables." return end page puts "#{sheet.title}:" puts sheet.to_s end |
#write_to_tempfile(title, body = nil) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/cheatly.rb', line 74 def write_to_tempfile(title, body = nil) tempfile = Tempfile.new(title + '.yml') tempfile.write(body) if body tempfile.close system "#{editor} #{tempfile.path}" tempfile.open body = tempfile.read tempfile.close body end |