barby 0.6.1 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad8050c7d031bdc4c866046b84740632b1f72b74
4
- data.tar.gz: daf30f880b830c955b4653684d95996bd0906356
3
+ metadata.gz: 25c48f523056303c71f3477a056906f12613bde3
4
+ data.tar.gz: 1860f349c37684539345aef410f09a570a50f319
5
5
  SHA512:
6
- metadata.gz: 9ba0fac4a2f56fd7c88689d1b09b69c72a1556e138d0e59a807e4dfe39aef3d1b775770e354fa19e0e2e1cd5e2b9525b6e6a9cfe8fc5c25920a2c0396ad7d7b7
7
- data.tar.gz: ce42a339359f0a77ba6127b43be32fc10fc82078a4d99e2883e9a0882c8325f4b5a9b9fe7950841b424b818c8bd23ee895443355d395f6d43bc6448084a273f0
6
+ metadata.gz: a793a5c6a5a5fba522ec2300ec703a3340edd5c45d249b9ca5d2056c2460555e6f4944c0cc4d9ccb8863f6a068a1aa2083f5f5181820d0c70b7a79d2c9cf2799
7
+ data.tar.gz: f7acba5a08dea02a79c1620a2b4743e1b77a1f53469d83bf4d58229c5f5e618dc3f89959d2e858b8dbf80c0fef7ef33cfe211f5bdf28d750d9e578178d4bda6d
@@ -3,36 +3,144 @@ require 'barby/barcode/ean_13'
3
3
 
4
4
  module Barby
5
5
 
6
- #Bookland barcodes are EAN-13 barcodes with number system
7
- #978 (hence "Bookland"). The data they encode is an ISBN
8
- #with its check digit removed. This is a convenience class
9
- #that takes an ISBN no instead of "pure" EAN-13 data.
6
+ # Bookland barcodes are EAN-13 barcodes with number system
7
+ # 978/979 (hence "Bookland"). The data they encode is an ISBN
8
+ # with its check digit removed. This is a convenience class
9
+ # that takes an ISBN number instead of "pure" EAN-13 data.
10
+ #
11
+ # #These are all the same:
12
+ # barcode = Bookland.new('978-0-306-40615-7')
13
+ # barcode = Bookland.new('978-0-306-40615')
14
+ # barcode = Bookland.new('0-306-40615-7')
15
+ # barcode = Bookland.new('0-306-40615')
16
+ #
17
+ # If a prefix is not given, a default of 978 is used.
10
18
  class Bookland < EAN13
11
19
 
12
- BOOKLAND_NUMBER_SYSTEM = '978'
13
-
14
- attr_accessor :isbn
15
-
20
+ # isbn should be an ISBN number string, with or without an EAN prefix and an ISBN checksum
21
+ # non-number formatting like hyphens or spaces are ignored
22
+ #
23
+ # If a prefix is not given, a default of 978 is used.
16
24
  def initialize(isbn)
17
25
  self.isbn = isbn
18
26
  raise ArgumentError, 'data not valid' unless valid?
19
27
  end
20
28
 
21
29
  def data
22
- BOOKLAND_NUMBER_SYSTEM+isbn_only
30
+ isbn.isbn
23
31
  end
24
32
 
25
- #Removes any non-digit characters, number system and check digit
26
- #from ISBN, so "978-82-92526-14-9" would result in "829252614"
27
- def isbn_only
28
- s = isbn.gsub(/[^0-9]/, '')
29
- if s.size > 10#Includes number system
30
- s[3,9]
31
- else#No number system, may include check digit
32
- s[0,9]
33
- end
33
+ def isbn=(isbn)
34
+ @isbn = ISBN.new(isbn)
34
35
  end
35
36
 
36
- end
37
+ #An instance of ISBN
38
+ def isbn
39
+ @isbn
40
+ end
41
+
42
+
43
+ # Encapsulates an ISBN number
44
+ #
45
+ # isbn = ISBN.new('978-0-306-40615')
46
+ class ISBN
47
+
48
+ DEFAULT_PREFIX = '978'
49
+ PATTERN = /\A(?<prefix>\d\d\d)?(?<number>\d{9})(?<checksum>\d)?\Z/
50
+
51
+ attr_reader :number
52
+
53
+
54
+ # Takes one argument, which is the ISBN string with or without prefix and/or check digit.
55
+ # Non-digit characters like hyphens and spaces are ignored.
56
+ #
57
+ # Prefix is 978 if not given.
58
+ def initialize(isbn)
59
+ self.isbn = isbn
60
+ end
61
+
62
+
63
+ def isbn=(isbn)
64
+ if match = PATTERN.match(isbn.gsub(/\D/, ''))
65
+ @number = match[:number]
66
+ @prefix = match[:prefix]
67
+ else
68
+ raise ArgumentError, "Not a valid ISBN: #{isbn}"
69
+ end
70
+ end
71
+
72
+ def isbn
73
+ "#{prefix}#{number}"
74
+ end
75
+
76
+ def isbn_with_checksum
77
+ "#{isbn}#{checksum}"
78
+ end
79
+
80
+ def isbn_10
81
+ number
82
+ end
83
+
84
+ def isbn_10_with_checksum
85
+ "#{number}#{checksum}"
86
+ end
87
+
88
+ def formatted_isbn
89
+ "#{prefix}-#{number}-#{checksum}"
90
+ end
91
+
92
+ def formatted_isbn_10
93
+ "#{number}-#{checksum}"
94
+ end
95
+
96
+
97
+ def prefix
98
+ @prefix || DEFAULT_PREFIX
99
+ end
100
+
101
+ def digits
102
+ (prefix+number).split('').map(&:to_i)
103
+ end
104
+
105
+ def isbn_10_digits
106
+ number.split('').map(&:to_i)
107
+ end
108
+
109
+
110
+ # Calculates the ISBN 13-digit checksum following the algorithm from:
111
+ # http://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-13_check_digit_calculation
112
+ def checksum
113
+ (10 - (digits.each_with_index.inject(0) do |sum, (digit, index)|
114
+ sum + (digit * (index.even? ? 1 : 3))
115
+ end % 10)) % 10
116
+ end
117
+
118
+
119
+ ISBN_10_CHECKSUM_MULTIPLIERS = [10,9,8,7,6,5,4,3,2]
120
+
121
+ # Calculates the ISBN 10-digit checksum following the algorithm from:
122
+ # http://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-10_check_digit_calculation
123
+ def isbn_10_checksum
124
+ isbn_10_digits.zip(ISBN_10_CHECKSUM_MULTIPLIERS).inject(0) do |sum, (digit, multiplier)|
125
+ sum + (digit * multiplier)
126
+ end
127
+ end
128
+
129
+
130
+ def to_s
131
+ isbn_with_checksum
132
+ end
133
+
134
+ def inspect
135
+ klass = (self.class.ancestors + [self.class.name]).join(':')
136
+ "#<#{self.class}:0x#{'%014x' % object_id} #{formatted_isbn}>"
137
+ end
138
+
139
+
140
+ end#class ISBN
141
+
142
+
143
+ end#class Bookland
144
+
37
145
 
38
- end
146
+ end#module Barby
@@ -2,7 +2,7 @@ module Barby #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 6
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tore Darell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-31 00:00:00.000000000 Z
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Barby creates barcodes.
14
14
  email: toredarell@gmail.com