frill 0.1.3 → 0.1.4

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -1,6 +1,5 @@
1
1
  module ViewContextFrill
2
2
  include Frill
3
- first
4
3
 
5
4
  def self.frill? object, controller
6
5
  object.class_eval do
data/lib/frill/frill.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  module Frill
2
2
  def self.included(base)
3
- self.decorators << base
3
+ self.decorators.add base
4
4
  base.extend ClassMethods
5
5
  end
6
6
 
7
7
  def self.decorators
8
- @decorators ||= []
8
+ @decorators ||= Graph.new
9
9
  end
10
10
 
11
11
  def self.reset!
@@ -13,7 +13,7 @@ module Frill
13
13
  end
14
14
 
15
15
  def self.decorate object, context
16
- decorators.each do |d|
16
+ decorators.sorted_nodes.each do |d|
17
17
  object.extend d if d.frill? object, context
18
18
  end
19
19
 
@@ -22,22 +22,132 @@ module Frill
22
22
 
23
23
  module ClassMethods
24
24
  def before decorator
25
- move Frill.decorators.index(decorator)
25
+ Frill.decorators.move_before self, decorator
26
26
  end
27
27
 
28
28
  def after decorator
29
- move(Frill.decorators.index(decorator) + 1)
29
+ Frill.decorators.move_after self, decorator
30
30
  end
31
+ end
32
+
33
+ class Graph
34
+ def initialize
35
+ @nodes = {}
36
+ end
37
+
38
+ def add label
39
+ @nodes[label] ||= Node.new label
40
+ end
41
+
42
+ def move_before label1, label2
43
+ node1 = add label1
44
+ node2 = add label2
45
+
46
+ node1.move_before node2
47
+ end
48
+
49
+ def move_after label1, label2
50
+ node1 = add label1
51
+ node2 = add label2
52
+
53
+ node1.move_after node2
54
+ end
55
+
56
+ def [](label)
57
+ @nodes[label]
58
+ end
59
+
60
+ def empty?
61
+ @nodes.empty?
62
+ end
63
+
64
+ def include? label
65
+ @nodes.keys.include? label
66
+ end
67
+
68
+ def index label
69
+ sorted_nodes.index label
70
+ end
71
+
72
+ def sorted_nodes
73
+ lists = []
74
+
75
+ @nodes.values.each do |node|
76
+ unless lists.include? node.label
77
+ leaf = node.leaf
78
+ lists += leaf.to_a
79
+ end
80
+ end
31
81
 
32
- def first
33
- move 0
82
+ lists
34
83
  end
35
84
 
36
- private
37
- def move index
38
- Frill.decorators.delete self
39
- Frill.decorators.insert index, self
40
- Frill.decorators.compact!
85
+ class Node
86
+ attr_accessor :parent, :child
87
+ attr_reader :label
88
+
89
+ def initialize(label)
90
+ @label = label
91
+ @parent = nil
92
+ @child = nil
93
+ end
94
+
95
+ def move_before node
96
+ parent_node = node.leaf
97
+
98
+ parent_node.child = self
99
+ self.parent = parent_node
100
+ end
101
+
102
+ def move_after node
103
+ child_node = node.root
104
+
105
+ self.child = child_node
106
+ child.parent = self
107
+ end
108
+
109
+ def leaf
110
+ node = nil
111
+ current_node = self
112
+
113
+ until node
114
+ if current_node.child
115
+ current_node = current_node.child
116
+ else
117
+ node = current_node
118
+ end
119
+ end
120
+
121
+ node
122
+ end
123
+
124
+ def root
125
+ current_node = self
126
+ root_node = nil
127
+
128
+ until root_node
129
+ if current_node.parent
130
+ current_node = current_node.parent
131
+ else
132
+ root_node = current_node
133
+ end
134
+ end
135
+
136
+ root_node
137
+ end
138
+
139
+ def to_a
140
+ current_node = self
141
+
142
+ list = []
143
+
144
+ until current_node == nil
145
+ list << current_node.label
146
+ current_node = current_node.parent
147
+ end
148
+
149
+ list
150
+ end
41
151
  end
42
152
  end
43
153
  end
data/readme.markdown CHANGED
@@ -112,9 +112,11 @@ end
112
112
 
113
113
  Two things to note: the `HtmlTimestampFrill` is only applicable to
114
114
  objects that have timestamps _when presented in "html"_. Also, we
