Module: Pubid::Iso::ParserUrn

Included in:
Parser
Defined in:
lib/pubid/iso/parser_urn.rb

Constant Summary collapse

LANGUAGES =
%w[en fr ru other].freeze
SUPPLEMENTS =
%w[amd cor sup].freeze

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pubid/iso/parser_urn.rb', line 7

def self.included(base) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  base.class_eval do # rubocop:disable Metrics/BlockLength
    rule(:colon) { str(":") }

    rule(:urn_organization) do
      array_to_str(Parser::ORGANIZATIONS.map(&:downcase))
    end

    rule(:urn_publisher_copublisher) do
      urn_organization.as(:publisher) >> (dash >> urn_organization.as(:copublisher)).repeat >> colon
    end

    rule(:urn_type) do
      (array_to_str(Parser::TYPES.map(&:downcase)).as(:type) >> colon).maybe
    end

    rule(:urn_part) { (colon >> part).maybe }

    rule(:urn_iteration) do
      (str(".v") >> digits.as(:iteration)).maybe
    end

    rule(:stage_digits) do
      match('\d').repeat(2, 2) >> str(".") >> match('\d').repeat(2, 2)
    end

    rule(:urn_stage_prefix) do
      colon >> str("stage-")
    end

    rule(:urn_draft) do
      str("draft") | str("published") | stage_digits
    end

    rule(:urn_stage) do
      (urn_stage_prefix >> urn_draft.as(:stage) >> urn_iteration).maybe
    end

    rule(:urn_edition) do
      (colon >> (str("ed-") >> digits.as(:edition))).maybe
    end

    rule(:urn_typed_stage) do
      (urn_stage_prefix >> urn_draft.as(:typed_stage)).maybe
    end

    rule(:urn_supplement) do
      (urn_typed_stage >>
        colon >> (array_to_str(SUPPLEMENTS).as(:type) >>
        (colon >> year_digits.as(:year)).maybe >>
        (colon >> ((digits.as(:number) >> str(":v1")) | str("v") >> digits.as(:number))).maybe)
      ).repeat(1).as(:supplements).maybe
    end

    rule(:urn_extract) do
      ((urn_stage_prefix >> urn_draft).maybe >>
        colon >> str("ext") >> colon >> year_digits.as(:year) >>
        (colon >> str("v") >> digits.as(:number))).as(:extract).maybe
    end

    rule(:languages) do
      array_to_str(LANGUAGES)
    end

    rule(:urn_language) do
      (colon >> (languages >> (str(",") >> languages).repeat).as(:language)).maybe
    end

    rule(:urn_all_parts) do
      (colon >> str("ser").as(:all_parts)).maybe
    end

    rule(:urn_identifier) do
      str("urn:iso:std:") >> urn_publisher_copublisher >> urn_type >>
        digits.as(:number) >> urn_part >> urn_stage >> urn_edition >>
        urn_supplement >> urn_extract >> urn_language >> urn_all_parts
    end
  end
end