anki 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6c21472ccaf55739e044a25afb9ffc66e2752a41
4
- data.tar.gz: b21056c3a15d6e8e32408d11f2073e474a21323b
3
+ metadata.gz: d03bcf99764d976e6bc02dbdbf1da11545251922
4
+ data.tar.gz: 236e8230c7c684cbc3725df691c9bfcfac28e686
5
5
  SHA512:
6
- metadata.gz: d6661f4ae89070e88e122b2dd9d6db74649c630d617cb16dec93eb2c062ea65b5f9d7b5b28affb4e820e5db714428b991d6071414770efdb749a1e1b858a2890
7
- data.tar.gz: d1cd3de9ecd1f9e7896f8b575ae9fa49bcafc4e7b2f323d64df65e188ba90e8c27e9fe6357330536e68d5fefb0adaed4451691455ec694c2886317c54ab8f1db
6
+ metadata.gz: 3bbc95a72f555828ef77a8ab885fe20816a20148683fc408f918e4d04cc45fd784c357f5426c8410c8c02dc47bc36cc6e09da424d2d1ba1adcc80d35a15a76a8
7
+ data.tar.gz: 73b38e18b8b3f2633ef134365e42e9a3c40d2e00717aa62fc4e6106d1a06c05e7b458ca43dc54d59cb9161ee8d1ef6b147c77bc78abe0bdcd22da2d1ee8d2eb8
@@ -1,6 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.0
3
+ - 2.2
4
+ - 2.1
4
5
  - 2.0.0
5
6
  - 1.9.3
6
7
  - 1.9.2
data/README.md CHANGED
@@ -20,24 +20,31 @@ Or install it yourself as:
20
20
 
21
21
  ### Generating a string for cards
22
22
 
23
- After requiring the anki gem, simply create a new instance of `Anki::Deck`, pass in an array of hashes (either including the `card_data` option when creating the `Anki::Deck` object, or using the `Anki::Deck#card_data` method), and generate the deck by using the `Anki::Deck#generate_deck` method. It will return a string that is formatted to be ready to be saved into a file for importing into Anki.
23
+ After requiring the anki gem, create a new instance of `Anki::Deck`, pass in the headers (as an array of strings) and the card data (as an array of hashes), and generate the deck by using the `Anki::Deck#generate_deck` method. The generated deck is a string in the Anki separator format ready to be saved into a file for importing into Anki.
24
+
25
+ The Anki::Deck can be created either by including the `card_headers` and `card_data` option when creating the `Anki::Deck` object, or by using the `Anki::Deck#card_heaers` and `Anki::Deck#card_data` methods.
26
+
27
+ The card data can contain any keys and values. When the Anki::Deck is generated only the keys that match the card headers will be used as output, this may result in a completely empty card so make sure you check your headers and card data appropriately.
28
+
24
29
 
25
30
  ```ruby
26
31
  require 'anki'
27
32
 
33
+ headers = [ "front", "back" ]
28
34
  cards = [
29
- { "Front of the card" => "Back of the card" },
30
- { "Another card" => "Another answer" }
35
+ { "front" => "Front of the card", "back" => "Back of the card" },
36
+ { "front" => "Another card", "back" => "Another answer", "unused header" => "This will be ignored and not be in the deck" }
31
37
  ]
32
38
 
33
- deck = Anki::Deck.new(card_data: cards)
39
+ deck = Anki::Deck.new(card_headers: headers, card_data: cards)
34
40
 
35
41
  # Alternatively, you can pass in the array of hashes for card data
36
42
  # after initializing the object:
43
+ deck.card_headers = headers
37
44
  deck.card_data = cards
38
45
 
39
46
  deck.generate_deck
40
- # => "Front of the card;Back of the card\nAnother card;Another answer"
47
+ # => "#front;back\nFront of the card;Back of the card\nAnother card;Another answer"
41
48
  ```
42
49
 
43
50
  ### Generating a file
@@ -45,33 +52,25 @@ deck.generate_deck
45
52
  Alternatively, you can pass an optional `file` option when generating the deck to save the string into a file directly.
46
53
 
47
54
  ```ruby
48
- require 'anki'
49
-
50
- cards = [
51
- { "Front of the card" => "Back of the card" },
52
- { "Another card" => "Another answer" }
53
- ]
54
-
55
- deck = Anki::Deck.new(card_data: cards)
56
55
  # If you want to save it into a file directly, you can pass an optional `file` option
57
56
  # with the path where you want to save the file:
58
57
  deck.generate_deck(file: "/tmp/anki_deck.txt")
59
58
  ```
