bin/booklist in booklist-0.0.1 vs bin/booklist in booklist-0.0.7

- old
+ new

@@ -21,19 +21,21 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. require 'optparse' +require 'optparse/date' require 'ostruct' require 'rubygems' require 'active_record' require 'yaml' require 'pathname' +require 'booklist' +require 'date' module Booklist options = OpenStruct.new - STATES = { "read" => "Mark as read", "reading" => "Mark as currently reading", "toread" => "Mark as to-read" } options.act_num = 0 usage = nil OptionParser.new do |opts| opts.banner = "Usage: booklist [options]" @@ -93,10 +95,14 @@ opts.on("--tags X,Y,Z", Array, "List of tags") do |tags| options.tags = tags end + opts.on("--date_read DATE", Date, "Date read (eg 2014-02-14)") do |dr| + options.date_read = dr + end + opts.on("--id ID", Integer, "Book ID number") do |id| options.id = id end opts.separator "" @@ -105,20 +111,20 @@ # TODO - configurable booklist path #opts.on("-c", "--config FILE", "Use configuration file") do |file| # options.config_file = file #end - opts.on("--debug", "Debug node") do + opts.on("--debug", "Debug mode") do options.debug = true end opts.on("--updatedb", "Update the book database schema") do options.update_schema = true end opts.on_tail("--version", "Get the booklist version") do - puts Booklist::version + puts Booklist::VERSION exit end opts.on_tail("-h", "--help", "Show this message") do puts opts @@ -147,59 +153,23 @@ if options.update_schema || !File.exist?(booklist_db_path) ActiveRecord::Migrator.migrate('db/migrate') end - class Book < ActiveRecord::Base - has_many :taggings - has_many :tags, through: :taggings #, source: "book_id" - validates :title, presence: true - - def cli_display - puts "ID: #{id}\n" - puts "Title: #{title}\n" - puts "Author: #{author}\n" if author - puts "Additional authors: #{addn_authors}" if addn_authors - puts "State: #{state}" if state - puts "Tags: #{tag_list}\n" if tags.count > 0 - puts "\n" - end - - def tag_list - #put tags - tags.map(&:name).join(", ") - end - - def tag_list=(names) - self.tags = names.map do |n| - Tag.where(name: n.strip).first_or_create! - end - end - end - - class Tag < ActiveRecord::Base - has_many :taggings - has_many :books, through: :taggings #, source: "tag_id" - end - - class Tagging < ActiveRecord::Base - belongs_to :tag - belongs_to :book - end - if options.act_num = 1 if options.action == :add if !options.title || options.title == "" puts "Please add a --title with the book name" puts usage exit end book = Book.new do |b| b.title = options.title b.author = options.author if options.author - b.addn_authors if options.addn_authors + b.addn_authors = options.addn_authors if options.addn_authors b.state = options.state if options.state + b.date_read = options.date_read if options.date_read b.tag_list = options.tags if options.tags end book.save book.cli_display elsif options.action == :edit @@ -207,10 +177,11 @@ if book book.title = options.title if options.title book.author = options.author if options.author book.addn_authors = options.addn_authors if options.addn_authors book.state = options.state if options.state + book.date_read = options.date_read if options.date_read book.tag_list = options.tags if options.tags book.save book.cli_display else puts "No book with ID of #{options.id} was found" @@ -224,20 +195,21 @@ book.cli_display else puts "No book with ID of #{options.id} was found" end elsif options.action == :search - books = Book.where(["title LIKE ?", "%#{options.title}%"]) if options.title - # TODO - books << Book.where(["author LIKE ? or addn_authors LIKE ?", "%#{options.author}%", "%#{options.author}%"]) if options.author - # TODO show unique records - books.each { |b| - b.cli_display - } + books = [] + books += Book.where(["title LIKE ?", "%#{options.title}%"]).to_a if options.title + books += Book.where(["author LIKE ? or addn_authors LIKE ?", "%#{options.author}%", "%#{options.author}%"]).to_a if options.author + books += Book.where(["author LIKE ? or addn_authors LIKE ?", "%#{options.addn_authors}%", "%#{options.addn_authors}%"]).to_a if options.addn_authors + books += Book.where(["state = ?", "%#{options.state}%"]).to_a if options.state + books.uniq! + books.sort!{ |a, b| a.id <=> b.id} + books.select!{ |b| b.state == options.state} if options.state + books.each { |b| b.cli_display } elsif options.action == :list books = Book.all - books.each{ |b| - b.cli_display - } + books.each{ |b| b.cli_display } end elsif options.act_num > 1 puts "Please only use one action." puts usage exit