Class: Ankit::TextReadingCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/ankit/text_reading_command.rb

Direct Known Subclasses

AddCommand, NameCommand

Constant Summary

Constants inherited from Command

Command::COMMANDS

Instance Attribute Summary

Attributes inherited from Command

#args, #options, #runtime

Instance Method Summary collapse

Methods inherited from Command

available, by_name, command_name, define_options, #initialize, option_spec

Constructor Details

This class inherits a constructor from Ankit::Command

Instance Method Details

#ask_edit(template) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ankit/text_reading_command.rb', line 13

def ask_edit(template)
  f = Tempfile.new('toadd')
  begin
    f.write(template)
    f.flush
    system(options[:editor] + " " + f.path)
    FileUtils.copy(f.path, self.runtime.config.editor_backup)
    open(self.runtime.config.editor_backup) { |f| f.read }
  ensure
    f.close
  end
end

#each_text(template = "", &block) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/ankit/text_reading_command.rb', line 26

def each_text(template="", &block)
  if options[:stdin]
    block.call(runtime.stdin.read)
  elsif options[:editor]
    block.call(ask_edit(template))
  else
    args.each { |name| open(name) { |f| block.call(f.read) } }
  end
end

#validate_optionsObject

Raises:



36
37
38
39
40
# File 'lib/ankit/text_reading_command.rb', line 36

def validate_options
  raise BadOptions, "--stdin cannot have any fileame" if options[:stdin] and not args.empty?
  raise BadOptions, "--editor cannot have any fileame" if options[:editor] and not args.empty?
  raise BadOptions, "need a fileame" if not (options[:stdin] or options[:editor]) and args.empty?
end