Sha256: b234dc433fecefebb9ce694189191170bf3343e2da71a03e642e14909be014ab

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module RpiMarca
  class ViennaClass
    attr_reader :edition, :subclass1, :subclass2, :subclass3, :subclass4,
                :subclass5

    def initialize(edition:, subclass1:, subclass2: nil, subclass3: nil,
                   subclass4: nil, subclass5: nil)
      @edition = edition
      @subclass1 = subclass1
      @subclass2 = subclass2
      @subclass3 = subclass3
      @subclass4 = subclass4
      @subclass5 = subclass5
    end

    def self.parse(el)
      return unless el

      subclasses = parse_subclasses(el)

      new(
        edition: Helpers.get_attribute_value(el, 'edicao').to_i,
        **subclasses
      )
    end

    def self.parse_subclasses(el)
      subclasses = el.xpath('.//classe-vienna').map { |s| s['codigo'] }
      fail ParseError, "Vienna class can't have more than 5 subclasses" if
        subclasses.length > 5

      {
        subclass1: subclasses[0],
        subclass2: subclasses[1],
        subclass3: subclasses[2],
        subclass4: subclasses[3],
        subclass5: subclasses[4]
      }
    end

    private_class_method :parse_subclasses
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rpi_marca-0.0.1 lib/rpi_marca/vienna_class.rb