diff --git a/lib/barby/barcode/bookland.rb b/lib/barby/barcode/bookland.rb index 04c5a98..edbec07 100644 --- a/lib/barby/barcode/bookland.rb +++ b/lib/barby/barcode/bookland.rb @@ -16,6 +16,7 @@ module Barby # # If a prefix is not given, a default of 978 is used. class Bookland < EAN13 + attr_accessor :isbn # isbn should be an ISBN number string, with or without an EAN prefix and an ISBN checksum # non-number formatting like hyphens or spaces are ignored @@ -61,12 +62,11 @@ def initialize(isbn) def isbn=(isbn) - if match = PATTERN.match(isbn.gsub(/\D/, '')) - @number = match[:number] - @prefix = match[:prefix] - else - raise ArgumentError, "Not a valid ISBN: #{isbn}" - end + raise ArgumentError, "Not a valid ISBN: #{isbn}" if valid? + + match = PATTERN.match(isbn.gsub(/\D/, '')) + @number = match[:number] + @prefix = match[:prefix] end def isbn @@ -135,9 +135,15 @@ def inspect "#<#{self.class}:0x#{'%014x' % object_id} #{formatted_isbn}>" end + def valid? + !!PATTERN.match(isbn.gsub(/\D/, '')) + end end#class ISBN + def valid? + !!ISBN::PATTERN.match(isbn.gsub(/\D/, '')) + end end#class Bookland diff --git a/lib/barby/barcode/code_25_iata.rb b/lib/barby/barcode/code_25_iata.rb index 6aa9e5e..f17d5f7 100644 --- a/lib/barby/barcode/code_25_iata.rb +++ b/lib/barby/barcode/code_25_iata.rb @@ -18,6 +18,10 @@ def stop_encoding encoding_for_bars_without_end_space(STOP_ENCODING) end + def valid? + !!data + end + end end diff --git a/lib/barby/barcode/data_matrix.rb b/lib/barby/barcode/data_matrix.rb index f872a8c..97c5b1e 100644 --- a/lib/barby/barcode/data_matrix.rb +++ b/lib/barby/barcode/data_matrix.rb @@ -41,6 +41,9 @@ def to_s end + def valid? + !!data + end end end diff --git a/lib/barby/barcode/gs1_128.rb b/lib/barby/barcode/gs1_128.rb index 94cacbd..746209c 100644 --- a/lib/barby/barcode/gs1_128.rb +++ b/lib/barby/barcode/gs1_128.rb @@ -39,7 +39,6 @@ def to_s "(#{application_identifier}) #{partial_data}" end - end diff --git a/lib/barby/barcode/pdf_417.rb b/lib/barby/barcode/pdf_417.rb index 20e245b..65cd933 100644 --- a/lib/barby/barcode/pdf_417.rb +++ b/lib/barby/barcode/pdf_417.rb @@ -5,6 +5,8 @@ module Barby class Pdf417 < Barcode2D + attr_reader :data + DEFAULT_OPTIONS = { :options => 0, :y_height => 3, @@ -72,5 +74,9 @@ def encoding end enc end + + def valid? + !!data + end end end \ No newline at end of file diff --git a/lib/barby/barcode/qr_code.rb b/lib/barby/barcode/qr_code.rb index 83f9c98..5f0cf29 100644 --- a/lib/barby/barcode/qr_code.rb +++ b/lib/barby/barcode/qr_code.rb @@ -88,6 +88,10 @@ def to_s end + def valid? + !!data + end + private #Generate an RQRCode object with the available values diff --git a/test/code_25_iata_test.rb b/test/code_25_iata_test.rb index 14715cb..f5e7fa8 100644 --- a/test/code_25_iata_test.rb +++ b/test/code_25_iata_test.rb @@ -16,4 +16,7 @@ class Code25IATATest < Barby::TestCase @code.stop_encoding.must_equal '11101' end + it "should be valid" do + @code.valid?.must_equal true + end end diff --git a/test/data_matrix_test.rb b/test/data_matrix_test.rb index 6e971b7..f34ac50 100644 --- a/test/data_matrix_test.rb +++ b/test/data_matrix_test.rb @@ -27,4 +27,7 @@ class DataMatrixTest < Barby::TestCase @code.encoding.wont_equal prev_encoding end + it "should be valid" do + @code.valid?.must_equal true + end end diff --git a/test/pdf_417_test.rb b/test/pdf_417_test.rb index 7221b39..7f70a21 100644 --- a/test/pdf_417_test.rb +++ b/test/pdf_417_test.rb @@ -40,6 +40,10 @@ class Pdf417Test < Barby::TestCase enc[0].length.must_equal 117 end + it "should be valid" do + @code.valid?.must_equal true + end + end end diff --git a/test/qr_code_test.rb b/test/qr_code_test.rb index fc20644..121237f 100644 --- a/test/qr_code_test.rb +++ b/test/qr_code_test.rb @@ -68,7 +68,10 @@ class QrCodeTest < Barby::TestCase QrCode.new('123456789012345678901234567890').to_s.must_equal '12345678901234567890' end - + it "should be valid" do + @code.valid?.must_equal true + end + private def rqrcode(code)