Class: Ankit::AddCommand

Inherits:
TextReadingCommand show all
Includes:
CardNaming
Defined in:
lib/ankit/add_command.rb

Constant Summary

Constants inherited from Command

Command::COMMANDS

Instance Attribute Summary

Attributes inherited from Command

#args, #options, #runtime

Instance Method Summary collapse

Methods included from CardNaming

#card_wildcard_for, #to_card_name, #to_card_path

Methods inherited from TextReadingCommand

#each_text, #validate_options

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

#dest_dirObject



30
# File 'lib/ankit/add_command.rb', line 30

def dest_dir; options[:dir] || runtime.config.card_paths[0]; end

#executeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ankit/add_command.rb', line 14

def execute()
  validate_options
  each_text do |text|
    text.split(/\n\n+/).map(&:strip).each do |chunk|
      next if chunk.empty?
      card = Card.parse(chunk)
      next unless card
      # TODO: gaurd ovewrite
      # TODO: guard out-of-path write
      filename = to_card_path(dest_dir, card.name)
      File.open(filename, "w") { |f| f.write(chunk) }
      runtime.stdout.write("#{filename}\n")
    end
  end
end