Welcome to mirror list, hosted at ThFree Co, Russian Federation.

base_builder.rb « repository_metadata « rpm « packages « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c0a11457ec865623e1f0b2d2823ee267772b516 (plain)
1
2
3
4
5
6
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
# frozen_string_literal: true
module Packages
  module Rpm
    module RepositoryMetadata
      class BaseBuilder
        def initialize(xml: nil, data: {})
          @xml = Nokogiri::XML(xml) if xml.present?
          @data = data
        end

        def execute
          return build_empty_structure if xml.blank?

          update_xml_document
          update_package_count
          xml.to_xml
        end

        private

        attr_reader :xml, :data

        def build_empty_structure
          Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
            xml.method_missing(self.class::ROOT_TAG, self.class::ROOT_ATTRIBUTES)
          end.to_xml
        end

        def update_xml_document
          # Add to the root xml element a new package metadata node
          xml.at(self.class::ROOT_TAG).add_child(build_new_node)
        end

        def update_package_count
          packages_count = xml.css("//#{self.class::ROOT_TAG}/package").count

          xml.at(self.class::ROOT_TAG).attributes["packages"].value = packages_count.to_s
        end

        def build_new_node
          raise NotImplementedError, "#{self.class} should implement #{__method__}"
        end
      end
    end
  end
end