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")
DEFAULT_STATUS_FILE_NAME =
"statuses.json"
DEFAULT_CONSTANTS_FILE_NAME =
"constants.json"
STATUSES =
JSON.parse(
  File.read(
    File.join(File.dirname(__FILE__), "../../../static/statuses.json")
  )
).freeze

Class Method Summary collapse

Class Method Details

.dump_constants(path: DEFAULT_STATIC_PATH) ⇒ Object

dump_constants generates the constants hash and then serializes it as json at the given path



53
54
55
56
57
58
59
60
61
62
# File 'lib/primer/view_components.rb', line 53

def self.dump_constants(path: DEFAULT_STATIC_PATH)
  require "json"

  constants = generate_constants

  File.open(File.join(path, DEFAULT_CONSTANTS_FILE_NAME), "w") do |f|
    f.write(JSON.pretty_generate(constants))
    f.write($INPUT_RECORD_SEPARATOR)
  end
end

.dump_statuses(path: DEFAULT_STATIC_PATH) ⇒ Object

dump_statuses generates the status hash and then serializes it as json at the given path



24
25
26
27
28
29
30
31
32
33
# File 'lib/primer/view_components.rb', line 24

def self.dump_statuses(path: DEFAULT_STATIC_PATH)
  require "json"

  statuses = generate_statuses

  File.open(File.join(path, DEFAULT_STATUS_FILE_NAME), "w") do |f|
    f.write(JSON.pretty_generate(statuses))
    f.write($INPUT_RECORD_SEPARATOR)
  end
end

.generate_constantsObject

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



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

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.



16
17
18
19
20
# File 'lib/primer/view_components.rb', line 16

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_constants(path: DEFAULT_STATIC_PATH) ⇒ Object

read_constants returns a JSON string matching the output of generate_constants



66
67
68
# File 'lib/primer/view_components.rb', line 66

def self.read_constants(path: DEFAULT_STATIC_PATH)
  File.read(File.join(path, DEFAULT_CONSTANTS_FILE_NAME))
end

.read_statuses(path: DEFAULT_STATIC_PATH) ⇒ Object

read_statuses returns a JSON string matching the output of generate_statuses



37
38
39
# File 'lib/primer/view_components.rb', line 37

def self.read_statuses(path: DEFAULT_STATIC_PATH)
  File.read(File.join(path, DEFAULT_STATUS_FILE_NAME))
end