Commit 21bdd0a

mo khan <mo@mokhan.ca>
2015-04-08 21:54:38
convert snake case attributes to hyphen case.
1 parent c7363e6
Changed files (4)
lib/scale/shapes/rectangle.rb
@@ -4,6 +4,9 @@ module Scale
     attribute :width, String
     attribute :height, String
     attribute :fill, String
+    attribute :fill_opactiy, String
+    attribute :stroke, String
+    attribute :stroke_opacity, String
     attribute :x, Integer
     attribute :y, Integer
     attribute :rx, Integer
lib/scale/node.rb
@@ -36,7 +36,11 @@ module Scale
     end
 
     def xml_attributes
-      attributes.delete_if { |key, value| value.nil? }
+      attributes.inject({}) do |memo, (key, value)|
+        new_key = key.to_s.gsub(/\_/, "-").to_sym
+        memo[new_key] = value unless value.nil?
+        memo
+      end
     end
 
     private
spec/shapes/path_spec.rb
@@ -23,8 +23,11 @@ describe Scale::Path do
   end
 
   describe "#horizontal" do
-    it 'draws a horizontal line' do
+    before :each do
       subject.move_to(x: 10, y: 10)
+    end
+
+    it 'draws a horizontal line' do
       subject.horizontal(90)
       expected = <<-XML
 <?xml version="1.0"?>
@@ -34,7 +37,6 @@ describe Scale::Path do
     end
 
     it 'moves horizontally using relative position' do
-      subject.move_to(x: 10, y: 10)
       subject.horizontal(90, relative: true)
       expected = <<-XML
 <?xml version="1.0"?>
@@ -44,7 +46,7 @@ describe Scale::Path do
     end
   end
 
-  describe "#vertial" do
+  describe "#vertical" do
     before :each do
       subject.move_to(x: 10, y: 10)
     end
@@ -88,7 +90,7 @@ describe Scale::Path do
       subject.vertical(90)
       subject.horizontal(10)
       subject.line_to(x: 10, y: 10)
-      
+
       expected = <<-XML
 <?xml version="1.0"?>
 <path d="M10 10 H 90 V 90 H 10 L 10 10"/>
spec/node_spec.rb
@@ -3,6 +3,7 @@ describe Scale::Node do
     include Scale::Node
     attribute :x, Integer
     attribute :y, Integer
+    attribute :fill_opacity, Float
 
     def xml_tag
       :fake
@@ -42,5 +43,14 @@ describe Scale::Node do
       XML
       expect(subject.to_xml).to eql(expected)
     end
+
+    it 'replaces underscores in attribute names' do
+      subject.fill_opacity = 0.5
+      expected = <<-XML
+<?xml version="1.0"?>
+<fake fill-opacity="0.5"/>
+      XML
+      expect(subject.to_xml).to eql(expected)
+    end
   end
 end