115
- tell `Frill` to decorate after `TimestampFrill` is applied (so that
115
+ tell `Frill` to decorate `after` `TimestampFrill` is applied (so that
116
116
  `super` in `created_at` returns our `TimestampFrill` response).
117
117
 
118
+ Note that you can also specify decoration dependencies with `before` instead of `after`.
119
+
118
120
  Next, in our controller, we need to decorate our objects with frills:
119
121
 
120
122
  ```ruby
data/spec/frill_spec.rb CHANGED
@@ -7,7 +7,7 @@ describe Frill do
7
7
 
8
8
  describe ".reset" do
9
9
  before do
10
- Frill.decorators << Module.new
10
+ Module.new { include Frill }
11
11
  Frill.decorators.should_not be_empty
12
12
  end
13
13
 
@@ -62,46 +62,82 @@ describe Frill do
62
62
 
63
63
  describe Frill::ClassMethods do
64
64
  before do
65
- module Module1
66
- include Frill
67
- end
65
+ module Module2; include Frill; end
66
+ module Module1; include Frill; end
67
+ module Module5; include Frill; end
68
+ module Module4; include Frill; end
69
+ module Module3; include Frill; end
70
+ end
68
71
 
69
- module Module2
70
- include Frill
71
- end
72
+ describe ".before" do
73
+ it "inserts the current module before the requested module in Frill's list of decorators" do
74
+ Module5.before Module4
75
+ Module4.before Module3
76
+ Module3.before Module2
77
+ Module2.before Module1
72
78
 
73
- module Module3
74
- include Frill
79
+ Frill.decorators.index(Module5).should be < Frill.decorators.index(Module4)
80
+ Frill.decorators.index(Module3).should be < Frill.decorators.index(Module2)
81
+ Frill.decorators.index(Module2).should be < Frill.decorators.index(Module1)
82
+ Frill.decorators.index(Module4).should be < Frill.decorators.index(Module3)
75
83
  end
84
+ end
85
+ end
86
+
87
+ describe Frill::ClassMethods do
88
+ before do
89
+ module Module1; include Frill; end
90
+ module Module2; include Frill; end
91
+ module Module3; include Frill; end
76
92
  end
77
93
 
78
94
  describe ".before" do
79
95
  it "inserts the current module before the requested module in Frill's list of decorators" do
80
- Frill.decorators.should == [Module1, Module2, Module3]
96
+ Frill.decorators.sorted_nodes.should == [Module1, Module2, Module3]
97
+
98
+ Module1.before Module2
99
+ Frill.decorators.sorted_nodes.should == [Module1, Module2, Module3]
81
100
 
82
- Module2.before Module1
83
101
  Module3.before Module2
84
- Frill.decorators.should == [Module3, Module2, Module1]
102
+ Frill.decorators.sorted_nodes.should == [Module3, Module1, Module2]
85
103
  end
86
104
  end
87
105
 
88
106
  describe ".after" do
89
107
  it "inserts the current module after the requested module in Frill's list of decorators" do
90
- Frill.decorators.should == [Module1, Module2, Module3]
108
+ Frill.decorators.sorted_nodes.should == [Module1, Module2, Module3]
91
109
 
92
110
  Module1.after Module2
93
111
  Module3.after Module2
94
- Frill.decorators.first.should == Module2
95
- Frill.decorators.last(2).should =~ [Module1, Module3]
112
+
113
+ Frill.decorators.sorted_nodes.first.should == Module2
114
+ Frill.decorators.sorted_nodes.last(2).should =~ [Module1, Module3]
96
115
  end
97
116
  end
117
+ end
98
118
 
99
- describe ".first" do
100
- it "inserts the current module at the beginning of the list of Frill's decorators" do
101
- Frill.decorators.should == [Module1, Module2, Module3]
119
+ describe Frill::Graph do
120
+ describe "#add" do
121
+ it "should add an element to the graph" do
122
+ g = Frill::Graph.new
123
+ g.add "hi"
124
+ g["hi"].should_not be_nil
125
+ end
126
+ end
127
+
128
+ describe "#move_before(label1, label2)" do
129
+ it "should move label1 before label2" do
130
+ g = Frill::Graph.new
131
+ g.move_before "a", "b"
132
+ g.sorted_nodes.should == ["a", "b"]
133
+ end
134
+ end
102
135
 
103
- Module2.first
104
- Frill.decorators.first.should == Module2
136
+ describe "#move_after(label1, label2)" do
137
+ it "should move label1 after label2" do
138
+ g = Frill::Graph.new
139
+ g.move_after "a", "b"
140
+ g.sorted_nodes.should == ["b", "a"]
105
141
  end
106
142
  end
107
143
  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.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-08-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70329707866140 !ruby/object:Gem::Requirement
16
+ requirement: &70132147962560 !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: *70329707866140
24
+ version_requirements: *70132147962560
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70329707865560 !ruby/object:Gem::Requirement
27
+ requirement: &70132147962000 !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: *70329707865560
35
+ version_requirements: *70132147962000
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec-rails
38
- requirement: &70329707864700 !ruby/object:Gem::Requirement
38
+ requirement: &70132147961140 !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: *70329707864700
46
+ version_requirements: *70132147961140
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: capybara
49
- requirement: &70329707864140 !ruby/object:Gem::Requirement
49
+ requirement: &70132147960500 !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: *70329707864140
57
+ version_requirements: *70132147960500
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: pry
60
- requirement: &70329707863500 !ruby/object:Gem::Requirement
60
+ requirement: &70132147959960 !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: *70329707863500
68
+ version_requirements: *70132147959960
69
69
  description:
70
70
  email: moonmaster9000@gmail.com
71
71
  executables: []