pubid-nist 0.3.1 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6d4329248de2fde990db7f396796b104fc7152a4d3cf19c9b1fba9817bd9bc59
4
- data.tar.gz: 1f1368050820296d410019b9bb66d980de01d166ba62ec9572b9f048c1fde931
3
+ metadata.gz: 05fde56b36a4e9feb10d123af900a79ea1315f01ef829ce4ae87dd7efb504ad4
4
+ data.tar.gz: 6fbaa5126e3b6cfda4bc1ddbfc3b5a853605b1062f195431511a3bcea2cf3769
5
5
  SHA512:
6
- metadata.gz: 0d711d29e07f10c5f69307dcfb0267a4a480c12a715d75901d083362fb123ff5a7074d45d9d37f83cea5efc12ae8a5aa6e2c8d47f529c2b78858dba4e3e7fb23
7
- data.tar.gz: 50b16da1bb63033efe8c09d37f2f9687e79d2efc73a0b7b66a61637e1fad7d414a71b6a182b4710556ab9f332b6e9670d1af10d82940a170f30e12b1fc8f04d2
6
+ metadata.gz: ddc3f7b8dcb4e793209a77c5afe3344407a6c7b287f24148a148f888728139966fa6626fa343a634a020ad891d119d05a4e45993a9c2ace9786b55e09ef968f9
7
+ data.tar.gz: 7e99c47a000e6c077ef3a1cb3da17ea01479bbab3ea56bf17c294bff155c0d975b603b7c2b58887446bf838a1c6176b29ebdd1144bb9ebe575685effbd6e1629
@@ -0,0 +1,23 @@
1
+
2
+ module Pubid::Nist
3
+ module Identifier
4
+ class Addendum < Base
5
+ def_delegators 'Pubid::Nist::Identifier::Addendum', :type
6
+
7
+ attr_accessor :base
8
+
9
+ def initialize(base:, number: nil)
10
+ @number = number
11
+ @base = base
12
+ end
13
+
14
+ def self.type
15
+ { key: :add, title: "Addendum" }
16
+ end
17
+
18
+ def self.get_renderer_class
19
+ Renderer::Addendum
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "forwardable"
5
+
6
+ UPDATE_CODES = YAML.load_file(File.join(File.dirname(__FILE__), "../../../../update_codes.yaml"))
7
+
8
+ module Pubid::Nist
9
+ module Identifier
10
+ class Base < Pubid::Core::Identifier::Base
11
+ extend Forwardable
12
+ attr_accessor :series, :code, :revision, :publisher, :version, :volume,
13
+ :part, :addendum, :stage, :translation,
14
+ :edition, :supplement, :update,
15
+ :section, :appendix, :errata, :index, :insert
16
+
17
+ def initialize(publisher: "NIST", series:, number: nil, stage: nil, supplement: nil,
18
+ edition_month: nil, edition_year: nil, edition_day: nil, update: nil,
19
+ edition: nil, **opts)
20
+ @publisher = publisher.is_a?(Publisher) ? publisher : Publisher.new(publisher: publisher.to_s)
21
+ @series = series.is_a?(Series) ? series : Series.new(series: series)
22
+ @code = number
23
+ @stage = Stage.new(**stage) if stage
24
+ @supplement = (supplement.is_a?(Array) && "") || supplement
25
+ if edition_month || edition_year
26
+ @edition = parse_edition(edition_month, edition_year, edition_day)
27
+ elsif edition
28
+ @edition = Edition.new(number: edition)
29
+ end
30
+ @update = update
31
+ opts.each { |key, value| send("#{key}=", value.to_s) }
32
+ end
33
+
34
+ def parse_edition(edition_month, edition_year, edition_day)
35
+ if edition_month
36
+ date = Date.parse("#{edition_day || '01'}/#{edition_month}/#{edition_year}")
37
+ if edition_day
38
+ Edition.new(month: date.month, year: date.year, day: date.day)
39
+ else
40
+ Edition.new(month: date.month, year: date.year)
41
+ end
42
+ else
43
+ Edition.new(year: edition_year.to_i)
44
+ end
45
+ end
46
+
47
+ # returns weight based on amount of defined attributes
48
+ def weight
49
+ instance_variables.inject(0) do |sum, var|
50
+ sum + (instance_variable_get(var).nil? ? 0 : 1)
51
+ end
52
+ end
53
+
54
+ def ==(other)
55
+ other.instance_variables.each do |var|
56
+ return false if instance_variable_get(var) != other.instance_variable_get(var)
57
+ end
58
+ true
59
+ end
60
+
61
+ def merge(document)
62
+ document.instance_variables.each do |var|
63
+ val = document.instance_variable_get(var)
64
+ current_val = instance_variable_get(var)
65
+ if [:@series, :@publisher].include?(var) ||
66
+ (val && current_val.nil?) ||
67
+ (val && current_val.to_s.length < val.to_s.length)
68
+ instance_variable_set(var, val)
69
+ end
70
+ end
71
+
72
+ self
73
+ end
74
+
75
+ def self.update_old_code(code)
76
+ UPDATE_CODES.each do |from, to|
77
+ code = code.gsub(from.match?(/^\/.*\/$/) ? Regexp.new(from[1..-2]) : from, to)
78
+ end
79
+ code
80
+ end
81
+
82
+ # @param without_edition [Boolean] render pubid without rendering edition
83
+ def to_s(format = :short, without_edition: false)
84
+ self.class.get_renderer_class.new(to_h(deep: false)).render(format: format, without_edition: without_edition)
85
+ end
86
+
87
+ def to_json(*args)
88
+ result = {
89
+ styles: {
90
+ short: to_s(:short),
91
+ abbrev: to_s(:abbrev),
92
+ long: to_s(:long),
93
+ mr: to_s(:mr),
94
+ }
95
+ }
96
+
97
+ instance_variables.each do |var|
98
+ val = instance_variable_get(var)
99
+ result[var.to_s.gsub('@', '')] = val unless val.nil?
100
+ end
101
+ result.to_json(*args)
102
+ end
103
+
104
+ class << self
105
+ def create(**opts)
106
+ new(**opts)
107
+ end
108
+
109
+ def transform(params)
110
+ # run transform through each element,
111
+ # like running transformer.apply(number: 1) and transformer.apply(year: 1999)
112
+ # instead of running transformer on whole hash, like running transformer.apply({ number: 1, year: 1999 })
113
+ # where rule for number or year only will be not applied
114
+ # transformation only applied to rules matching the whole hash
115
+
116
+ identifier_params = params.map do |k, v|
117
+ get_transformer_class.new.apply({ k => v }, params)
118
+ end.inject({}, :merge)
119
+
120
+ if identifier_params[:addendum]
121
+ return Addendum.new(base: new(
122
+ **identifier_params.dup.tap { |h| h.delete(:addendum) }
123
+ ), **identifier_params[:addendum])
124
+ end
125
+
126
+ new(**identifier_params)
127
+ end
128
+
129
+ def get_parser_class
130
+ Parser
131
+ end
132
+
133
+ def get_transformer_class
134
+ Transformer
135
+ end
136
+
137
+ def get_renderer_class
138
+ Renderer::Base
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -1,140 +1,10 @@
1
- # frozen_string_literal: true
2
-
3
- require "json"
4
- require "forwardable"
5
-
6
- UPDATE_CODES = YAML.load_file(File.join(File.dirname(__FILE__), "../../../update_codes.yaml"))
7
-
8
1
  module Pubid::Nist