60
59
 
61
60
  ### Including card tags
62
-
63
- If you want to include tags with the generated Anki deck, the value of the card data can be a hash that includes an array of strings.
61
+ Tags are nothing special. Any field can be used as the Tag. When you are importing to Anki set the Field mapping to "Map to Tags" by clicking on the Change button for the field that contains your tags.
64
62
 
65
63
  ```ruby
66
64
  require 'anki'
67
65
 
68
- card = [
69
- { "Front" => { "value" => "Back", "tags" => ["tag1, tag2"]} }
70
- ]
71
-
72
- deck = Anki::Deck.new(card_data: card)
66
+ headers = [ "front", "back", "Tags" ]
67
+ cards = [
68
+ { "front" => "Front of the card", "back" => "Back of the card", "Tags" => "one_tag" },
69
+ { "front" => "Another card", "back" => "Another answer", "Tags" => "multiple tags are separated by spaces" }
70
+ ]
71
+ deck = Anki::Deck.new(card_headers: headers, card_data: cards)
73
72
  deck.generate_deck
74
- # => "Front;Back;tag1 tag2"
73
+ # => "#front;back;Tags\nFront of the card;Back of the card;one_tag\nAnother card;Another answer;multiple tags are separated by spaces"
75
74
  ```
76
75
 
77
76
  ## Contributing
@@ -19,6 +19,6 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_development_dependency "rake"
22
- spec.add_development_dependency "rspec", "~> 2.14"
22
+ spec.add_development_dependency "rspec", "~> 3.1"
23
23
  spec.add_development_dependency "fakefs", "~> 0.5"
24
24
  end
@@ -1,34 +1,38 @@
1
1
  # coding: utf-8
2
2
  module Anki
3
3
  class Deck
4
- attr_accessor :card_data
4
+ attr_accessor :card_headers,:card_data
5
5
 
6
6
  def initialize(options = {})
7
+ @card_headers = options.delete(:card_headers)
7
8
  @card_data = options.delete(:card_data)
8
9
  end
9
10
 
10
11
  def generate_deck(options = {})
11
- raise ArgumentError, "card_data should be an array of hashes" if !self.card_data.is_a?(Array)
12
- raise ArgumentError, "You need card data." if self.card_data.empty?
12
+ raise ArgumentError, "card_headers must be an array" if !self.card_headers.is_a?(Array)
13
+ raise ArgumentError, "card_headers must not be empty" if self.card_headers.empty?
14
+ raise ArgumentError, "card_data must be an array" if !self.card_data.is_a?(Array)
15
+ raise ArgumentError, "card_data must not be empty" if self.card_data.empty?
13
16
 
14
- anki_string = self.card_data.map { |card| card_data_to_string(card) }.compact.join("\n")
17
+ anki_string = ""
18
+ anki_string << card_header_to_string()
19
+ anki_string << self.card_data.map { |card| card_data_to_string(card) }.compact.join("\n")
15
20
  create_file(anki_string, options[:file]) if options[:file]
16
21
  anki_string
17
22
  end
18
23
 
19
24
  private
20
25
 
26
+ def card_header_to_string()
27
+ "#" + self.card_headers.join(";") + "\n"
28
+ end
29
+
21
30
  def card_data_to_string(card)
22
- front_card = card.keys.first
23
- back_card = card.values.first
24
-
25
- if back_card.is_a?(String)
26
- "#{front_card};#{back_card}"
27
- elsif back_card.is_a?(Hash)
28
- back_card_value = back_card["value"]
29
- tags = back_card["tags"].join(" ")
30
- "#{front_card};#{back_card_value};#{tags}"
31
- end
31
+ raise ArgumentError, "card must be a hash" if !card.is_a?(Hash)
32
+
33
+ card.default = ""
34
+
35
+ self.card_headers.map{ |header| card[header] }.join(";")
32
36
  end
33
37
 
34
38
  def create_file(str, file)
@@ -1,3 +1,3 @@
1
1
  module Anki
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,43 +1,71 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Anki::Deck do
3
+ RSpec.describe Anki::Deck do
4
4
  describe "#generate_deck" do
