Class: RailChaser::Storage
- Inherits:
-
Object
- Object
- RailChaser::Storage
- Defined in:
- lib/rail_chaser/storage.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #add_class(file) ⇒ Object
- #add_spec(file) ⇒ Object
- #associate_spec_with_class(spec_id, class_id) ⇒ Object
- #create! ⇒ Object
- #db_path ⇒ Object
- #destroy! ⇒ Object
- #find_class_by_file(file) ⇒ Object
- #find_spec_by_file(file) ⇒ Object
- #find_specs_by_class(file) ⇒ Object
-
#initialize(options = {:db_path => 'spec.db'}) ⇒ Storage
constructor
A new instance of Storage.
- #list_classes ⇒ Object
- #list_specs ⇒ Object
- #load! ⇒ Object
- #save!(example_collection) ⇒ Object
Constructor Details
#initialize(options = {:db_path => 'spec.db'}) ⇒ Storage
Returns a new instance of Storage.
7 8 9 |
# File 'lib/rail_chaser/storage.rb', line 7 def initialize(={:db_path => 'spec.db'}) @options = end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/rail_chaser/storage.rb', line 5 def @options end |
Class Method Details
.create(options = {:db_path => 'spec.db'}) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/rail_chaser/storage.rb', line 11 def self.create(={:db_path => 'spec.db'}) storage = RailChaser::Storage.new() storage.destroy! storage.create! storage end |
Instance Method Details
#add_class(file) ⇒ Object
26 27 28 |
# File 'lib/rail_chaser/storage.rb', line 26 def add_class(file) @connection.execute("INSERT INTO classes (file) values ('#{file}');") end |
#add_spec(file) ⇒ Object
30 31 32 |
# File 'lib/rail_chaser/storage.rb', line 30 def add_spec(file) @connection.execute("INSERT INTO specs (file) values ('#{file}');") end |
#associate_spec_with_class(spec_id, class_id) ⇒ Object
34 35 36 |
# File 'lib/rail_chaser/storage.rb', line 34 def associate_spec_with_class(spec_id, class_id) @connection.execute("INSERT INTO specs_classes (spec_id, class_id) values (#{spec_id}, #{class_id});") end |
#create! ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rail_chaser/storage.rb', line 67 def create! @connection = SQLite3::Database.new(db_path) # PRAGMA foreign_keys = ON; sql = <<-SQL CREATE TABLE IF NOT EXISTS specs( id INTEGER PRIMARY KEY NOT NULL, file TEXT UNIQUE NOT NULL ); CREATE TABLE IF NOT EXISTS classes( id INTEGER PRIMARY KEY NOT NULL, file TEXT UNIQUE NOT NULL ); CREATE TABLE IF NOT EXISTS specs_classes( spec_id INTEGER, class_id INTEGER, FOREIGN KEY (spec_id) REFERENCES spec(id) ON DELETE CASCADE, FOREIGN KEY (class_id) REFERENCES class(id) ON DELETE CASCADE ); CREATE INDEX IF NOT EXISTS spec_index ON specs_classes(spec_id); CREATE INDEX IF NOT EXISTS class_index ON specs_classes(class_id); SQL @connection.execute_batch(sql) end |
#db_path ⇒ Object
55 56 57 |
# File 'lib/rail_chaser/storage.rb', line 55 def db_path [:db_path] end |
#destroy! ⇒ Object
63 64 65 |
# File 'lib/rail_chaser/storage.rb', line 63 def destroy! File.delete(db_path) if File.exists?(db_path) end |
#find_class_by_file(file) ⇒ Object
42 43 44 |
# File 'lib/rail_chaser/storage.rb', line 42 def find_class_by_file(file) @connection.execute("SELECT id FROM classes WHERE file = '#{file}';").first.first end |
#find_spec_by_file(file) ⇒ Object
38 39 40 |
# File 'lib/rail_chaser/storage.rb', line 38 def find_spec_by_file(file) @connection.execute("SELECT id FROM specs WHERE file = '#{file}';").first.first end |
#find_specs_by_class(file) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/rail_chaser/storage.rb', line 46 def find_specs_by_class(file) sql = <<-SQL SELECT s.file FROM specs_classes sc JOIN classes c JOIN specs s ON sc.class_id = c.id AND sc.spec_id = s.id WHERE c.file LIKE '%#{file}'; SQL @connection.execute(sql).flatten end |
#list_classes ⇒ Object
22 23 24 |
# File 'lib/rail_chaser/storage.rb', line 22 def list_classes @connection.execute("SELECT file FROM classes").flatten end |
#list_specs ⇒ Object
18 19 20 |
# File 'lib/rail_chaser/storage.rb', line 18 def list_specs @connection.execute("SELECT file FROM specs").flatten end |
#load! ⇒ Object
59 60 61 |
# File 'lib/rail_chaser/storage.rb', line 59 def load! @connection = SQLite3::Database.open(db_path) end |
#save!(example_collection) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rail_chaser/storage.rb', line 92 def save!(example_collection) example_collection.classes.each do |klass| add_class(klass) end example_collection.each_spec_and_classes do |spec, classes| add_spec(spec) spec_id = find_spec_by_file(spec) classes.each do |klass| class_id = find_class_by_file(klass) associate_spec_with_class(spec_id, class_id) end end end |