9
- class Identifier < Pubid::Core::Identifier::Base
10
- extend Forwardable
11
- attr_accessor :series, :code, :revision, :publisher, :version, :volume,
12
- :part, :addendum, :stage, :translation,
13
- :edition, :supplement, :update,
14
- :section, :appendix, :errata, :index, :insert
15
-
16
- def initialize(publisher: "NIST", series:, number: nil, stage: nil, supplement: nil,
17
- edition_month: nil, edition_year: nil, edition_day: nil, update: nil,
18
- edition: nil, **opts)
19
- @publisher = publisher.is_a?(Publisher) ? publisher : Publisher.new(publisher: publisher.to_s)
20
- @series = series.is_a?(Series) ? series : Series.new(series: series)
21
- @code = number
22
- @stage = Stage.new(**stage) if stage
23
- @supplement = (supplement.is_a?(Array) && "") || supplement
24
- if edition_month || edition_year
25
- @edition = parse_edition(edition_month, edition_year, edition_day)
26
- elsif edition
27
- @edition = Edition.new(number: edition)
28
- end
29
- @update = update
30
- opts.each { |key, value| send("#{key}=", value.to_s) }
31
- end
32
-
33
- def parse_edition(edition_month, edition_year, edition_day)
34
- if edition_month
35
- date = Date.parse("#{edition_day || '01'}/#{edition_month}/#{edition_year}")
36
- if edition_day
37
- Edition.new(month: date.month, year: date.year, day: date.day)
38
- else
39
- Edition.new(month: date.month, year: date.year)
40
- end
41
- else
42
- Edition.new(year: edition_year.to_i)
43
- end
44
- end
45
-
46
- # returns weight based on amount of defined attributes
47
- def weight
48
- instance_variables.inject(0) do |sum, var|
49
- sum + (instance_variable_get(var).nil? ? 0 : 1)
50
- end
51
- end
52
-
53
- def ==(other)
54
- other.instance_variables.each do |var|
55
- return false if instance_variable_get(var) != other.instance_variable_get(var)
56
- end
57
- true
58
- end
59
-
60
- def merge(document)
61
- document.instance_variables.each do |var|
62
- val = document.instance_variable_get(var)
63
- current_val = instance_variable_get(var)
64
- if [:@series, :@publisher].include?(var) ||
65
- (val && current_val.nil?) ||
66
- (val && current_val.to_s.length < val.to_s.length)
67
- instance_variable_set(var, val)
68
- end
69
- end
70
-
71
- self
72
- end
73
-
74
- def self.update_old_code(code)
75
- UPDATE_CODES.each do |from, to|
76
- code = code.gsub(from.match?(/^\/.*\/$/) ? Regexp.new(from[1..-2]) : from, to)
77
- end
78
- code
79
- end
80
-
81
- # @param without_edition [Boolean] render pubid without rendering edition
82
- def to_s(format = :short, without_edition: false)
83
- self.class.get_renderer_class.new(to_h(deep: false)).render(format: format, without_edition: without_edition)
84
- end
85
-
86
- def to_json(*args)
87
- result = {
88
- styles: {
89
- short: to_s(:short),
90
- abbrev: to_s(:abbrev),
91
- long: to_s(:long),
92
- mr: to_s(:mr),
93
- }
94
- }
95
-
96
- instance_variables.each do |var|
97
- val = instance_variable_get(var)
98
- result[var.to_s.gsub('@', '')] = val unless val.nil?
99
- end
100
- result.to_json(*args)
101
- end
102
-
2
+ module Identifier
103
3
  class << self