5
5
  subject { Anki::Deck.new }
6
- let(:cards) { [{ "a" => "b" }, { "c" => "d" }] }
6
+ let(:headers) { ["front", "back"] }
7
+ let(:cards) {
8
+ [
9
+ { "front" => "a", "back" => "b" },
10
+ { "front" => "c", "back" => "d" }
11
+ ]
12
+ }
7
13
 
8
- it "raises an ArgumentError if card_data is not an array" do
14
+ it "raises an ArgumentError if card_header is not an array" do
15
+ subject.card_headers = "I'm card_header!"
16
+
17
+ expect {
18
+ subject.generate_deck
19
+ }.to raise_error(ArgumentError, "card_headers must be an array")
20
+ end
21
+
22
+ it "raises an ArgumentError if card_header is an empty array" do
23
+ subject.card_headers = []
24
+
25
+ expect {
26
+ subject.generate_deck
27
+ }.to raise_error(ArgumentError, "card_headers must not be empty")
28
+ end
29
+
30
+ it "raises an ArgumentError if card_data is not a array" do
31
+ subject.card_headers = headers
9
32
  subject.card_data = "I'm card_data!"
10
33
 
11
34
  expect {
12
35
  subject.generate_deck
13
- }.to raise_error(ArgumentError)
36
+ }.to raise_error(ArgumentError, "card_data must be an array")
14
37
  end
15
38
 
16
39
  it "raises an ArgumentError if card_data is an empty array" do
40
+ subject.card_headers = headers
17
41
  subject.card_data = []
18
42
 
19
43
  expect {
20
44
  subject.generate_deck
21
- }.to raise_error(ArgumentError)
45
+ }.to raise_error(ArgumentError, "card_data must not be empty")
46
+ end
47
+
48
+ it "raise an ArgumentError if card values are not hashes" do
49
+ subject.card_headers = headers
50
+ subject.card_data = [ "I'm card data!" ]
51
+
52
+ expect {
53
+ subject.generate_deck
54
+ }.to raise_error(ArgumentError, "card must be a hash")
22
55
  end
23
56
 
24
- it "returns a string with the card_data hashes, separating key and value with a semicolon and adding line breaks" do
57
+ it "returns a string with the card_headers comment, card_data values separated with a semicolon and new cards by line breaks" do
58
+ subject.card_headers = headers
25
59
  subject.card_data = cards
26
- subject.generate_deck.should == "a;b\nc;d"
60
+ expect(subject.generate_deck).to eq("#front;back\na;b\nc;d")
27
61
  end
28
62
 
29
63
  it "saves to a file if the file option is passed" do
64
+ subject.card_headers = headers
30
65
  subject.card_data = cards
31
66
  subject.generate_deck(file: "/tmp/anki_deck.txt")
32
- File.exists?("/tmp/anki_deck.txt").should be_true
67
+ expect(File.exist?("/tmp/anki_deck.txt")).to be_truthy
33
68
  end
34
69
 
35
- it "returns a string including tags if the value of each card data is a hash" do
36
- subject.card_data = [{ "a" => { "value" => "b", "tags" => ["level1"] } },
37
- { "c" => { "value" => "d", "tags" => ["level1", "level2"] } }]
38
-
39
- subject.generate_deck.should match /a;b;level1/
40
- subject.generate_deck.should match /c;d;level1 level2/
41
- end
42
70
  end
43
71
  end
@@ -1,8 +1,8 @@
1
- require "anki"
1
+ require 'anki'
2
2
 
3
3
  RSpec.configure do |config|
4
- config.treat_symbols_as_metadata_keys_with_true_values = true
5
- config.run_all_when_everything_filtered = true
6
- config.filter_run :focus
7
- config.order = 'random'
4
+ config.disable_monkey_patching!
5
+ config.warnings = true
6
+ config.order = :random
7
+ Kernel.srand config.seed
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Martinez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-20 00:00:00.000000000 Z
11
+ date: 2015-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.14'
33
+ version: '3.1'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.14'
40
+ version: '3.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: fakefs
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  version: '0'
96
96
  requirements: []
97
97
  rubyforge_project:
98
- rubygems_version: 2.2.2
98
+ rubygems_version: 2.4.5
99
99
  signing_key:
100
100
  specification_version: 4
101
101
  summary: Generate decks ready to be imported into Anki!