Class: RelatonBipm::Id
- Inherits:
-
Object
- Object
- RelatonBipm::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_group_num(id) ⇒ Object
- #parse_group_type(id) ⇒ Object
- #parse_jcgm(id) ⇒ Object
- #parse_metrologia(id) ⇒ Object
- #parse_outcome(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
81 82 83 84 85 86 87 88 |
# File 'lib/relaton_bipm/id_parser.rb', line 81 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.
76 77 78 |
# File 'lib/relaton_bipm/id_parser.rb', line 76 def id @id end |
Instance Method Details
#==(other) ⇒ Boolean
Compare two Id objects
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/relaton_bipm/id_parser.rb', line 180 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
118 119 120 |
# File 'lib/relaton_bipm/id_parser.rb', line 118 def group "(?<group>CGPM|CIPM(?:\\sMRA|[A-Z-])?|CC[a-zA-Z-]+[IVX]*|JC[a-zA-Z-]+|CEC|WG-MS)" end |
#lang ⇒ Object
125 |
# File 'lib/relaton_bipm/id_parser.rb', line 125 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
210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/relaton_bipm/id_parser.rb', line 210 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
238 239 240 241 242 |
# File 'lib/relaton_bipm/id_parser.rb', line 238 def normalized_number(src) return unless src[:number] src[:number].to_s.sub(/^0+/, "") end |
#normalized_type(src) ⇒ String
Translate type into abbreviation
228 229 230 231 |
# File 'lib/relaton_bipm/id_parser.rb', line 228 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
127 |
# File 'lib/relaton_bipm/id_parser.rb', line 127 def num_and_year; "(?:(?:#{number}\\s)?#{year_lang}|#{year}-#{number}|#{number})"; end |
#number ⇒ Object
123 |
# File 'lib/relaton_bipm/id_parser.rb', line 123 def number; "(?<number>[A-Z0-9-]+)"; end |
#parse(id) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/relaton_bipm/id_parser.rb', line 92 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 RelatonBib::RequestError, e end |
#parse_brochure(id) ⇒ Object
129 130 131 132 133 134 |
# File 'lib/relaton_bipm/id_parser.rb', line 129 def parse_brochure(id) %r{^ (?<group>SI)\s(?<type>Brochure) (?:,?\s(?:(?:Part|Partie)\s(?<part>\d+)|(?:Appendix|Annexe)\s(?<append>\d+)))? $}x.match(id) end |
#parse_group_num(id) ⇒ Object
106 107 108 |
# File 'lib/relaton_bipm/id_parser.rb', line 106 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
110 111 112 |
# File 'lib/relaton_bipm/id_parser.rb', line 110 def parse_group_type(id) %r{^#{group}\s(?:--\s)?#{type}\s#{num_and_year}$}.match(id) end |
#parse_jcgm(id) ⇒ Object
140 141 142 |
# File 'lib/relaton_bipm/id_parser.rb', line 140 def parse_jcgm(id) %r{^#{group}\s#{number}(?::#{year})?(?:\s(?<corr>Corrigendum))?$}.match(id) end |
#parse_metrologia(id) ⇒ Object
136 137 138 |
# File 'lib/relaton_bipm/id_parser.rb', line 136 def parse_metrologia(id) %r{^(?<group>Metrologia)(?:\s(?<number>[a-zA-Z0-9\s]+))?$}.match(id) end |
#parse_outcome(id) ⇒ Object
102 103 104 |
# File 'lib/relaton_bipm/id_parser.rb', line 102 def parse_outcome(id) parse_group_num(id) || parse_group_type(id) || parse_type_group(id) end |
#parse_type_group(id) ⇒ Object
114 115 116 |
# File 'lib/relaton_bipm/id_parser.rb', line 114 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
198 199 200 |
# File 'lib/relaton_bipm/id_parser.rb', line 198 def to_hash @to_hash ||= normalize_hash id end |
#type ⇒ Object
122 |
# File 'lib/relaton_bipm/id_parser.rb', line 122 def type; "(?<type>[[:alpha:]]+)"; end |
#year ⇒ Object
124 |
# File 'lib/relaton_bipm/id_parser.rb', line 124 def year; "(?<year>\\d{4})"; end |
#year_lang ⇒ Object
126 |
# File 'lib/relaton_bipm/id_parser.rb', line 126 def year_lang; "\\(#{year}(?:#{lang})?\\)"; end |