104
- def create(**opts)
105
- new(**opts)
106
- end
107
-
108
- def transform(params)
109
- # run transform through each element,
110
- # like running transformer.apply(number: 1) and transformer.apply(year: 1999)
111
- # instead of running transformer on whole hash, like running transformer.apply({ number: 1, year: 1999 })
112
- # where rule for number or year only will be not applied
113
- # transformation only applied to rules matching the whole hash
114
-
115
- identifier_params = params.map do |k, v|
116
- get_transformer_class.new.apply({ k => v }, params)
117
- end.inject({}, :merge)
118
-
119
- if identifier_params[:addendum]
120
- return Addendum.new(base: new(
121
- **identifier_params.dup.tap { |h| h.delete(:addendum) }
122
- ), **identifier_params[:addendum])
123
- end
124
-
125
- new(**identifier_params)
126
- end
127
-
128
- def get_parser_class
129
- Parser
130
- end
131
-
132
- def get_transformer_class
133
- Transformer
134
- end
4
+ include Pubid::Core::Identifier
135
5
 
136
- def get_renderer_class
137
- Renderer::Base
6
+ def parse(*args)
7
+ Base.parse(*args)
138
8
  end
139
9
  end
140
10
  end
@@ -14,9 +14,9 @@ module Pubid::Nist
14
14
  "%{supplement}%{section}%{appendix}%{errata}%{index}%{insert}%{update}"\
