Class: Relaton::Bipm::Id
- Inherits:
-
Object
- Object
- Relaton::Bipm::Id
- Defined in:
- lib/relaton/bipm/id_parser.rb
Constant Summary collapse
- TYPES =
root :result end
{ "Resolution" => "RES", "Résolution" => "RES", "Recommendation" => "REC", "Recommandation" => "REC", "Decision" => "DECN", "Décision" => "DECN", "Declaration" => "DECL", "Déclaration" => "DECL", "Réunion" => "Meeting", "Action" => "ACT", }.freeze
Instance Attribute Summary collapse
-
#id ⇒ Hash
The parsed id components.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compare two Id objects.
- #group ⇒ Object
-
#initialize ⇒ Id
constructor
Create a new Id object.
- #lang ⇒ Object
-
#normalize_hash(src) ⇒ Hash
Normalize ID parts Traslate type into abbreviation, remove leading zeros from number.
-
#normalized_number(src) ⇒ String?
Remove leading zeros from number.
-
#normalized_type(src) ⇒ String
Translate type into abbreviation.
- #num_and_year ⇒ Object
- #number ⇒ Object
- #parse(id) ⇒ Object
- #parse_brochure(id) ⇒ Object
- #parse_brochure_other(id) ⇒ Object
- #parse_group_num(id) ⇒ Object
- #parse_group_type(id) ⇒ Object
- #parse_jcgm(id) ⇒ Object
- #parse_metrologia(id) ⇒ Object
- #parse_outcome(id) ⇒ Object
- #parse_si_brochure(id) ⇒ Object
-
#parse_si_brochure_en(id) ⇒ Object
English form.
-
#parse_si_brochure_fr(id) ⇒ Object
French form.
- #parse_type_group(id) ⇒ Object
-
#to_hash ⇒ Hash
Transform ID parts.
- #type ⇒ Object
- #year ⇒ Object
- #year_lang ⇒ Object
Constructor Details
#initialize ⇒ Id
Create a new Id object
82 83 84 85 86 87 88 89 |
# File 'lib/relaton/bipm/id_parser.rb', line 82 def initialize # @id = Parser.new.parse(id) # @id = parse(id) # rescue Parslet::ParseFailed => e # Util.warn "WARNING: Incorrect reference: `#{id}`" # warn e.parse_failure_cause.ascii_tree # raise RelatonBib::RequestError, e end |
Instance Attribute Details
#id ⇒ Hash
Returns the parsed id components.
77 78 79 |
# File 'lib/relaton/bipm/id_parser.rb', line 77 def id @id end |
Instance Method Details
#==(other) ⇒ Boolean
Compare two Id objects
210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/relaton/bipm/id_parser.rb', line 210 def ==(other) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/AbcSize other_hash = other.is_a?(Id) ? other.to_hash : normalize_hash(other) hash = to_hash.dup hash.delete(:number) if other_hash[:number].nil? && hash[:number] == "1" && hash[:year] other_hash.delete(:number) if hash[:number].nil? && other_hash[:number] == "1" && other_hash[:year] # hash.delete(:year) unless other_hash[:year] other_hash.delete(:year) unless hash[:year] hash.delete(:lang) unless other_hash[:lang] other_hash.delete(:lang) unless hash[:lang] hash.delete(:part) unless other_hash[:part] hash.delete(:append) unless other_hash[:append] hash == other_hash end |
#group ⇒ Object
119 120 121 |
# File 'lib/relaton/bipm/id_parser.rb', line 119 def group "(?<group>CGPM|CIPM(?:\\sMRA|[A-Z-])?|CC[a-zA-Z-]+[IVX]*|JC[a-zA-Z-]+|CEC|WG-MS)" end |
#lang ⇒ Object
126 |
# File 'lib/relaton/bipm/id_parser.rb', line 126 def lang; ",\\s?(?<lang>[A-Z]{1,2})"; end |
#normalize_hash(src) ⇒ Hash
Normalize ID parts Traslate type into abbreviation, remove leading zeros from number
242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/relaton/bipm/id_parser.rb', line 242 def normalize_hash(src) # rubocop:disable Metrics/AbcSize hash = { group: src[:group].to_s.sub("CCDS", "CCTF") } hash[:type] = normalized_type(src) if src[:type] norm_num = normalized_number(src) hash[:number] = norm_num unless norm_num.nil? || norm_num.empty? hash[:year] = src[:year].to_s if src[:year] hash[:corr] = true if src[:corr] hash[:part] = src[:part].to_s if src[:part] hash[:append] = src[:append].to_s if src[:append] hash[:lang] = src[:lang].to_s if src[:lang] hash end |
#normalized_number(src) ⇒ String?
Remove leading zeros from number
270 271 272 273 274 |
# File 'lib/relaton/bipm/id_parser.rb', line 270 def normalized_number(src) return unless src[:number] src[:number].to_s.sub(/^0+/, "") end |
#normalized_type(src) ⇒ String
Translate type into abbreviation
260 261 262 263 |
# File 'lib/relaton/bipm/id_parser.rb', line 260 def normalized_type(src) type = TYPES[src[:type].to_s.capitalize] || src[:type].to_s type == type.upcase ? type : type.capitalize end |
#num_and_year ⇒ Object
128 |
# File 'lib/relaton/bipm/id_parser.rb', line 128 def num_and_year; "(?:(?:#{number}\\s)?#{year_lang}|#{year}-#{number}|#{number})"; end |
#number ⇒ Object
124 |
# File 'lib/relaton/bipm/id_parser.rb', line 124 def number; "(?<number>[A-Z0-9-]+)"; end |
#parse(id) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/relaton/bipm/id_parser.rb', line 93 def parse(id) # str = StringScanner.new id match = parse_outcome(id) || parse_brochure(id) || parse_metrologia(id) || parse_jcgm(id) @id = match.named_captures.compact.transform_keys(&:to_sym) self rescue StandardError => e Util.warn "Incorrect reference: `#{id}`" raise Relaton::RequestError, e end |
#parse_brochure(id) ⇒ Object
130 131 132 |
# File 'lib/relaton/bipm/id_parser.rb', line 130 def parse_brochure(id) parse_si_brochure(id) || parse_brochure_other(id) end |
#parse_brochure_other(id) ⇒ Object
162 163 164 |
# File 'lib/relaton/bipm/id_parser.rb', line 162 def parse_brochure_other(id) %r{^(?<group>CCEM|CCL|CCM|SI|Rapport)[-\s](?<type>GD-RSI|GD-MeP|MEP|BIPM)[-\s](?<number>\w+|\d{4}\/\d{2})$}.match(id) end |
#parse_group_num(id) ⇒ Object
107 108 109 |
# File 'lib/relaton/bipm/id_parser.rb', line 107 def parse_group_num(id) %r{^#{group}\s#{number}[a-z]{1,2}\s#{type}\s#{year_lang}$}.match(id) end |
#parse_group_type(id) ⇒ Object
111 112 113 |
# File 'lib/relaton/bipm/id_parser.rb', line 111 def parse_group_type(id) %r{^#{group}\s(?:--\s)?#{type}\s#{num_and_year}$}.match(id) end |
#parse_jcgm(id) ⇒ Object
170 171 172 |
# File 'lib/relaton/bipm/id_parser.rb', line 170 def parse_jcgm(id) %r{^#{group}\s#{number}(?::#{year})?(?:\s(?<corr>Corrigendum))?$}.match(id) end |
#parse_metrologia(id) ⇒ Object
166 167 168 |
# File 'lib/relaton/bipm/id_parser.rb', line 166 def parse_metrologia(id) %r{^(?<group>Metrologia)(?:\s(?<number>[a-zA-Z0-9\s]+))?$}.match(id) end |
#parse_outcome(id) ⇒ Object
103 104 105 |
# File 'lib/relaton/bipm/id_parser.rb', line 103 def parse_outcome(id) parse_group_num(id) || parse_group_type(id) || parse_type_group(id) end |
#parse_si_brochure(id) ⇒ Object
134 135 136 |
# File 'lib/relaton/bipm/id_parser.rb', line 134 def parse_si_brochure(id) parse_si_brochure_en(id) || parse_si_brochure_fr(id) end |
#parse_si_brochure_en(id) ⇒ Object
English form. Accepts the bare/sectioned forms (“SI Brochure”, “SI Brochure, Part 1”, “SI Brochure Concise” …) and the edition-tagged docnumber emitted by SI Brochure 9e v3.01:
"SI Brochure 9e v3.01 (2019/2024, E)"
Edition/version/year/lang are matched but not captured so the index key collapses to type as the prior collection-render flow.
144 145 146 147 148 149 150 |
# File 'lib/relaton/bipm/id_parser.rb', line 144 def parse_si_brochure_en(id) %r{^ (?<group>SI)\s(?<type>Brochure) (?:,?\s(?:(?:Part|Partie)\s(?<part>\d+)|(?:Appendix|Annexe)\s(?<append>\d+)|(?<number>Concise|FAQ)))? (?:\s\d+e\sv[\d.]+\s\([\d/]+(?:,\s*[A-Z])?\))? $}x.match(id) end |
#parse_si_brochure_fr(id) ⇒ Object
French form. The SI Brochure 9e v3.01 French bibdata emits its docnumber as “Brochure sur le SI 9e v3.01 (2019/2024, F)”. Same document as the English form — collapse to the same type.
155 156 157 158 159 160 |
# File 'lib/relaton/bipm/id_parser.rb', line 155 def parse_si_brochure_fr(id) %r{^ (?<type>Brochure)\ssur\sle\s(?<group>SI) (?:\s\d+e\sv[\d.]+\s\([\d/]+(?:,\s*[A-Z])?\))? $}x.match(id) end |
#parse_type_group(id) ⇒ Object
115 116 117 |
# File 'lib/relaton/bipm/id_parser.rb', line 115 def parse_type_group(id) %r{^#{type}\s#{group}\/#{num_and_year}$}.match(id) end |
#to_hash ⇒ Hash
Transform ID parts. Traslate type into abbreviation, remove leading zeros from number
230 231 232 |
# File 'lib/relaton/bipm/id_parser.rb', line 230 def to_hash @to_hash ||= normalize_hash id end |
#type ⇒ Object
123 |
# File 'lib/relaton/bipm/id_parser.rb', line 123 def type; "(?<type>[[:alpha:]]+)"; end |
#year ⇒ Object
125 |
# File 'lib/relaton/bipm/id_parser.rb', line 125 def year; "(?<year>\\d{4})"; end |
#year_lang ⇒ Object
127 |
# File 'lib/relaton/bipm/id_parser.rb', line 127 def year_lang; "\\(#{year}(?:#{lang})?\\)"; end |