Sha256: cb3611fad3db9f2e9751ae63d0fb65288e05fcc9f4e92fc912d10d8bfca56863

Contents?: true

Size: 1.74 KB

Versions: 28

Compression:

Stored size: 1.74 KB

Contents

module GovukPublishingComponents
  # @private
  class ComponentDocs
    def initialize(gem_components: false, limit_to: false)
      @limit_to = limit_to
      @documentation_directory = gem_components ? gem_documentation_directory : app_documentation_directory
    end

    def get(id)
      component = fetch_component_doc(id)
      build(component)
    end

    def all
      fetch_component_docs.map { |component| build(component) }.sort_by(&:name)
    end

    def used_in_this_app
      fetch_component_docs.map { |component| build(component) if component_in_use(component[:id]) }.compact.sort_by(&:name)
    end

  private

    def build(component)
      ComponentDoc.new(component)
    end

    def fetch_component_docs
      doc_files = Rails.root.join(@documentation_directory, "*.yml")
      Dir[doc_files].sort.map { |file| parse_documentation(file) }
    end

    def component_in_use(component)
      return true if @limit_to.include?(component)
    end

    def fetch_component_doc(id)
      file = Rails.root.join(@documentation_directory, "#{id}.yml")
      if !file.exist?
        file = gem_documentation_directory.join("#{id}.yml")
        parse_documentation(file).merge(source: "gem")
      else
        parse_documentation(file).merge(source: "application")
      end
    end

    def parse_documentation(file)
      { id: File.basename(file, ".yml") }.merge(YAML::load_file(file)).with_indifferent_access
    end

    def app_documentation_directory
      Rails.root.join("app", "views", GovukPublishingComponents::Config.component_directory_name, "docs")
    end

    def gem_documentation_directory
      Pathname.new(GovukPublishingComponents::Config.gem_directory).join("app", "views", "govuk_publishing_components", "components", "docs")
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
govuk_publishing_components-21.42.0 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.41.4 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.41.3 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.41.2 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.41.1 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.41.0 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.40.0 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.39.0 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.38.5 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.38.4 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.38.3 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.38.2 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.38.1 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.38.0 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.37.0 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.36.1 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.36.0 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.35.0 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.34.1 app/models/govuk_publishing_components/component_docs.rb
govuk_publishing_components-21.34.0 app/models/govuk_publishing_components/component_docs.rb