Class: Primer::Classify::Utilities

Inherits:
Object
  • Object
show all
Defined in:
lib/primer/classify/utilities.rb

Overview

Handler for PrimerCSS utility classes loaded from utilities.rake

Constant Summary collapse

UTILITIES =

Load the utilities.yml file. Disabling because we want to load symbols, strings, and integers from the .yml file rubocop:disable Security/YAMLLoad

YAML.load(
  File.read(
    File.join(File.dirname(__FILE__), "./utilities.yml")
  )
).freeze
BREAKPOINTS =

rubocop:enable Security/YAMLLoad

["", "-sm", "-md", "-lg", "-xl"].freeze
REPLACEMENT_KEYS =

Replacements for some classnames that end up being a different argument key

{
  "^anim" => "animation",
  "^v-align" => "vertical_align",
  "^d" => "display",
  "^wb" => "word_break",
  "^v" => "visibility",
  "^width" => "w",
  "^height" => "h"
}.freeze

Class Method Summary collapse

Class Method Details

.classes_to_args(classes) ⇒ Object



117
118
119
# File 'lib/primer/classify/utilities.rb', line 117

def classes_to_args(classes)
  hash_to_args(classes_to_hash(classes))
end

.classes_to_hash(classes) ⇒ Object

Extract hash from classes ie. “mr-1 mb-2 foo” => { mr: 1, mb: 2, classes: “foo” }



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/primer/classify/utilities.rb', line 83

def classes_to_hash(classes)
  # This method is too slow to run in production
  return { classes: classes } if ENV["RAILS_ENV"] == "production"

  obj = {}
  classes = classes.split
  # Loop through all classes supplied and reject ones we find a match for
  # So when we're at the end of the loop we have classes left with any non-system classes.
  classes.reject! do |classname|
    key, value, index = find_selector(classname)
    next false if key.nil?

    # Create array if nil
    obj[key] = Array.new(5, nil) if obj[key].nil?
    # Place the arguments in the responsive array based on index mr: [nil, 2]
    obj[key][index] = value
    next true
  end

  # Transform responsive arrays into arrays without trailing nil, so `mr: [1, nil, nil, nil, nil]` becomes `mr: 1`
  obj.transform_values! do |value|
    value = value.reverse.drop_while(&:nil?).reverse
    if value.count == 1
      value.first
    else
      value
    end
  end

  # Add back the non-system classes
  obj[:classes] = classes.join(" ") if classes.any?
  obj
end

.classname(key, val, breakpoint = "") ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/primer/classify/utilities.rb', line 33

def classname(key, val, breakpoint = "")
  if (valid = validate(key, val, breakpoint))
    valid
  else
    # Get selector
    UTILITIES[key][val][BREAKPOINTS.index(breakpoint)]
  end
end

.hash_to_args(hash) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/primer/classify/utilities.rb', line 121

def hash_to_args(hash)
  hash.map do |key, value|
    val = case value
          when Symbol
            ":#{value}"
          when String
            value.to_json
          else
            value
          end

    "#{key}: #{val}"
  end.join(", ")
end

.mappings(key) ⇒ Object

Get the options for the given key

returns Array or nil if key not supported



76
77
78
79
80
# File 'lib/primer/classify/utilities.rb', line 76

def mappings(key)
  return unless supported_key?(key)

  UTILITIES[key].keys
end

.responsive?(key, val) ⇒ Boolean

Is the key and value responsive

returns Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/primer/classify/utilities.rb', line 69

def responsive?(key, val)
  supported_value?(key, val) && UTILITIES[key][val].count > 1
end

.supported_key?(key) ⇒ Boolean

Does the Utilitiy class support the given key

returns Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/primer/classify/utilities.rb', line 45

def supported_key?(key)
  UTILITIES[key].present?
end

.supported_selector?(selector) ⇒ Boolean

Does the given selector exist in the utilities file

returns Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
# File 'lib/primer/classify/utilities.rb', line 59

def supported_selector?(selector)
  # This method is too slow to run in production
  return false if ENV["RAILS_ENV"] == "production"

  find_selector(selector).present?
end

.supported_value?(key, val) ⇒ Boolean

Does the Utilitiy class support the given key and value

returns Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/primer/classify/utilities.rb', line 52

def supported_value?(key, val)
  supported_key?(key) && UTILITIES[key][val].present?
end