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

#ask_edit, #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



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

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

#executeObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ankit/add_command.rb', line 19

def execute()
  validate_options
  each_text(CARD_TEMPLATE.strip) 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