booklist 0.0.1 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/booklist +22 -50
- data/booklist.gemspec +1 -0
- data/lib/booklist/book.rb +29 -0
- data/lib/booklist/tag.rb +11 -0
- data/lib/booklist/version.rb +1 -1
- data/lib/booklist.rb +3 -0
- data/spec/book_spec.rb +61 -0
- data/spec/support/active_record.rb +26 -0
- metadata +31 -16
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aa6eb403053898f7677b45118fb2ae78fe33c6fe
|
4
|
+
data.tar.gz: 3c3a141cbb261f36115e67b51253ccf597534445
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6f765a2a23c68fefe682ccae5c9665879f4a68fc44f7a28a46961e6cb5f263e024d4f2c84ee8e180072778658ab31a1065acaa90417def68ca4aea5af3ecac97
|
7
|
+
data.tar.gz: f4cbc5aa0b7cac9be2f71bc174f8a0b29e8140c37ee6de592ecb5dfa45cd2c98b1be42069ab7f2fca2ca23bcf32c49f48cd3c5e029d6e1baa841a965059f5753
|
data/bin/booklist
CHANGED
@@ -23,15 +23,17 @@
|
|
23
23
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
24
24
|
|
25
25
|
require 'optparse'
|
26
|
+
require 'optparse/date'
|
26
27
|
require 'ostruct'
|
27
28
|
require 'rubygems'
|
28
29
|
require 'active_record'
|
29
30
|
require 'yaml'
|
30
31
|
require 'pathname'
|
32
|
+
require 'booklist'
|
33
|
+
require 'date'
|
31
34
|
|
32
35
|
module Booklist
|
33
36
|
options = OpenStruct.new
|
34
|
-
STATES = { "read" => "Mark as read", "reading" => "Mark as currently reading", "toread" => "Mark as to-read" }
|
35
37
|
options.act_num = 0
|
36
38
|
usage = nil
|
37
39
|
OptionParser.new do |opts|
|
@@ -95,6 +97,10 @@ module Booklist
|
|
95
97
|
options.tags = tags
|
96
98
|
end
|
97
99
|
|
100
|
+
opts.on("--date_read DATE", Date, "Date read (eg 2014-02-14)") do |dr|
|
101
|
+
options.date_read = dr
|
102
|
+
end
|
103
|
+
|
98
104
|
opts.on("--id ID", Integer, "Book ID number") do |id|
|
99
105
|
options.id = id
|
100
106
|
end
|
@@ -107,7 +113,7 @@ module Booklist
|
|
107
113
|
# options.config_file = file
|
108
114
|
#end
|
109
115
|
|
110
|
-
opts.on("--debug", "Debug
|
116
|
+
opts.on("--debug", "Debug mode") do
|
111
117
|
options.debug = true
|
112
118
|
end
|
113
119
|
|
@@ -116,7 +122,7 @@ module Booklist
|
|
116
122
|
end
|
117
123
|
|
118
124
|
opts.on_tail("--version", "Get the booklist version") do
|
119
|
-
puts Booklist::
|
125
|
+
puts Booklist::VERSION
|
120
126
|
exit
|
121
127
|
end
|
122
128
|
|
@@ -149,43 +155,6 @@ module Booklist
|
|
149
155
|
ActiveRecord::Migrator.migrate('db/migrate')
|
150
156
|
end
|
151
157
|
|
152
|
-
class Book < ActiveRecord::Base
|
153
|
-
has_many :taggings
|
154
|
-
has_many :tags, through: :taggings #, source: "book_id"
|
155
|
-
validates :title, presence: true
|
156
|
-
|
157
|
-
def cli_display
|
158
|
-
puts "ID: #{id}\n"
|
159
|
-
puts "Title: #{title}\n"
|
160
|
-
puts "Author: #{author}\n" if author
|
161
|
-
puts "Additional authors: #{addn_authors}" if addn_authors
|
162
|
-
puts "State: #{state}" if state
|
163
|
-
puts "Tags: #{tag_list}\n" if tags.count > 0
|
164
|
-
puts "\n"
|
165
|
-
end
|
166
|
-
|
167
|
-
def tag_list
|
168
|
-
#put tags
|
169
|
-
tags.map(&:name).join(", ")
|
170
|
-
end
|
171
|
-
|
172
|
-
def tag_list=(names)
|
173
|
-
self.tags = names.map do |n|
|
174
|
-
Tag.where(name: n.strip).first_or_create!
|
175
|
-
end
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
class Tag < ActiveRecord::Base
|
180
|
-
has_many :taggings
|
181
|
-
has_many :books, through: :taggings #, source: "tag_id"
|
182
|
-
end
|
183
|
-
|
184
|
-
class Tagging < ActiveRecord::Base
|
185
|
-
belongs_to :tag
|
186
|
-
belongs_to :book
|
187
|
-
end
|
188
|
-
|
189
158
|
if options.act_num = 1
|
190
159
|
if options.action == :add
|
191
160
|
if !options.title || options.title == ""
|
@@ -196,8 +165,9 @@ module Booklist
|
|
196
165
|
book = Book.new do |b|
|
197
166
|
b.title = options.title
|
198
167
|
b.author = options.author if options.author
|
199
|
-
b.addn_authors if options.addn_authors
|
168
|
+
b.addn_authors = options.addn_authors if options.addn_authors
|
200
169
|
b.state = options.state if options.state
|
170
|
+
b.date_read = options.date_read if options.date_read
|
201
171
|
b.tag_list = options.tags if options.tags
|
202
172
|
end
|
203
173
|
book.save
|
@@ -209,6 +179,7 @@ module Booklist
|
|
209
179
|
book.author = options.author if options.author
|
210
180
|
book.addn_authors = options.addn_authors if options.addn_authors
|
211
181
|
book.state = options.state if options.state
|
182
|
+
book.date_read = options.date_read if options.date_read
|
212
183
|
book.tag_list = options.tags if options.tags
|
213
184
|
book.save
|
214
185
|
book.cli_display
|
@@ -226,17 +197,18 @@ module Booklist
|
|
226
197
|
puts "No book with ID of #{options.id} was found"
|
227
198
|
end
|
228
199
|
elsif options.action == :search
|
229
|
-
books =
|
230
|
-
|
231
|
-
|
232
|
-
books.
|
233
|
-
|
234
|
-
|
200
|
+
books = []
|
201
|
+
books += Book.where(["title LIKE ?", "%#{options.title}%"]).to_a if options.title
|
202
|
+
books += Book.where(["author LIKE ? or addn_authors LIKE ?", "%#{options.author}%", "%#{options.author}%"]).to_a if options.author
|
203
|
+
books += Book.where(["author LIKE ? or addn_authors LIKE ?", "%#{options.addn_authors}%", "%#{options.addn_authors}%"]).to_a if options.addn_authors
|
204
|
+
books += Book.where(["state = ?", "%#{options.state}%"]).to_a if options.state
|
205
|
+
books.uniq!
|
206
|
+
books.sort!{ |a, b| a.id <=> b.id}
|
207
|
+
books.select!{ |b| b.state == options.state} if options.state
|
208
|
+
books.each { |b| b.cli_display }
|
235
209
|
elsif options.action == :list
|
236
210
|
books = Book.all
|
237
|
-
books.each{ |b|
|
238
|
-
b.cli_display
|
239
|
-
}
|
211
|
+
books.each{ |b| b.cli_display }
|
240
212
|
end
|
241
213
|
elsif options.act_num > 1
|
242
214
|
puts "Please only use one action."
|
data/booklist.gemspec
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Booklist
|
2
|
+
class Book < ActiveRecord::Base
|
3
|
+
has_many :taggings
|
4
|
+
has_many :tags, through: :taggings #, source: "book_id"
|
5
|
+
validates :title, presence: true
|
6
|
+
|
7
|
+
def cli_display
|
8
|
+
puts "ID: #{id}\n"
|
9
|
+
puts "Title: #{title}\n"
|
10
|
+
puts "Author: #{author}\n" if author
|
11
|
+
puts "Additional authors: #{addn_authors}" if addn_authors
|
12
|
+
puts "State: #{state}" if state
|
13
|
+
puts "Date Read: #{date_read.to_date.to_s}" if date_read
|
14
|
+
puts "Tags: #{tag_list}\n" if tags.count > 0
|
15
|
+
puts "\n"
|
16
|
+
end
|
17
|
+
|
18
|
+
def tag_list
|
19
|
+
#put tags
|
20
|
+
tags.map(&:name).join(", ")
|
21
|
+
end
|
22
|
+
|
23
|
+
def tag_list=(names)
|
24
|
+
self.tags = names.map do |n|
|
25
|
+
Tag.where(name: n.strip).first_or_create!
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/booklist/tag.rb
ADDED
data/lib/booklist/version.rb
CHANGED
data/lib/booklist.rb
ADDED
data/spec/book_spec.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'support/active_record'
|
2
|
+
require 'booklist/book'
|
3
|
+
|
4
|
+
module Booklist
|
5
|
+
describe Book do
|
6
|
+
it "requires a name" do
|
7
|
+
subject.title = ""
|
8
|
+
expect(subject).to have_at_least(1).error_on(:title)
|
9
|
+
subject.title = "The Importance of Living"
|
10
|
+
expect(subject).to be_valid
|
11
|
+
end
|
12
|
+
|
13
|
+
context "simple book" do
|
14
|
+
let(:book) {
|
15
|
+
Book.new do |b|
|
16
|
+
b.title = "The Epic of Gilgamesh"
|
17
|
+
end
|
18
|
+
}
|
19
|
+
|
20
|
+
it "has no author" do
|
21
|
+
expect(book.author).to be_nil
|
22
|
+
end
|
23
|
+
it "has no additional authors" do
|
24
|
+
expect(book.addn_authors).to be_nil
|
25
|
+
end
|
26
|
+
it "has no state" do
|
27
|
+
expect(book.state).to be_nil
|
28
|
+
end
|
29
|
+
it "is a valid record" do
|
30
|
+
expect(book).to be_valid
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "complex book" do
|
35
|
+
let(:book) {
|
36
|
+
Book.new do |b|
|
37
|
+
b.title = "Kareem"
|
38
|
+
b.author = "Kareem Abdul-Jabbar"
|
39
|
+
b.addn_authors = "Mignon McCarthy"
|
40
|
+
b.state = "read"
|
41
|
+
b.date_read = '2001-01-01'
|
42
|
+
end
|
43
|
+
}
|
44
|
+
it "has an author" do
|
45
|
+
expect(book.author).to eq "Kareem Abdul-Jabbar"
|
46
|
+
end
|
47
|
+
it "has an additional author" do
|
48
|
+
expect(book.addn_authors).to eq "Mignon McCarthy"
|
49
|
+
end
|
50
|
+
it "has a state" do
|
51
|
+
expect(book.state).to eq "read"
|
52
|
+
end
|
53
|
+
it "has a custom read date" do
|
54
|
+
expect(book.date_read).to eq '2001-01-01'
|
55
|
+
end
|
56
|
+
it "is a valid record" do
|
57
|
+
expect(book).to be_valid
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
3
|
+
|
4
|
+
I18n.enforce_available_locales = false
|
5
|
+
|
6
|
+
ActiveRecord::Migrator.up "db/migrate"
|
7
|
+
|
8
|
+
module ActiveModel::Validations
|
9
|
+
# Extension to enhance `should have` on AR Model instances. Calls
|
10
|
+
# model.valid? in order to prepare the object's errors object.
|
11
|
+
#
|
12
|
+
# You can also use this to specify the content of the error messages.
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
#
|
16
|
+
# model.should have(:no).errors_on(:attribute)
|
17
|
+
# model.should have(1).error_on(:attribute)
|
18
|
+
# model.should have(n).errors_on(:attribute)
|
19
|
+
#
|
20
|
+
# model.errors_on(:attribute).should include("can't be blank")
|
21
|
+
def errors_on(attribute)
|
22
|
+
self.valid?
|
23
|
+
[self.errors[attribute]].flatten.compact
|
24
|
+
end
|
25
|
+
alias :error_on :errors_on
|
26
|
+
end
|
metadata
CHANGED
@@ -1,36 +1,32 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: booklist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.7
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Dafydd Crosby
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-02-23 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: sqlite3
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: activerecord
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,11 +34,24 @@ dependencies:
|
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: 4.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
46
55
|
description: A book collection program
|
47
56
|
email: dafydd@dafyddcrosby.com
|
48
57
|
executables:
|
@@ -56,30 +65,36 @@ files:
|
|
56
65
|
- bin/booklist
|
57
66
|
- booklist.gemspec
|
58
67
|
- db/migrate/001_initial.rb
|
68
|
+
- lib/booklist.rb
|
69
|
+
- lib/booklist/book.rb
|
70
|
+
- lib/booklist/tag.rb
|
59
71
|
- lib/booklist/version.rb
|
72
|
+
- spec/book_spec.rb
|
73
|
+
- spec/support/active_record.rb
|
60
74
|
homepage: https://github.com/dafyddcrosby/booklist
|
61
75
|
licenses:
|
62
76
|
- bsd
|
77
|
+
metadata: {}
|
63
78
|
post_install_message:
|
64
79
|
rdoc_options: []
|
65
80
|
require_paths:
|
66
81
|
- lib
|
67
82
|
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
-
none: false
|
69
83
|
requirements:
|
70
|
-
- -
|
84
|
+
- - '>='
|
71
85
|
- !ruby/object:Gem::Version
|
72
86
|
version: '1.9'
|
73
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
-
none: false
|
75
88
|
requirements:
|
76
|
-
- -
|
89
|
+
- - '>='
|
77
90
|
- !ruby/object:Gem::Version
|
78
91
|
version: '0'
|
79
92
|
requirements: []
|
80
93
|
rubyforge_project:
|
81
|
-
rubygems_version:
|
94
|
+
rubygems_version: 2.0.14
|
82
95
|
signing_key:
|
83
|
-
specification_version:
|
96
|
+
specification_version: 4
|
84
97
|
summary: Booklist helps you keep a list of books you've read or want to read.
|
85
|
-
test_files:
|
98
|
+
test_files:
|
99
|
+
- spec/book_spec.rb
|
100
|
+
- spec/support/active_record.rb
|