frill 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/frill/frill.rb +58 -60
  3. data/spec/frill_spec.rb +7 -6
  4. metadata +12 -12
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.7
1
+ 0.1.8
@@ -7,10 +7,11 @@ module Frill
7
7
  end
8
8
 
9
9
  def self.decorators
10
- list.to_a
10
+ @decorators ||= list.to_a
11
11
  end
12
12
 
13
13
  def self.reset!
14
+ @decorators = nil
14
15
  @list = nil
15
16
  end
16
17
 
@@ -42,7 +43,7 @@ module Frill
42
43
  end
43
44
 
44
45
  def add label
45
- @nodes[label] ||= Node.new label
46
+ nodes[label] ||= Node.new label
46
47
  end
47
48
 
48
49
  def move_before label1, label2
@@ -51,19 +52,19 @@ module Frill
51
52
 
52
53
  node1.move_before node2
53
54
 
54
- detect_cycles
55
+ CycleDetecter.detect! nodes
55
56
  end
56
57
 
57
58
  def [](label)
58
- @nodes[label]
59
+ nodes[label]
59
60
  end
60
61
 
61
62
  def empty?
62
- @nodes.empty?
63
+ nodes.empty?
63
64
  end
64
65
 
65
66
  def include? label
66
- @nodes.keys.include? label
67
+ nodes[label]
67
68
  end
68
69
 
69
70
  def index label
@@ -71,37 +72,65 @@ module Frill
71
72
  end
72
73
 
73
74
  def to_a
74
- lists = []
75
+ array = []
75
76
 
76
- @nodes.values.each do |node|
77
- unless lists.include? node.label
78
- first = node.first
79
- lists += first.to_a
80
- end
77
+ nodes.values.each do |node|
78
+ array += construct_array(node) unless array.include? node.label
81
79
  end
82
80
 
83
- lists
81
+ array
84
82
  end
85
83
 
86
84
  private
85
+ attr_reader :nodes
87
86
 
88
- def detect_cycles
89
- @nodes.values.each do |node|
90
- visited = {}
91
- visited[node.label] = true
87
+ def construct_array node
88
+ array = []
89
+ current_node = node.first
92
90
 
93
- current_node = node.next
94
- while current_node
95
- raise Frill::CyclicDependency if visited[current_node.label]
96
- visited[current_node.label] = true
97
- current_node = current_node.next
91
+ while current_node
92
+ array << current_node.label
93
+ current_node = current_node.next
94
+ end
95
+
96
+ array
97
+ end
98
+
99
+ class CycleDetecter
100
+ def self.detect! nodes
101
+ new(nodes).detect!
102
+ end
103
+
104
+ def initialize(nodes)
105
+ @nodes = nodes
106
+ @visited = {}
107
+ end
108
+
109
+ def detect!
110
+ nodes.values.each do |node|
111
+ fan_out node unless visited[node.label]
98
112
  end
113
+ end
114
+
115
+
116
+ private
117
+
118
+ attr_reader :nodes, :visited
119
+
120
+ def fan_out(node)
121
+ visited[node.label] = true
122
+
123
+ fan :next, node
124
+ fan :previous, node
125
+ end
126
+
127
+ def fan(direction, start_node)
128
+ current_node = start_node.send direction
99
129
 
100
- current_node = node.previous
101
130
  while current_node
102
131
  raise Frill::CyclicDependency if visited[current_node.label]
103
132
  visited[current_node.label] = true
104
- current_node = current_node.previous
133
+ current_node = current_node.send direction
105
134
  end
106
135
  end
107
136
  end
@@ -125,47 +154,16 @@ module Frill
125
154
  end
126
155
 
127
156
  def first
128
- node = nil
129
- current_node = self
130
-
131
- until node
132
- if current_node.previous
133
- current_node = current_node.previous
134
- else
135
- node = current_node
136
- end
137
- end
138
-
139
- node
157
+ first_node = self
158
+ first_node = first_node.previous while first_node.previous
159
+ first_node
140
160
  end
