Module: Primer::ViewComponents

Defined in:
lib/primer/view_components.rb,
lib/primer/view_components/engine.rb,
lib/primer/view_components/version.rb,
lib/primer/view_components/statuses.rb,
lib/primer/view_components/constants.rb

Overview

:nodoc:

Defined Under Namespace

Modules: VERSION Classes: Constants, Engine

Constant Summary collapse

DEFAULT_STATIC_PATH =
File.expand_path("static")
FILE_NAMES =
{
  statuses: "statuses.json",
  constants: "constants.json",
  audited_at: "audited_at.json"
}.freeze
STATUSES =
JSON.parse(
  File.read(
    File.join(File.dirname(__FILE__), "../../../static/statuses.json")
  )
).freeze

Class Method Summary collapse

Class Method Details

.dump(stats) ⇒ Object

dump generates the requested stat hash and outputs it to a file.



44
45
46
47
48
49
50
51
# File 'lib/primer/view_components.rb', line 44

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

.generate_audited_atObject

generate_audited_at returns a hash mapping component name to the day the component has passed an accessibility audit.



27
28
29
30
31
# File 'lib/primer/view_components.rb', line 27

def self.generate_audited_at
  Primer::Component.descendants.sort_by(&:name).each_with_object({}) do |component, mem|
    mem[component.to_s] = component.audited_at.to_s
  end
end

.generate_constantsObject

generate_constants returns a hash mapping component name to all of its constants.



35
36
37
38
39
40
41
# File 'lib/primer/view_components.rb', line 35

def self.generate_constants
  Primer::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

.generate_statusesObject

generate_statuses returns a hash mapping component name to the component’s status sorted alphabetically by the component name.



19
20
21
22
23
# File 'lib/primer/view_components.rb', line 19

def self.generate_statuses
  Primer::Component.descendants.sort_by(&:name).each_with_object({}) do |component, mem|
    mem[component.to_s] = component.status.to_s
  end
end

.read(stats) ⇒ Object

read returns a JSON string matching the output of the corresponding stat.



54
55
56
# File 'lib/primer/view_components.rb', line 54

def self.read(stats)
  File.read(File.join(DEFAULT_STATIC_PATH, FILE_NAMES[stats]))
end