Class: Pubid::Core::Transformer

Inherits:
Parslet::Transform
  • Object
show all
Defined in:
lib/pubid/core/transformer.rb

Constant Summary collapse

ROMAN_TO_INT =
{
  "I" => 1,
  "V" => 5,
  "X" => 10,
  "L" => 50,
  "C" => 100,
  "D" => 500,
  "M" => 1000,
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransformer

Returns a new instance of Transformer.



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
# File 'lib/pubid/core/transformer.rb', line 13

def initialize
  super

  rule(amendments: subtree(:amendments)) do |context|
    if context[:amendments].is_a?(Array)
      context[:amendments] = context[:amendments].map do |amendment|
        self.class.get_amendment_class.new(number: amendment[:number], year: amendment[:year])
      end
    else
      context[:amendments] =
        [self.class.get_amendment_class.new(
          number: context[:amendments][:number],
          year: context[:amendments][:year])]

    end
    context
  end

  rule(corrigendums: subtree(:corrigendums)) do |context|
    if context[:corrigendums].is_a?(Array)
      context[:corrigendums] = context[:corrigendums].map do |corrigendum|
        self.class.get_corrigendum_class.new(number: corrigendum[:number], year: corrigendum[:year])
      end
    else
      context[:corrigendums] =
        [self.class.get_corrigendum_class.new(
          number: context[:corrigendums][:number],
          year: context[:corrigendums][:year])]
    end
    context
  end

  rule(roman_numerals: simple(:roman_numerals)) do |context|
    roman_to_int(context[:roman_numerals])
  end
end

Class Method Details

.get_amendment_classObject



4
5
6
# File 'lib/pubid/core/transformer.rb', line 4

def get_amendment_class
  Amendment
end

.get_corrigendum_classObject



8
9
10
# File 'lib/pubid/core/transformer.rb', line 8

def get_corrigendum_class
  Corrigendum
end

Instance Method Details

#roman_to_int(roman) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/pubid/core/transformer.rb', line 60

def roman_to_int(roman)
  sum = ROMAN_TO_INT[roman.to_s[0]]
  roman.to_s.chars.each_cons(2) do |c1, c2|
    sum += ROMAN_TO_INT[c2]
    sum -= ROMAN_TO_INT[c1] * 2 if ROMAN_TO_INT[c1] < ROMAN_TO_INT[c2]
  end
  sum
end