141
161
 
142
162
  def last
143
- current_node = self
144
- last_node = nil
145
-
146
- until last_node
147
- if current_node.next
148
- current_node = current_node.next
149
- else
150
- last_node = current_node
151
- end
152
- end
153
-
163
+ last_node = self
164
+ last_node = last_node.next while last_node.next
154
165
  last_node
155
166
  end
156
-
157
- def to_a
158
- current_node = self
159
-
160
- list = []
161
-
162
- until current_node == nil
163
- list << current_node.label
164
- current_node = current_node.next
165
- end
166
-
167
- list
168
- end
169
167
  end
170
168
  end
171
169
  end
@@ -71,10 +71,10 @@ describe Frill do
71
71
 
72
72
  describe ".before" do
73
73
  it "inserts the current module before the requested module in Frill's list of decorators" do
74
+ Module4.before Module3
74
75
  Module2.before Module1
75
76
  Module5.after Module4
76
77
  Module3.before Module2
77
- Module4.before Module3
78
78
 
79
79
  Frill.decorators.index(Module5).should be > Frill.decorators.index(Module4)
80
80
  Frill.decorators.index(Module3).should be < Frill.decorators.index(Module2)
@@ -93,19 +93,19 @@ describe Frill do
93
93
 
94
94
  describe ".before" do
95
95
  it "inserts the current module before the requested module in Frill's list of decorators" do
96
- Frill.decorators.should == [Module1, Module2, Module3]
96
+ Frill.list.to_a.should == [Module1, Module2, Module3]
97
97
 
98
98
  Module1.before Module2
99
- Frill.decorators.should == [Module1, Module2, Module3]
99
+ Frill.list.to_a.should == [Module1, Module2, Module3]
100
100
 
101
101
  Module3.before Module2
102
- Frill.decorators.should == [Module3, Module1, Module2]
102
+ Frill.list.to_a.should == [Module3, Module1, Module2]
103
103
  end
104
104
  end
105
105
 
106
106
  describe ".after" do
107
107
  it "inserts the current module after the requested module in Frill's list of decorators" do
108
- Frill.decorators.should == [Module1, Module2, Module3]
108
+ Frill.list.to_a.should == [Module1, Module2, Module3]
109
109
 
110
110
  Module1.after Module2
111
111
  Module3.after Module2
@@ -136,10 +136,11 @@ describe Frill do
136
136
 
137
137
  it "should throw exceptions when cycles are detected" do
138
138
  g = Frill::List.new
139
+ g.move_before "c", "b"
139
140
  g.move_before "b", "a"
140
141
 
141
142
  expect {
142
- g.move_before "a", "b"
143
+ g.move_before "a", "c"
143
144
  }.to raise_exception(Frill::CyclicDependency)
144
145
  end
145
146
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frill
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-23 00:00:00.000000000 Z
12
+ date: 2012-08-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70180336114600 !ruby/object:Gem::Requirement
16
+ requirement: &70351976271960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.2.2
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70180336114600
24
+ version_requirements: *70351976271960
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70180336113640 !ruby/object:Gem::Requirement
27
+ requirement: &70351976271340 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70180336113640
35
+ version_requirements: *70351976271340
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec-rails
38
- requirement: &70180336113120 !ruby/object:Gem::Requirement
38
+ requirement: &70351976270520 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70180336113120
46
+ version_requirements: *70351976270520
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: capybara
49
- requirement: &70180336112420 !ruby/object:Gem::Requirement
49
+ requirement: &70351976270000 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70180336112420
57
+ version_requirements: *70351976270000
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: pry
60
- requirement: &70180336111800 !ruby/object:Gem::Requirement
60
+ requirement: &70351976269480 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70180336111800
68
+ version_requirements: *70351976269480
69
69
  description:
70
70
  email: moonmaster9000@gmail.com
71
71
  executables: []