ankit 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,11 @@ require 'ankit/card'
3
3
  require 'ankit/text_reading_command'
4
4
 
5
5
  module Ankit
6
+ CARD_TEMPLATE = <<EOF
7
+ O:
8
+ T:
9
+ EOF
10
+
6
11
  class AddCommand < TextReadingCommand
7
12
  include CardNaming
8
13
  available
@@ -13,7 +18,7 @@ module Ankit
13
18
 
14
19
  def execute()
15
20
  validate_options
16
- each_text do |text|
21
+ each_text(CARD_TEMPLATE.strip) do |text|
17
22
  text.split(/\n\n+/).map(&:strip).each do |chunk|
18
23
  next if chunk.empty?
19
24
  card = Card.parse(chunk)
@@ -27,6 +32,6 @@ module Ankit
27
32
  end
28
33
  end
29
34
 
30
- def dest_dir; options[:dir] || runtime.config.card_paths[0]; end
35
+ def dest_dir; options[:dir] || runtime.config.primary_card_path; end
31
36
  end
32
37
  end
@@ -21,11 +21,12 @@ module Ankit
21
21
  class Config
22
22
  DEFAULT_PATH = File.expand_path("~/.ankit")
23
23
 
24
- attr_writer :repo, :location, :card_paths, :challenge_limit
24
+ attr_writer :repo, :location, :card_paths, :primary_card_path, :challenge_limit
25
25
 
26
26
  def repo; @repo ||= File.expand_path("~/.ankit.d"); end
27
27
  def location; @location ||= `hostname`.strip; end
28
28
  def card_paths; @card_paths ||= [File.join(repo, "cards")]; end
29
+ def primary_card_path; @primary_card_path ||= card_paths[0]; end
29
30
  def challenge_limit; @challenge_limit ||= 50; end
30
31
 
31
32
  # Computed parameters
@@ -48,6 +49,9 @@ module Ankit
48
49
  paths.sort
49
50
  end
50
51
 
52
+ def editor_backup
53
+ File.join(self.repo, "last_edited.txt")
54
+ end
51
55
 
52
56
  def self.open(path)
53
57
  config = self.new
@@ -1,15 +1,33 @@
1
1
 
2
2
  require 'ankit/command'
3
+ require 'tempfile'
4
+ require 'fileutils'
3
5
 
4
6
  module Ankit
5
7
  class TextReadingCommand < Command
6
8
  define_options do |spec, options|
7
9
  spec.on("-i", "--stdin") { options[:stdin] = true }
10
+ spec.on("-e", "--editor EDITOR") { |editor| options[:editor] = editor }
8
11
  end
9
12
 
10
- def each_text(&block)
13
+ def ask_edit(template)
14
+ f = Tempfile.new('toadd')
15
+ begin
16
+ f.write(template)
17
+ f.flush
18
+ system(options[:editor] + " " + f.path)
19
+ FileUtils.copy(f.path, self.runtime.config.editor_backup)
20
+ open(self.runtime.config.editor_backup) { |f| f.read }
21
+ ensure
22
+ f.close
23
+ end
24
+ end
25
+
26
+ def each_text(template="", &block)
11
27
  if options[:stdin]
12
28
  block.call(runtime.stdin.read)
29
+ elsif options[:editor]
30
+ block.call(ask_edit(template))
13
31
  else
14
32
  args.each { |name| open(name) { |f| block.call(f.read) } }
15
33
  end
@@ -17,7 +35,8 @@ module Ankit
17
35
 
18
36
  def validate_options
19
37
  raise BadOptions, "--stdin cannot have any fileame" if options[:stdin] and not args.empty?
20
- raise BadOptions, "need a fileame" if not options[:stdin] and args.empty?
38
+ raise BadOptions, "--editor cannot have any fileame" if options[:editor] and not args.empty?
39
+ raise BadOptions, "need a fileame" if not (options[:stdin] or options[:editor]) and args.empty?
21
40
  end
22
41
  end
23
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ankit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-15 00:00:00.000000000 Z
12
+ date: 2012-05-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: highline