Class: Primer::Classify::Utilities
- Inherits:
-
Object
- Object
- Primer::Classify::Utilities
- Defined in:
- app/lib/primer/classify/utilities.rb
Overview
Handler for PrimerCSS utility classes loaded from utilities.rake
Class Method Summary collapse
-
.classes_to_hash(classes) ⇒ Object
Extract hash from classes ie.
- .classname(key, val, breakpoint = "") ⇒ Object
-
.mappings(key) ⇒ Object
Get the options for the given key.
-
.responsive?(key, val) ⇒ Boolean
Is the key and value responsive.
-
.supported_key?(key) ⇒ Boolean
Does the Utilitiy class support the given key.
-
.supported_selector?(selector) ⇒ Boolean
Does the given selector exist in the utilities file.
-
.supported_value?(key, val) ⇒ Boolean
Does the Utilitiy class support the given key and value.
Class Method Details
.classes_to_hash(classes) ⇒ Object
Extract hash from classes ie. “mr-1 mb-2 foo” => { mr: 1, mb: 2, classes: “foo” }
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/lib/primer/classify/utilities.rb', line 59 def classes_to_hash(classes) # This method is too slow to run in production return { classes: classes } if 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
9 10 11 12 13 14 15 16 |
# File 'app/lib/primer/classify/utilities.rb', line 9 def classname(key, val, breakpoint = "") if (valid = validate(key, val, breakpoint)) valid else # Get selector Primer::Classify::UTILITIES[key][val][Primer::Classify::BREAKPOINTS.index(breakpoint)] end end |
.mappings(key) ⇒ Object
Get the options for the given key
returns Array or nil if key not supported
52 53 54 55 56 |
# File 'app/lib/primer/classify/utilities.rb', line 52 def mappings(key) return unless supported_key?(key) Primer::Classify::UTILITIES[key].keys end |
.responsive?(key, val) ⇒ Boolean
Is the key and value responsive
returns Boolean
45 46 47 |
# File 'app/lib/primer/classify/utilities.rb', line 45 def responsive?(key, val) supported_value?(key, val) && Primer::Classify::UTILITIES[key][val].count > 1 end |
.supported_key?(key) ⇒ Boolean
Does the Utilitiy class support the given key
returns Boolean
21 22 23 |
# File 'app/lib/primer/classify/utilities.rb', line 21 def supported_key?(key) Primer::Classify::UTILITIES[key].present? end |
.supported_selector?(selector) ⇒ Boolean
Does the given selector exist in the utilities file
returns Boolean
35 36 37 38 39 40 |
# File 'app/lib/primer/classify/utilities.rb', line 35 def supported_selector?(selector) # This method is too slow to run in production return false if Rails.env.production? find_selector(selector).present? end |