Skip to content

Commit

Permalink
First passing spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwnall committed Oct 15, 2010
1 parent ce9068e commit f80ca58
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 15 deletions.
12 changes: 12 additions & 0 deletions .project
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>anki-import</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
<nature>org.radrails.rails.core.railsnature</nature>
</natures>
</projectDescription>
7 changes: 2 additions & 5 deletions Gemfile
@@ -1,10 +1,7 @@
source "http://rubygems.org"
# Add dependencies required to use your gem here.
# Example:
# gem "activesupport", ">= 2.3.5"
gem 'activesupport', '>= 3.0.0', :require => 'active_support/core_ext'
gem 'sqlite3-ruby', '>= 1.3.1', :require => 'sqlite3'

# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
gem "rspec", "~> 2.0.0"
gem "bundler", "~> 1.0.0"
Expand Down
32 changes: 32 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,32 @@
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.2)
git (1.2.5)
jeweler (1.5.0.pre5)
bundler (~> 1.0.0)
git (>= 1.2.5)
rake
rake (0.8.7)
rcov (0.9.9)
rspec (2.0.0)
rspec-core (= 2.0.0)
rspec-expectations (= 2.0.0)
rspec-mocks (= 2.0.0)
rspec-core (2.0.0)
rspec-expectations (2.0.0)
diff-lcs (>= 1.1.2)
rspec-mocks (2.0.0)
rspec-core (= 2.0.0)
rspec-expectations (= 2.0.0)
sqlite3-ruby (1.3.1)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.0.0)
jeweler (~> 1.5.0.pre5)
rcov
rspec (~> 2.0.0)
sqlite3-ruby (>= 1.3.1)
2 changes: 1 addition & 1 deletion README.rdoc
@@ -1,6 +1,6 @@
= anki_importer

Description goes here.
Imports data from Anki deck databases.

== Contributing to anki_importer

Expand Down
12 changes: 7 additions & 5 deletions Rakefile
Expand Up @@ -12,16 +12,18 @@ require 'rake'
require 'jeweler'
Jeweler::Tasks.new do |gem|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
gem.name = "anki_importer"
gem.summary = %Q{TODO: one-line summary of your gem}
gem.description = %Q{TODO: longer description of your gem}
gem.email = "costan@gmail.com"
gem.name = "anki-importer"
gem.summary = %Q{Importer for Anki decks}
gem.description = %Q{Extracts models, facts and cards from Anki deck databases.}
gem.email = "victor@costan.us"
gem.homepage = "http://github.com/pwnall/anki_importer"
gem.authors = ["Victor Costan"]
# Include your dependencies below. Runtime dependencies are required when using your gem,
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
# spec.add_runtime_dependency 'jabber4r', '> 0.1'
# spec.add_development_dependency 'rspec', '> 1.2.3'
gem.add_runtime_dependency 'sqlite3-ruby', '>= 1.3.1'
gem.add_runtime_dependency 'activesupport', '>= 3.0.0'
gem.add_development_dependency "rspec", "~> 2.0.0"
gem.add_development_dependency "bundler", "~> 1.0.0"
gem.add_development_dependency "jeweler", "~> 1.5.0.pre5"
Expand All @@ -47,7 +49,7 @@ Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""

rdoc.rdoc_dir = 'rdoc'
rdoc.title = "anki_importer #{version}"
rdoc.title = "anki-importer #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end
2 changes: 2 additions & 0 deletions lib/anki/importer.rb
@@ -0,0 +1,2 @@
require 'anki/importer/deck.rb'
require 'anki/importer/model.rb'
51 changes: 51 additions & 0 deletions lib/anki/importer/deck.rb
@@ -0,0 +1,51 @@
require 'active_support/core_ext'
require 'sqlite3'

# :nodoc: namespace
module Anki
# :nodoc: namespace
module Importer

# The root of the Anki object graph.
class Deck
# Array of models.
attr_reader :models

# Reads an Anki deck database.
#
# Args:
# deck_path:: path to an Anki deck on the filesystem
#
# Returns a deck.
def self.from_file(deck_path)
deck_db = SQLite3::Database.new deck_path
from_db deck_db
end

# Reads an Anki deck database.
#
# Args:
# deck_db:: a Sqlite3::Datbase
#
# Returns a deck.
def self.from_db(deck_db)
deck = self.new
deck.models = Model.from_db deck_db

deck
end

# :nodoc: private
def initialize
end

# :nodoc: private
def models=(new_models)
@models = new_models
@models_by_id = new_models.index_by(&:anki_id)
end
# :nodoc: private
attr_reader :models_by_id
end # class Anki::Importer::Deck
end # namespace Anki::Importer
end # namespace Anki
45 changes: 45 additions & 0 deletions lib/anki/importer/model.rb
@@ -0,0 +1,45 @@
require 'sqlite3'

# :nodoc: namespace
module Anki
# :nodoc: namespace
module Importer

# Schema for Anki cards.
#
# A deck can have multiple models.
class Model
# Name assigned in the Anki UI.
attr_reader :name
# Generally empty.
attr_reader :description
# Assigned in the Anki UI. Space-separated string.
attr_reader :tags
# Unique ID in the models table.
attr_reader :anki_id

# Reads the models from an Anki deck.
#
# Args:
# deck_db:: a Sqlite3::Datbase
#
# Returns an array of Model instances.
def self.from_db(deck_db)
query = 'SELECT id, name, description, tags, features FROM models'
models = deck_db.execute(query).map do |anki_id, name, description, tags,
features|
self.new anki_id, name, description, tags, features
end
end

# :nodoc: private
def initialize(anki_id, name, description, tags, features)
@anki_id = anki_id
@name = name
@description = description
@tags = tags
@features = features
end
end # class Anki::Importer::Model
end # namespace Anki::Importer
end # namespace Anki
Empty file removed lib/anki_importer.rb
Empty file.
25 changes: 22 additions & 3 deletions spec/anki_importer_spec.rb
@@ -1,7 +1,26 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe "AnkiImporter" do
it "fails" do
fail "hey buddy, you should probably rename this file and start specing for real"
describe "anki-importer" do
let(:deck) do
deck_path = File.join File.dirname(__FILE__), 'fixtures', 'deck.anki'
Anki::Importer::Deck.from_file deck_path
end

describe 'Deck' do
it 'should have a Model' do
deck.should have(1).model
end
end

describe 'Model' do
let(:model) { deck.models.first }

it 'should extract the name correctly' do
model.name.should == 'CSS Color'
end

it 'should extract tags correctly' do
model.tags.should == 'css html color'
end
end
end
Binary file added spec/fixtures/deck.anki
Binary file not shown.
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
@@ -1,4 +1,4 @@
require 'anki_importer'
require 'anki/importer'
require 'rspec'

# Requires supporting files with custom matchers and macros, etc,
Expand Down

0 comments on commit f80ca58

Please sign in to comment.