savage 1.1.7 → 1.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -93,6 +93,7 @@ I have no doubt that will be some problems with this thing, as well as features
93
93
  * Jeremy Holland (jeremy@jeremypholland.com, github:awebneck) -- author
94
94
  * Christoffer Klang (toffeklang@yahoo.se, github:christoffer) -- regexp improvements
95
95
  * MatmaRex (github:MatmaRex) -- parser improvements, scientific notation support, bug fixes
96
+ * Brandon Liu (github:bdon) -- spec improvements
96
97
 
97
98
  == Copyright
98
99
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.7
1
+ 1.1.8
@@ -7,8 +7,8 @@ module Savage
7
7
  super(target_x, target_y, absolute)
8
8
  @radius = Point.new(radius_x, radius_y)
9
9
  @rotation = rotation
10
- @large_arc = large_arc
11
- @sweep = sweep
10
+ @large_arc = large_arc.is_a?(Numeric) ? large_arc > 0 : large_arc
11
+ @sweep = sweep.is_a?(Numeric) ? sweep > 0 : sweep
12
12
  end
13
13
 
14
14
  def to_command
@@ -20,4 +20,4 @@ module Savage
20
20
  end
21
21
  end
22
22
  end
23
- end
23
+ end
@@ -1,5 +1,26 @@
1
1
  module Savage
2
2
  class Parser
3
+ DIRECTIONS = {
4
+ :m => {:class => Directions::MoveTo,
5
+ :args => 2},
6
+ :l => {:class => Directions::LineTo,
7
+ :args => 2},
8
+ :h => {:class => Directions::HorizontalTo,
9
+ :args => 1},
10
+ :v => {:class => Directions::VerticalTo,
11
+ :args => 1},
12
+ :c => {:class => Directions::CubicCurveTo,
13
+ :args => 6},
14
+ :s => {:class => Directions::CubicCurveTo,
15
+ :args => 4},
16
+ :q => {:class => Directions::QuadraticCurveTo,
17
+ :args => 4},
18
+ :t => {:class => Directions::QuadraticCurveTo,
19
+ :args => 2},
20
+ :a => {:class => Directions::ArcTo,
21
+ :args => 7}
22
+ }
23
+
3
24
  class << self
4
25
  def parse(parsable)
5
26
  raise TypeError if parsable.class != String
@@ -50,84 +71,30 @@ module Savage
50
71
 
51
72
  def build_direction(parsable, force_absolute=false)
52
73
  directions = []
53
- coordinates = extract_coordinates parsable
74
+ @coordinates = extract_coordinates parsable
54
75
  recurse_code = parsable[0,1]
55
76
  first_absolute = force_absolute
56
77
 
57
78
  # we need to handle this separately, since ClosePath doesn't take any coordinates
58
- if coordinates.empty? && recurse_code =~ /[Zz]/
79
+ if @coordinates.empty? && recurse_code =~ /[Zz]/
59
80
  directions << Directions::ClosePath.new(parsable[0,1] == parsable[0,1].upcase)
60
81
  end
61
82
 
62
- until coordinates.empty?
83
+ until @coordinates.empty?
63
84
  absolute = (first_absolute || parsable[0,1] == parsable[0,1].upcase)
64
- case recurse_code
65
- when /[Mm]/
66
- x = coordinates.shift
67
- y = coordinates.shift
68
- raise TypeError if x.nil? || y.nil?
69
- directions << Directions::MoveTo.new(x,y,absolute)
70
- recurse_code = 'L'
71
- when /[Ll]/
72
- x = coordinates.shift
73
- y = coordinates.shift
74
- raise TypeError if x.nil? || y.nil?
75
- directions << Directions::LineTo.new(x,y,absolute)
76
- when /[Hh]/
77
- target = coordinates.shift
78
- raise TypeError if target.nil?
79
- directions << Directions::HorizontalTo.new(target,absolute)
80
- when /[Vv]/
81
- target = coordinates.shift
82
- raise TypeError if target.nil?
83
- directions << Directions::VerticalTo.new(target,absolute)
84
- when /[Cc]/
85
- control_1_x = coordinates.shift
86
- control_1_y = coordinates.shift
87
- control_2_x = coordinates.shift
88
- control_2_y = coordinates.shift
89
- x = coordinates.shift
90
- y = coordinates.shift
91
- raise TypeError if x.nil? || y.nil? || control_1_x.nil? || control_1_y.nil? || control_2_x.nil? || control_2_y.nil?
92
- directions << Directions::CubicCurveTo.new(control_1_x,control_1_y,control_2_x,control_2_y,x,y,absolute)
93
- when /[Ss]/
94
- control_2_x = coordinates.shift
95
- control_2_y = coordinates.shift
96
- x = coordinates.shift
97
- y = coordinates.shift
98
- raise TypeError if x.nil? || y.nil? || control_2_x.nil? || control_2_y.nil?
99
- directions << Directions::CubicCurveTo.new(control_2_x,control_2_y,x,y,absolute)
100
- when /[Qq]/
101
- control_x = coordinates.shift
102
- control_y = coordinates.shift
103
- x = coordinates.shift
104
- y = coordinates.shift
105
- raise TypeError if x.nil? || y.nil? || control_x.nil? || control_y.nil?
106
- directions << Directions::QuadraticCurveTo.new(control_x,control_y,x,y,absolute)
107
- when /[Tt]/
108
- x = coordinates.shift
109
- y = coordinates.shift
110
- raise TypeError if x.nil? || y.nil?
111
- directions << Directions::QuadraticCurveTo.new(x,y,absolute)
112
- when /[Aa]/
113
- rx = coordinates.shift
114
- ry = coordinates.shift
115
- rotation = coordinates.shift
116
- large_arc = (coordinates.shift > 0) ? true : false
117
- sweep = (coordinates.shift > 0) ? true : false
118
- x = coordinates.shift
119
- y = coordinates.shift
120
- raise TypeError if x.nil? || y.nil? || rx.nil? || ry.nil? || rotation.nil?
121
- directions << Directions::ArcTo.new(rx,ry,rotation,large_arc,sweep,x,y,absolute)
122
- when /[^MmLlHhVvCcSsQqTtAaZz]/
123
- coordinates = []
124
- raise TypeError
125
- end
85
+ directions << construct_direction(recurse_code.strip[0].downcase.intern, absolute)
86
+ recurse_code = 'L' if recurse_code.downcase =~ /m/
126
87
  first_absolute = false
