gs1 1.1.0 → 2.0.0

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.
data/lib/gs1/record.rb CHANGED
@@ -15,7 +15,10 @@ module GS1
15
15
  @descendants = []
16
16
 
17
17
  class << self
18
+ def generated = false
19
+
18
20
  def inherited(subclass)
21
+ super
19
22
  descendants << subclass
20
23
  end
21
24
 
@@ -0,0 +1,223 @@
1
+ require 'gs1/syntax_dictionary/specification_parser'
2
+
3
+ module GS1
4
+ module SyntaxDictionary
5
+ # Parser for the GS1 syntax dictionary.
6
+ #
7
+ # The latest version of the syntax dictionary is available at:
8
+ # https://ref.gs1.org/tools/gs1-barcode-syntax-resource/syntax-dictionary.
9
+ #
10
+ # The syntax dictionary has been copied to gs1-syntax-dictionary.txt. The
11
+ # format of the syntax dictionary is available at the top of the dictionary.
12
+ class Parser
13
+ # Some generic String helpers.
14
+ module StringHelpers
15
+ refine String do
16
+ def blank? = empty? || /\A[[:space:]]*\z/.match?(self)
17
+
18
+ def comment? = start_with?('#')
19
+ end
20
+ end
21
+
22
+ using StringHelpers
23
+
24
+ # Corresponds to a single entry in the syntax dictionary.
25
+ class Entry
26
+ # @return [String] the application identifier
27
+ attr_reader :ai
28
+
29
+ # @return [Boolean] indicates if a separator is required or not
30
+ attr_reader :separator_required
31
+
32
+ # @return [Range<Integer>] the length of the AI
33
+ attr_reader :length
34
+
35
+ # @return [String] the title of the AI
36
+ attr_reader :title
37
+
38
+ # rubocop:disable Naming/MethodParameterName
39
+ def initialize(ai:, separator_required:, length:, title:)
40
+ # rubocop:enable Naming/MethodParameterName
41
+ @ai = ai
42
+ @separator_required = separator_required
43
+ @length = length
44
+ @title = title
45
+ end
46
+ end
47
+
48
+ # @param data [String] the syntax dictionary data to parse
49
+ def initialize(data)
50
+ @data = data
51
+ end
52
+
53
+ # Parses the syntax dictionary data and returns a list of dictionary
54
+ # entries.
55
+ #
56
+ # @return [Array<Entry>] a list of syntax dictionary entries
57
+ def parse = entry_lines.flat_map { parse_line(_1) }
58
+
59
+ private
60
+
61
+ attr_reader :data
62
+
63
+ def lines = @lines ||= data.split("\n")
64
+
65
+ def entry_lines = @entry_lines ||= lines.reject { _1.comment? || _1.blank? }
66
+
67
+ def columns(line_data)
68
+ line_data.match(column_patterns.entry_rx)
69
+ &.named_captures
70
+ &.values_at(*%w[ais flags spec title]) or
71
+ raise "Invalid entry format: #{line_data}"
72
+ end
73
+
74
+ def parse_line(line_data)
75
+ ais, flags, specification, base_title = columns(line_data)
76
+
77
+ parse_ais(ais, base_title).map do |(ai, title)|
78
+ Entry.new(
79
+ ai: ai,
80
+ separator_required: separator_required?(flags),
81
+ length: SpecificationParser.new(specification).parse,
82
+ title: title
83
+ )
84
+ end
85
+ end
86
+
87
+ def parse_ais(ai_data, base_title)
88
+ first, last = ai_data.split('-')
89
+ last ||= first
90
+ range = first..last
91
+ range.map { [_1, build_title(range, base_title, _1)] }
92
+ end
93
+
94
+ def build_title(ai_range, base_title, current_ai)
95
+ if ai_range.count == 1
96
+ base_title || current_ai
97
+ else
98
+ base_title ? "#{base_title}_#{current_ai}" : current_ai
99
+ end
100
+ end
101
+
102
+ def separator_required?(flags) = !flags.include?('*')
103
+
104
+ def column_patterns = @column_patterns ||= ColumnPatterns.new
105
+
106
+ # Patterns stolen from: https://github.com/gs1/gs1-syntax-engine/blob/f8b6345655fa9fa25ce1c5a11c5a317f9239529a/src/c-lib/build-embedded-ai-table.pl#L17-L100
107
+ # The naming and extra indentation is to keep the code as close to the
108
+ # original Perl code above.
109
+ #
110
+ # rubocop:disable Lint/DuplicateRegexpCharacterClassElement
111
+ # rubocop:disable Style/RegexpLiteral
112
+ class ColumnPatterns
113
+ # rubocop:disable Metrics/MethodLength
114
+ def entry_rx
115
+ /
116
+ ^
117
+ (?<ais>#{ai_rng_rx})
118
+ (
119
+ \s+
120
+ (?<flags>#{flags_rx})
121
+ )?
122
+ \s+
123
+ (?<spec>#{spec_rx})
124
+ (
125
+ \s+
126
+ (?<keyvals>
127
+ (#{keyval_rx}\s+)*
128
+ #{keyval_rx}
129
+ )
130
+ )?
131
+ (
132
+ \s+
133
+ \#
134
+ \s
135
+ (?<title>#{title_rx})
136
+ )?
137
+ \s*
138
+ $
139
+ /x
140
+ end
141
+ # rubocop:enable Metrics/MethodLength
142
+
143
+ private
144
+
145
+ def ai_rx
146
+ /
147
+ (
148
+ (0\d)
149
+ |
150
+ ([1-9]\d{1,3})
151
+ )
152
+ /x
153
+ end
154
+
155
+ def ai_rng_rx = /#{ai_rx}(-#{ai_rx})?/
156
+
157
+ def flags_rx
158
+ /
159
+ [
160
+ *?!"%&'()+,.:;<=>@\[_`{|}~
161
+ \$ \- \/ \\ \] \^
162
+ ]+
163
+ /x
164
+ end
165
+
166
+ def type_mand_rx
167
+ /
168
+ [XNYZ]
169
+ (
170
+ ([1-9]\d?)
171
+ |
172
+ (\.\.[1-9]\d?)
173
+ )
174
+ /x
175
+ end
176
+
177
+ def type_opt_rx
178
+ /
179
+ \[#{type_mand_rx}\]
180
+ /x
181
+ end
182
+
183
+ def type_rx
184
+ /
185
+ (
186
+ #{type_mand_rx}
187
+ |
188
+ #{type_opt_rx}
189
+ )
190
+ /x
191
+ end
192
+
193
+ def comp_rx
194
+ /
195
+ #{type_rx}
196
+ (,\w+)*
197
+ /x
198
+ end
199
+
200
+ def spec_rx
201
+ /
202
+ #{comp_rx}
203
+ (\s+#{comp_rx})*
204
+ /x
205
+ end
206
+
207
+ def keyval_rx
208
+ /
209
+ (
210
+ (\w+)
211
+ |
212
+ (\w+=\S+)
213
+ )
214
+ /x
215
+ end
216
+
217
+ def title_rx = /\S.*\S/
218
+ end
219
+ # rubocop:enable Lint/DuplicateRegexpCharacterClassElement
220
+ # rubocop:enable Style/RegexpLiteral
221
+ end
222
+ end
223
+ end
@@ -0,0 +1,74 @@
1
+ require 'English'
2
+ require 'gs1/syntax_dictionary/specification_parser'
3
+
4
+ module GS1
5
+ module SyntaxDictionary
6
+ # Parses the specification column.
7
+ class SpecificationParser
8
+ def initialize(specification_data)
9
+ @data = specification_data
10
+ end
11
+
12
+ # @return [Range<Integer>] returns the length
13
+ def parse
14
+ lengths = components
15
+ .map { parse_component(_1) }
16
+ .reduce(Length.new(0, 0), :+)
17
+
18
+ lengths.min..lengths.max
19
+ end
20
+
21
+ private
22
+
23
+ class Length
24
+ attr_reader :min, :max
25
+
26
+ def initialize(min, max)
27
+ @min = min
28
+ @max = max
29
+ end
30
+
31
+ def +(other) = Length.new(other.calculate_min(min), max + other.max)
32
+
33
+ def calculate_min(other_min) = other_min + min
34
+ end
35
+
36
+ private_constant :Length
37
+
38
+ class OptionalLength < Length
39
+ def calculate_min(other_min) = other_min
40
+ end
41
+
42
+ private_constant :OptionalLength
43
+
44
+ attr_reader :data
45
+
46
+ def components = @components ||= data.split
47
+
48
+ # rubocop:disable Metrics/MethodLength
49
+ def parse_component(component)
50
+ types = 'NXYZ'
51
+ fixed = /^[#{types}](?<length>\d+)/
52
+ fixed_optional = /^\[[#{types}](?<length>\d+)\]/
53
+ variable = /^[#{types}]\.\.(?<length>\d+)/
54
+ variable_optional = /^\[[#{types}]\.\.(?<length>\d+)\]/
55
+
56
+ case component
57
+ when fixed
58
+ length = $LAST_MATCH_INFO[:length].to_i
59
+ Length.new(length, length)
60
+ when fixed_optional
61
+ length = $LAST_MATCH_INFO[:length].to_i
62
+ OptionalLength.new(length, length)
63
+ when variable
64
+ Length.new(1, $LAST_MATCH_INFO[:length].to_i)
65
+ when variable_optional
66
+ OptionalLength.new(1, $LAST_MATCH_INFO[:length].to_i)
67
+ else
68
+ raise "Invalid format for component of specification column: #{component}"
69
+ end
70
+ end
71
+ # rubocop:enable Metrics/MethodLength
72
+ end
73
+ end
74
+ end
data/lib/gs1/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module GS1
2
- VERSION = '1.1.0'.freeze
2
+ VERSION = '2.0.0'.freeze
3
3
  end
data/lib/gs1.rb CHANGED
@@ -1,21 +1,3 @@
1
- require 'gs1/version'
2
-
3
- require 'gs1/ai'
4
- require 'gs1/check_digit_calculator'
5
- require 'gs1/extensions'
6
- require 'gs1/definitions'
7
- require 'gs1/validations'
8
-
9
- require 'gs1/record'
10
- require 'gs1/batch'
11
- require 'gs1/content'
12
- require 'gs1/expiration_date'
13
- require 'gs1/gtin'
14
- require 'gs1/serial_number'
15
- require 'gs1/sscc'
16
-
17
- require 'gs1/barcode'
18
-
19
1
  # GS1 module.
20
2
  #
21
3
  module GS1
@@ -36,16 +18,27 @@ module GS1
36
18
  # Configuration holds custom configuration parameters.
37
19
  #
38
20
  class Configuration
39
- attr_accessor :company_prefix
21
+ attr_accessor :company_prefix, :ignore_extra_barcode_elements
40
22
  attr_writer :barcode_separator
41
23
 
24
+ def initialize
25
+ @ignore_extra_barcode_elements = true
26
+ end
27
+
42
28
  def barcode_separator
43
29
  @barcode_separator || GS1::Barcode::DEFAULT_SEPARATOR
44
30
  end
45
31
  end
46
32
 
47
- AI_CLASSES = GS1::Record.descendants.each_with_object({}) do |klass, hash|
48
- hash[klass.ai] = klass
33
+ def self.ai_classes
34
+ @ai_classes ||= begin
35
+ GeneratedAIClasses.ai_classes
36
+ # sort to get non-generated classes first
37
+ ai_classes = GS1::Record.descendants.sort_by { _1.generated ? 1 : 0 }
38
+ ai_classes.each_with_object({}) do |klass, hash|
39
+ hash[klass.ai] ||= klass
40
+ end
41
+ end
49
42
  end
50
43
 
51
44
  module AIDCMarketingLevels
@@ -54,3 +47,22 @@ module GS1
54
47
  HIGHEST = 3].freeze
55
48
  end
56
49
  end
50
+
51
+ require 'gs1/version'
52
+
53
+ require 'gs1/ai'
54
+ require 'gs1/check_digit_calculator'
55
+ require 'gs1/extensions'
56
+ require 'gs1/definitions'
57
+ require 'gs1/validations'
58
+
59
+ require 'gs1/record'
60
+ require 'gs1/generated_ai_classes'
61
+ require 'gs1/batch'
62
+ require 'gs1/content'
63
+ require 'gs1/expiration_date'
64
+ require 'gs1/gtin'
65
+ require 'gs1/serial_number'
66
+ require 'gs1/sscc'
67
+
68
+ require 'gs1/barcode'
metadata CHANGED
@@ -1,113 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gs1
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Åhman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-23 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: byebug
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '10.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '10.0'
41
- - !ruby/object:Gem::Dependency
42
- name: rake
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '10.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '10.0'
55
- - !ruby/object:Gem::Dependency
56
- name: rb-readline
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '0.1'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '0.1'
69
- - !ruby/object:Gem::Dependency
70
- name: rspec
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '3.0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '3.0'
83
- - !ruby/object:Gem::Dependency
84
- name: rubocop
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: 0.49.0
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: 0.49.0
97
- - !ruby/object:Gem::Dependency
98
- name: simplecov
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: '0.1'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: '0.1'
11
+ date: 2025-04-22 00:00:00.000000000 Z
12
+ dependencies: []
111
13
  description: GS1 defines a set of GS1 standards directly from the documentation
112
14
  email:
113
15
  - stefan.ahman@apoex.se
@@ -115,24 +17,17 @@ executables: []
115
17
  extensions: []
116
18
  extra_rdoc_files: []
117
19
  files:
118
- - ".gitignore"
119
- - ".rspec"
120
- - ".rubocop.yml"
121
- - ".ruby-gemset"
122
- - ".ruby-version"
123
- - ".travis.yml"
124
20
  - CHANGELOG.md
125
21
  - CODE_OF_CONDUCT.md
126
- - Gemfile
127
22
  - LICENSE.txt
128
23
  - README.md
129
- - Rakefile
130
- - bin/console
131
- - bin/setup
132
24
  - gs1.gemspec
133
25
  - lib/gs1.rb
134
26
  - lib/gs1/ai.rb
135
27
  - lib/gs1/barcode.rb
28
+ - lib/gs1/barcode/attribute_validator.rb
29
+ - lib/gs1/barcode/attribute_validators/ignoring_record_validator.rb
30
+ - lib/gs1/barcode/attribute_validators/record_validator.rb
136
31
  - lib/gs1/barcode/base.rb
137
32
  - lib/gs1/barcode/definitions.rb
138
33
  - lib/gs1/barcode/error.rb
@@ -149,10 +44,13 @@ files:
149
44
  - lib/gs1/extensions/date.rb
150
45
  - lib/gs1/extensions/date_month_based.rb
151
46
  - lib/gs1/extensions/gtin.rb
47
+ - lib/gs1/generated_ai_classes.rb
152
48
  - lib/gs1/gtin.rb
153
49
  - lib/gs1/record.rb
154
50
  - lib/gs1/serial_number.rb
155
51
  - lib/gs1/sscc.rb
52
+ - lib/gs1/syntax_dictionary/parser.rb
53
+ - lib/gs1/syntax_dictionary/specification_parser.rb
156
54
  - lib/gs1/validations.rb
157
55
  - lib/gs1/validations/check_digit_validation.rb
158
56
  - lib/gs1/validations/date_validation.rb
@@ -163,6 +61,7 @@ licenses:
163
61
  - MIT
164
62
  metadata:
165
63
  allowed_push_host: https://rubygems.org
64
+ rubygems_mfa_required: 'true'
166
65
  post_install_message:
167
66
  rdoc_options: []
168
67
  require_paths:
@@ -171,14 +70,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
171
70
  requirements:
172
71
  - - ">="
173
72
  - !ruby/object:Gem::Version
174
- version: 2.3.0
73
+ version: 3.0.5
175
74
  required_rubygems_version: !ruby/object:Gem::Requirement
176
75
  requirements:
177
76
  - - ">="
178
77
  - !ruby/object:Gem::Version
179
78
  version: '0'
180
79
  requirements: []
181
- rubygems_version: 3.1.2
80
+ rubygems_version: 3.2.33
182
81
  signing_key:
183
82
  specification_version: 4
184
83
  summary: A ruby gem for implementing GS1 standards
data/.gitignore DELETED
@@ -1,13 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
-
10
- Gemfile.lock
11
-
12
- # rspec failure tracking
13
- .rspec_status
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.rubocop.yml DELETED
@@ -1,13 +0,0 @@
1
- AllCops:
2
- TargetRubyVersion: 2.3
3
-
4
- Metrics/BlockLength:
5
- Exclude:
6
- - 'spec/**/*'
7
- - 'gs1.gemspec'
8
-
9
- Metrics/LineLength:
10
- Max: 120
11
-
12
- Style/FrozenStringLiteralComment:
13
- Enabled: false
data/.ruby-gemset DELETED
@@ -1 +0,0 @@
1
- gs1
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.7.0
data/.travis.yml DELETED
@@ -1,18 +0,0 @@
1
- ---
2
- env:
3
- global:
4
- secure: lQbZqhDx1dIuRA26K9vxaHWCwquTuJe4fuG9NEnJK1XnHWZge12NmFbGKrYrGEXsvzVRG6u4L6+MLudGT6nr1NOfOWHpA3FD73RcbBpuSrjrLGrnk1Z2OgBWQDbAMD8OszH3ciRQuqO780JZcTD3L6RULPJ6xHPcXI3UpCcQC78ZjuBjNGVPg6qBKmB6HeErj/THWiz4R4ouSo6/p4MsVS3ELqlukw1m6bx0wyyxI6sF33i5zC6V0dn7TUNrRDljNjO/bHfhYwRfabrTl0q4MzGC4rqS+CC8JXRMlCl7AHTKFeiRvSeI+Co8zidfGBCsocFAT4jshXC+NTddhtTnVbQ3GmSt6BGdfnprwbg8/GfsFE14L7IBlUDH1hzpuJYTsCQzR6+8UE8LA76z48Ttuv4of5TLAhDCCtKhRleVi9lbVuTT2xqlCTMwMWY3iH535L0Ou584BEYG7eRcdZS3rA96JWAlmuhmSVd8YBnXwhJC74eS5dJsb0qYSvCsUzfBUn7RbeWpCelnNRV4eX8n8Gn46DFSB414tAjCUL7rvIeSnouuI15lYaaqeORdGbhECnNoryxOsfPqp/zhpiT6dQW4LnF4WgMErzbyyZcVydFAB454XtM40kTEqLJ0S6bPfoYycR1yD/v4YjoH1nHB89om/zFVpB0VJ9BltMbAZJ8=
5
- sudo: false
6
- language: ruby
7
- cache: bundler
8
- rvm:
9
- - 2.5.1
10
- - 2.7.0
11
- before_script:
12
- - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
13
- - chmod +x ./cc-test-reporter
14
- - ./cc-test-reporter before-build
15
- script:
16
- - bundle exec rake
17
- after_script:
18
- - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in gs1.gemspec
6
- gemspec
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require 'bundler/gem_tasks'
2
- require 'rspec/core/rake_task'
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task default: :spec
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'bundler/setup'
4
- require 'gs1'
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require 'pry'
11
- # Pry.start
12
-
13
- require 'irb'
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here