Skip to content

Instantly share code, notes, and snippets.

@bsa7
Last active August 29, 2015 14:22
Show Gist options
  • Save bsa7/651791e5bb32754f512c to your computer and use it in GitHub Desktop.
Save bsa7/651791e5bb32754f512c to your computer and use it in GitHub Desktop.
Iterate styles on create
<%
def rules(sizes, style_selector, rules)
rule_stub = "<rules>"
size_stub = "<size>"
rules_result = ""
sizes.each do |size|
rule_template = "#{style_selector.sub(size_stub, "#{size}")}{#{rule_stub}}"
[rules].flatten.each do |rule|
rule_template.sub!(rule_stub, rule.gsub(size_stub, "#{size}") + rule_stub)
end
rules_result << rule_template.sub(rule_stub, "")
end
rules_result
end
%>
/************************ MARGINS *******************************/
<%= rules(%w(3 5 10 20 40 60 100 150 300), ".ml<size>", "margin-left: <size>px;") %>
<%= rules(%w(10 20), ".ml<size>p", "margin-left: <size>% !important;") %>
<%= rules(%w(5 10 15), ".mr<size>", "margin-right: <size>px !important;") %>
<%= rules(%w(5 10 15 16 20 30 40 44 50 100), ".mt<size>", "margin-top: <size>px !important;") %>
<%= rules(%w(5 10), ".mt<size>p", "margin-top: <size>% !important;") %>
<%= rules(%w(0 5 10 15 20 40 55 60), ".mb<size>", "margin-bottom: <size>px !important;") %>
<%= rules(%w(5 8 10 15 20), ".m<size>", "margin: <size>px !important;") %>
<%= rules(%w(4 20 30), ".mh<size>", "margin-left:<size>px !important;margin-right:<size>px !important;") %>
<%= rules(%w(8), ".mv<size>", "margin-top:<size>px !important;margin-bottom:<size>px !important;") %>
/************************ PADDINGS *******************************/
<%= rules(%w(5 10 20), ".pl<size>", "padding-left:<size>px !important;") %>
<%= rules(%w(10 20), ".pr<size>", "padding-right:<size>px !important;") %>
<%= rules(%w(5 10), ".pt<size>", "padding-top:<size>px !important;") %>
<%= rules(%w(0), ".pb<size>", "padding-bottom:<size>px !important;") %>
<%= rules(%w(5 10 20 40), ".p<size>", "padding:<size>px !important;") %>
<%= rules(%w(30), ".ph<size>", "padding-left:<size>px !important;padding-right:<size>px !important;") %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment