Sha256: fcadd0bc7398c01386916ce8f5a8a4aff66a08fbe15e8e346db17c5839a764bd

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require "ariadne/classify"
require "ariadne/view_components/version"
require "ariadne/view_components/engine"
require "ariadne/view_components/constants"

module Ariadne
  # :nodoc:
  module ViewComponents
    DEFAULT_STATIC_PATH = File.expand_path("static")
    FILE_NAMES = {
      statuses: "statuses.json",
      constants: "constants.json",
      audited_at: "audited_at.json",
    }.freeze

    # generate_statuses returns a hash mapping component name to
    # the component's status sorted alphabetically by the component name.
    def self.generate_statuses
      Ariadne::Component.descendants.sort_by(&:name).each_with_object({}) do |component, mem|
        mem[component.to_s] = component.status.to_s
      end
    end

    # generate_audited_at returns a hash mapping component name to
    # the day the component has passed an accessibility audit.
    def self.generate_audited_at
      Ariadne::Component.descendants.sort_by(&:name).each_with_object({}) do |component, mem|
        mem[component.to_s] = component.audited_at.to_s
      end
    end

    # generate_constants returns a hash mapping component name to
    # all of its constants.
    def self.generate_constants
      Ariadne::Component.descendants.sort_by(&:name).each_with_object({}) do |component, mem|
        mem[component.to_s] = component.constants(false).sort.each_with_object({}) do |constant, h|
          h[constant] = component.const_get(constant)
        end
      end
    end

    # dump generates the requested stat hash and outputs it to a file.
    def self.dump(stats)
      require "json"

      File.open(File.join(DEFAULT_STATIC_PATH, FILE_NAMES[stats]), "w") do |f|
        f.write(JSON.pretty_generate(send("generate_#{stats}")))
        f.write($INPUT_RECORD_SEPARATOR)
      end
    end

    # read returns a JSON string matching the output of the corresponding stat.
    def self.read(stats)
      File.read(File.join(DEFAULT_STATIC_PATH, FILE_NAMES[stats]))
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.4 lib/ariadne/view_components.rb
ariadne_view_components-0.0.3 lib/ariadne/view_components.rb
ariadne_view_components-0.0.2 lib/ariadne/view_components.rb
ariadne_view_components-0.0.1 lib/ariadne/view_components.rb