127
88
  end
128
89
 
129
90
  directions
130
91
  end
92
+
93
+ def construct_direction(recurse_code, absolute)
94
+ args = @coordinates.shift DIRECTIONS[recurse_code][:args]
95
+ raise TypeError if args.any?(&:nil?)
96
+ DIRECTIONS[recurse_code][:class].new(*args, absolute)
97
+ end
131
98
 
132
99
  def extract_coordinates(command_string)
133
100
  coordinates = []
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "savage"
8
- s.version = "1.1.7"
8
+ s.version = "1.1.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jeremy Holland"]
12
- s.date = "2012-10-16"
12
+ s.date = "2013-01-03"
13
13
  s.description = "A little gem for extracting and manipulating SVG vector path data."
14
14
  s.email = "jeremy@jeremypholland.com"
15
15
  s.extra_rdoc_files = [
@@ -12,7 +12,7 @@ describe ArcTo do
12
12
  end
13
13
 
14
14
  include Command
15
- include DirectionShared
15
+ it_behaves_like 'Direction'
16
16
 
17
17
  it 'should have a target' do
18
18
  @dir.respond_to?(:target).should == true
@@ -9,7 +9,7 @@ describe ClosePath do
9
9
  def create_relative; ClosePath.new(false); end
10
10
  def command_code; 'z'; end
11
11
  include Command
12
- include DirectionShared
12
+ it_behaves_like 'Direction'
13
13
  it 'should be constructed with with either no parameters or a single boolean parameter' do
14
14
  lambda { ClosePath.new }.should_not raise_error
15
15
  lambda { ClosePath.new true }.should_not raise_error
@@ -12,7 +12,7 @@ describe CubicCurveTo do
12
12
  end
13
13
 
14
14
  include Command
15
- include DirectionShared
15
+ it_behaves_like 'Direction'
16
16
 
17
17
  it 'should have a target' do
18
18
  @dir.respond_to?(:target).should == true
@@ -6,5 +6,5 @@ describe HorizontalTo do
6
6
  def dir_class; HorizontalTo; end
7
7
  def create_relative; HorizontalTo.new(100,false); end
8
8
  def command_code; 'h'; end
9
- include CoordinateTargetShared
9
+ it_behaves_like 'CoordinateTarget'
10
10
  end
@@ -6,5 +6,5 @@ describe LineTo do
6
6
  def dir_class; LineTo; end
7
7
  def create_relative; LineTo.new(100,200,false); end
8
8
  def command_code; 'l'; end
9
- include PointTargetShared
9
+ it_behaves_like 'PointTarget'
10
10
  end
@@ -6,5 +6,5 @@ describe MoveTo do
6
6
  def dir_class; MoveTo; end
7
7
  def create_relative; MoveTo.new(100,200,false); end
8
8
  def command_code; 'm'; end
9
- include PointTargetShared
9
+ it_behaves_like 'PointTarget'
10
10
  end
@@ -12,7 +12,7 @@ describe QuadraticCurveTo do
12
12
  end
13
13
 
14
14
  include Command
15
- include DirectionShared
15
+ it_behaves_like 'Direction'
16
16
 
17
17
  it 'should have a target' do
18
18
  @dir.respond_to?(:target).should == true
@@ -6,5 +6,5 @@ describe VerticalTo do
6
6
  def dir_class; VerticalTo; end
7
7
  def create_relative; VerticalTo.new(100,false); end
8
8
  def command_code; 'v'; end
9
- include CoordinateTargetShared
9
+ it_behaves_like 'CoordinateTarget'
10
10
  end
@@ -1,9 +1,9 @@
1
- share_as :CoordinateTargetShared do
1
+ shared_examples 'CoordinateTarget' do
2
2
  before :each do
3
3
  @dir = dir_class.new(100)
4
4
  end
5
5
  include Command
6
- include DirectionShared
6
+ it_behaves_like 'Direction'
7
7
  it 'should have an accessible target, based on the constructor argument' do
8
8
  @dir.respond_to?(:target).should == true
9
9
  @dir.target.should == 100
@@ -1,4 +1,4 @@
1
- share_as :DirectionShared do
1
+ shared_examples "Direction" do
2
2
  include Command
3
3
  it 'should have a to_command method' do
4
4
  @dir.respond_to?(:to_command).should == true
@@ -1,9 +1,9 @@
1
- share_as :PointTargetShared do
1
+ shared_examples_for 'PointTarget' do
2
2
  before :each do
3
3
  @dir = dir_class.new(100,200)
4
4
  end
5
5
  include Command
6
- include DirectionShared
6
+ it_behaves_like 'Direction'
7
7
  it 'should have a target' do
8
8
  @dir.respond_to?(:target).should == true
9
9
  @dir.target.class.should == Point
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: savage
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.7
4
+ version: 1.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-16 00:00:00.000000000 Z
12
+ date: 2013-01-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec