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_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
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/relaton/bipm/id_parser.rb', line 189 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 == 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
219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/relaton/bipm/id_parser.rb', line 219 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
247 248 249 250 251 |
# File 'lib/relaton/bipm/id_parser.rb', line 247 def normalized_number(src) return unless src[:number] src[:number].to_s.sub(/^0+/, "") end |
#normalized_type(src) ⇒ String
Translate type into abbreviation
237 238 239 240 |
# File 'lib/relaton/bipm/id_parser.rb', line 237 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
141 142 143 |
# File 'lib/relaton/bipm/id_parser.rb', line 141 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
149 150 151 |
# File 'lib/relaton/bipm/id_parser.rb', line 149 def parse_jcgm(id) %r{^#{group}\s#{number}(?::#{year})?(?:\s(?<corr>Corrigendum))?$}.match(id) end |
#parse_metrologia(id) ⇒ Object
145 146 147 |
# File 'lib/relaton/bipm/id_parser.rb', line 145 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 137 138 139 |
# File 'lib/relaton/bipm/id_parser.rb', line 134 def parse_si_brochure(id) %r{^ (?<group>SI)\s(?<type>Brochure) (?:,?\s(?:(?:Part|Partie)\s(?<part>\d+)|(?:Appendix|Annexe)\s(?<append>\d+)|(?<number>Concise|FAQ)))? $}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
207 208 209 |
# File 'lib/relaton/bipm/id_parser.rb', line 207 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 |