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 +4 -4
- data/.travis.yml +2 -1
- data/README.md +20 -21
- data/anki.gemspec +1 -1
- data/lib/anki/deck.rb +18 -14
- data/lib/anki/version.rb +1 -1
- data/spec/anki/deck_spec.rb +43 -15
- data/spec/spec_helper.rb +5 -5
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d03bcf99764d976e6bc02dbdbf1da11545251922
|
4
|
+
data.tar.gz: 236e8230c7c684cbc3725df691c9bfcfac28e686
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3bbc95a72f555828ef77a8ab885fe20816a20148683fc408f918e4d04cc45fd784c357f5426c8410c8c02dc47bc36cc6e09da424d2d1ba1adcc80d35a15a76a8
|
7
|
+
data.tar.gz: 73b38e18b8b3f2633ef134365e42e9a3c40d2e00717aa62fc4e6106d1a06c05e7b458ca43dc54d59cb9161ee8d1ef6b147c77bc78abe0bdcd22da2d1ee8d2eb8
|
data/.travis.yml
CHANGED
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,
|
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
|
-
# => "
|
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
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|
-
# => "
|
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
|
data/anki.gemspec
CHANGED
@@ -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", "~>
|
22
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
23
23
|
spec.add_development_dependency "fakefs", "~> 0.5"
|
24
24
|
end
|
data/lib/anki/deck.rb
CHANGED
@@ -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, "
|
12
|
-
raise ArgumentError, "
|
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 =
|
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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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)
|
data/lib/anki/version.rb
CHANGED
data/spec/anki/deck_spec.rb
CHANGED
@@ -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(:
|
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
|
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
|
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.
|
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.
|
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
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require
|
1
|
+
require 'anki'
|
2
2
|
|
3
3
|
RSpec.configure do |config|
|
4
|
-
config.
|
5
|
-
config.
|
6
|
-
config.
|
7
|
-
config.
|
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.
|
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:
|
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: '
|
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: '
|
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.
|
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!
|