15
15
  "%{stage}%{translation}" % params
16
16
  else
17
- "%{series}%{code}%{stage}%{volume}%{part}%{edition}%{revision}%{version}"\
17
+ "%{series}%{code}%{stage}%{volume}%{part}%{revision}%{version}"\
18
18
  "%{supplement}%{section}%{appendix}%{errata}%{index}%{insert}%{update}"\
19
- "%{translation}" % params
19
+ "%{edition}%{translation}" % params
20
20
  end
21
21
  end
22
22
 
@@ -1,5 +1,5 @@
1
1
  module Pubid
2
2
  module Nist
3
- VERSION = "0.3.1".freeze
3
+ VERSION = "0.4.1".freeze
4
4
  end
5
5
  end
data/lib/pubid/nist.rb CHANGED
@@ -10,6 +10,7 @@ module Pubid
10
10
  end
11
11
  end
12
12
 
13
+ require_relative "nist/identifier"
13
14
  require_relative "nist/series"
14
15
  require_relative "nist/parsers/default"
15
16
  require_relative "nist/update"
@@ -29,10 +30,16 @@ end.map do |parser_class|
29
30
  [parser.name.split("::").last.gsub(/(.)([A-Z])/, '\1 \2').upcase, parser]
30
31
  end.to_h
31
32
 
32
- require_relative "nist/identifier"
33
+ require_relative "nist/identifier/base"
33
34
  require_relative "nist/publisher"
34
35
  require_relative "nist/stage"
35
36
  require_relative "nist/errors"
36
37
  require_relative "nist/nist_tech_pubs"
37
38
  require_relative "nist/edition"
38
- require_relative "nist/addendum"
39
+ require_relative "nist/identifier/addendum"
40
+
41
+ config = Pubid::Core::Configuration.new
42
+ config.stages = YAML.load_file(File.join(File.dirname(__FILE__), "../../stages.yaml"))
43
+ config.default_type = Pubid::Nist::Identifier::Base
44
+ config.types = [Pubid::Nist::Identifier::Addendum]
45
+ Pubid::Nist::Identifier.set_config(config)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pubid-nist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-12 00:00:00.000000000 Z
11
+ date: 2024-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -165,10 +165,11 @@ files:
165
165
  - exe/pubid-nist
166
166
  - lib/pubid-nist.rb
167
167
  - lib/pubid/nist.rb
168
- - lib/pubid/nist/addendum.rb
169
168
  - lib/pubid/nist/edition.rb
170
169
  - lib/pubid/nist/errors.rb
171
170
  - lib/pubid/nist/identifier.rb
171
+ - lib/pubid/nist/identifier/addendum.rb
172
+ - lib/pubid/nist/identifier/base.rb
172
173
  - lib/pubid/nist/nist_tech_pubs.rb
173
174
  - lib/pubid/nist/parser.rb
174
175
  - lib/pubid/nist/parsers/circ.rb
@@ -1,21 +0,0 @@
1
-
2
- module Pubid::Nist
3
- class Addendum < Identifier
4
- def_delegators 'Pubid::Nist::Addendum', :type
5
-
6
- attr_accessor :base
7
-
8
- def initialize(base:, number: nil)
9
- @number = number
10
- @base = base
11
- end
12
-
13
- def self.type
14
- { key: :add, title: "Addendum" }
15
- end
16
-
17
- def self.get_renderer_class
18
- Renderer::Addendum
19
- end
20
- end
21
- end