Class: Yattho::Classify::Utilities

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

Overview

Handler for YatthoCSS utility classes loaded from utilities.rake

Constant Summary collapse

UTILITIES =
YAML.safe_load(
  File.read(
    File.join(File.dirname(__FILE__), "utilities.yml")
  ),
  permitted_classes: [Symbol]
).freeze
BREAKPOINTS =
["", "-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",
  "^color-bg" => "bg",
  "^color-border" => "border_color",
  "^color-fg" => "color"
}.freeze
SUPPORTED_KEY_CACHE =
Hash.new { |h, k| h[k] = !UTILITIES[k].nil? }
BREAKPOINT_INDEX_CACHE =
Hash.new { |h, k| h[k] = BREAKPOINTS.index(k) }

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.validate_class_namesObject Also known as: validate_class_names?

Returns the value of attribute validate_class_names.



37
38
39
# File 'lib/yattho/classify/utilities.rb', line 37

def validate_class_names
  @validate_class_names
end

Class Method Details

.classes_to_args(classes) ⇒ Object



127
128
129
# File 'lib/yattho/classify/utilities.rb', line 127

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” }



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/yattho/classify/utilities.rb', line 93

def classes_to_hash(classes)
  # This method is too slow to run in production
  return { classes: classes } unless validate_class_names?

  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



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/yattho/classify/utilities.rb', line 40

def classname(key, val, breakpoint = "")
  # For cases when `argument: false` is passed in, treat like we would nil
  return nil unless val

  if (valid = validate(key, val, breakpoint))
    valid
  else
    # Get selector
    UTILITIES[key][val][BREAKPOINT_INDEX_CACHE[breakpoint]]
  end
end

.hash_to_args(hash) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/yattho/classify/utilities.rb', line 131

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



86
87
88
89
90
# File 'lib/yattho/classify/utilities.rb', line 86

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)


79
80
81
# File 'lib/yattho/classify/utilities.rb', line 79

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

.supported_key?(key) ⇒ Boolean

Does the Utility class support the given key

returns Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/yattho/classify/utilities.rb', line 55

def supported_key?(key)
  SUPPORTED_KEY_CACHE[key]
end

.supported_selector?(selector) ⇒ Boolean

Does the given selector exist in the utilities file

returns Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
# File 'lib/yattho/classify/utilities.rb', line 69

def supported_selector?(selector)
  # This method is too slow to run in production
  return false unless validate_class_names?

  find_selector(selector).present?
end

.supported_value?(key, val) ⇒ Boolean

Does the Utility class support the given key and value

returns Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/yattho/classify/utilities.rb', line 62

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

.validate(key, val, breakpoint) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/yattho/classify/utilities.rb', line 146

def validate(key, val, breakpoint)
  unless supported_key?(key)
    raise ArgumentError, "#{key} is not a valid Yattho utility key" if validate_class_names?

    return ""
  end

  unless breakpoint.empty? || responsive?(key, val)
    raise ArgumentError, "#{key} does not support responsive values" if validate_class_names?

    return ""
  end

  unless supported_value?(key, val)
    if validate_class_names?
      raise ArgumentError,
            "#{val} is not a valid value for :#{key}. Use one of #{mappings(key)}"
    end

    return nil if [true, false].include?(val)

    return "#{key.to_s.dasherize}-#{val.to_s.dasherize}"
  end

  nil
end