Skip to content

Commit

Permalink
Neatening up the border to_style rules
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtimberlake committed Jan 7, 2011
1 parent c3a005c commit 9973303
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
58 changes: 27 additions & 31 deletions lib/css/properties/border_property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,35 @@ def name
'border'
end

def style_values
NESW.map { |o| send(o).style }
end

def size_values
NESW.map { |o| send(o).size }
end

def color_values
NESW.map { |o| send(o).color }
end

def to_s
if %w(size style color).map { |p| send("#{p}_values").uniq }.flatten.compact.size == 3
%w(size style color).map { |p| top.send(p) }.join(' ')
elsif %w(style color).map { |p| send("#{p}_values").uniq }.flatten.compact.size == 0
["#{name}-size", top.size].join(':')
elsif %w(style color).map { |p| send("#{p}_values").uniq }.flatten.compact.size == 2
%w(style color).map { |p| top.send(p) }.flatten.compact.join(' ')
elsif %w(size style).map { |p| send("#{p}_values").uniq }.flatten.compact.size == 2
%w(size style).map { |p| top.send(p) }.flatten.compact.join(' ')
sides = NESW.map { |o| @properties[o] }
if %w(size style color).all? { |p| sides.all? { |side| @properties['top'].try(p) == side.try(p) && !side.try(p).nil? } }
%w(size style color).map { |p| top.send(p).value }.join(' ')
elsif %w(style color).all? { |p| sides.all? { |side| @properties['top'].try(p) == side.try(p) && !side.try(p).nil? } }
%w(style color).map { |p| top.send(p).value }.join(' ')
elsif %w(size style).all? { |p| sides.all? { |side| @properties['top'].try(p) == side.try(p) && !side.try(p).nil? } }
%w(size style).map { |p| top.send(p).value }.join(' ')
end
end

def to_style
if %w(size style color).map { |p| send("#{p}_values").uniq }.flatten.compact.size == 3
value = %w(size style color).map { |p| top.send(p) }.compact.join(' ')
[name, value].join(':')
elsif %w(style color).map { |p| send("#{p}_values").uniq }.flatten.compact.size == 0
["#{name}-size", top.size].join(':')
elsif %w(style color).map { |p| send("#{p}_values").uniq }.flatten.compact.size == 2
value = %w(style color).map { |p| top.send(p) }.flatten.compact.join(' ')
[name, value].join(':')
elsif %w(size style).map { |p| send("#{p}_values").uniq }.flatten.compact.size == 2
value = %w(size style).map { |p| top.send(p) }.flatten.compact.join(' ')
sides = NESW.map { |o| @properties[o] }
value = nil
if %w(size style color).all? { |p| sides.all? { |side| @properties['top'].try(p) == side.try(p) && !side.try(p).nil? } }
value = %w(size style color).map { |p| top.send(p).value }.join(' ')
elsif %w(style color).all? { |p| sides.all? { |side| @properties['top'].try(p) == side.try(p) && !side.try(p).nil? } }
value = %w(style color).map { |p| top.send(p).value }.join(' ')
elsif %w(size style).all? { |p| sides.all? { |side| @properties['top'].try(p) == side.try(p) && !side.try(p).nil? } }
value = %w(size style).map { |p| top.send(p).value }.join(' ')
end
if value
[name, value].join(':')
else
NESW.map { |o| send(o).to_style }.join(';')
if %w(style color).all? { |p| sides.all? { |side| side.try(p).nil? } } && sides.all? { |side| @properties['top'].try('size') == side.try('size') && !side.try('size').nil? }
"border-size:#{top.size.value}"
else
sides.map { |side| side.empty? ? nil : side.to_style }.compact.join(';')
end
end
end

Expand Down Expand Up @@ -81,6 +72,11 @@ def method_missing(method_name, *args)
end
end

def respond_to?(method_name, include_private = false)
property_name = normalize_property_name(method_name.to_s[-1..-1] == '=' ? method_name.to_s.chop : method_name)
%w(size style color).include?(property_name) || super
end

private
def init(parent, name, value)
@parent = parent
Expand Down
9 changes: 9 additions & 0 deletions lib/css/property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ def method_missing(method_name, *args, &block)
end
end

def respond_to?(method_name, include_private = false)
property_name = normalize_property_name(method_name.to_s[-1..-1] == '=' ? method_name.to_s.chop : method_name)
default_properties.keys.include?(property_name) || super
end

def empty?
@value.nil? && @properties.all? { |p| p.empty? }
end

private
def init(parent, name, value)
@parent = parent
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/css/properties/border_property_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ module CSS
border.to_style.should == 'border-top:2ex inset red;border-right:3em dashed yellow;border-bottom:4px dotted blue;border-left:5% double green'
end
end

context "with just a single side" do
let(:css) { Parser.parse("div { border-top: 2px solid red }") }
let(:border) { css['div'].border }

it "should return just that side in #to_style" do
border.to_style.should == "border-top:2px solid red"
end
end
end
end
end

0 comments on commit 9973303

Please sign in to comment.