Skip to content

Commit 927b037

Browse files
committed
Refactors margin and padding transformers to dry up code
1 parent 2ba21e7 commit 927b037

File tree

4 files changed

+53
-58
lines changed

4 files changed

+53
-58
lines changed

lib/csscss.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
require "csscss/parser/common"
1616
require "csscss/parser/color"
1717
require "csscss/parser/base"
18+
require "csscss/parser/multi_width_transformer"
19+
1820
require "csscss/parser/background"
1921
require "csscss/parser/list_style"
2022
require "csscss/parser/margin"

lib/csscss/parser/margin.rb

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,8 @@ class Parser < Parslet::Parser
2424
end
2525

2626
class Transformer < Parslet::Transform
27-
rule(margin: simple(:inherit)) {[]}
28-
29-
SIDE = proc {|side, value| Declaration.from_parser("margin-#{side}", value) }
30-
31-
rule(margin: {
32-
top:simple(:top),
33-
right:simple(:right),
34-
bottom:simple(:bottom),
35-
left:simple(:left)
36-
}) {
37-
values = [top, right, bottom, left].compact
38-
case values.size
39-
when 4
40-
%w(top right bottom left).zip(values).map {|side, value| SIDE[side, value] }
41-
when 3
42-
%w(top right bottom).zip(values).map {|side, value| SIDE[side, value] }.tap do |declarations|
43-
declarations << SIDE["left", values[1]]
44-
end
45-
when 2
46-
%w(top right).zip(values).map {|side, value| SIDE[side, value] }.tap do |declarations|
47-
declarations << SIDE["bottom", values[0]]
48-
declarations << SIDE["left", values[1]]
49-
end
50-
when 1
51-
%w(top right bottom left).map do |side|
52-
SIDE[side, values[0]]
53-
end
54-
end
55-
}
27+
@property = :margin
28+
extend MultiWidthTransformer
5629
end
5730
end
5831
end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module Csscss
2+
module Parser
3+
module MultiWidthTransformer
4+
def self.extended(base)
5+
base.instance_eval do
6+
extend ClassMethods
7+
8+
rule(@property => simple(:inherit)) {[]}
9+
10+
rule({@property => {
11+
top:simple(:top),
12+
right:simple(:right),
13+
bottom:simple(:bottom),
14+
left:simple(:left)
15+
}}, &method(:transform_sides))
16+
end
17+
end
18+
19+
module ClassMethods
20+
def side_declaration(side, value)
21+
Declaration.from_parser("#{@property}-#{side}", value)
22+
end
23+
24+
def transform_sides(context)
25+
values = [context[:top], context[:right], context[:bottom], context[:left]].compact
26+
case values.size
27+
when 4
28+
%w(top right bottom left).zip(values).map {|side, value| side_declaration(side, value) }
29+
when 3
30+
%w(top right bottom).zip(values).map {|side, value| side_declaration(side, value) }.tap do |declarations|
31+
declarations << side_declaration("left", values[1])
32+
end
33+
when 2
34+
%w(top right).zip(values).map {|side, value| side_declaration(side, value) }.tap do |declarations|
35+
declarations << side_declaration("bottom", values[0])
36+
declarations << side_declaration("left", values[1])
37+
end
38+
when 1
39+
%w(top right bottom left).map do |side|
40+
side_declaration(side, values[0])
41+
end
42+
end
43+
end
44+
end
45+
end
46+
end
47+
end

lib/csscss/parser/padding.rb

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,8 @@ class Parser < Parslet::Parser
2424
end
2525

2626
class Transformer < Parslet::Transform
27-
rule(padding: simple(:inherit)) {[]}
28-
29-
SIDE = proc {|side, value| Declaration.from_parser("padding-#{side}", value) }
30-
31-
rule(padding: {
32-
top:simple(:top),
33-
right:simple(:right),
34-
bottom:simple(:bottom),
35-
left:simple(:left)
36-
}) {
37-
values = [top, right, bottom, left].compact
38-
case values.size
39-
when 4
40-
%w(top right bottom left).zip(values).map {|side, value| SIDE[side, value] }
41-
when 3
42-
%w(top right bottom).zip(values).map {|side, value| SIDE[side, value] }.tap do |declarations|
43-
declarations << SIDE["left", values[1]]
44-
end
45-
when 2
46-
%w(top right).zip(values).map {|side, value| SIDE[side, value] }.tap do |declarations|
47-
declarations << SIDE["bottom", values[0]]
48-
declarations << SIDE["left", values[1]]
49-
end
50-
when 1
51-
%w(top right bottom left).map do |side|
52-
SIDE[side, values[0]]
53-
end
54-
end
55-
}
27+
@property = :padding
28+
extend MultiWidthTransformer
5629
end
5730
end
5831
end

0 commit comments

Comments
 (0)