activesupport-decorators 0.0.2 → 0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -4
- data/lib/active_support_decorators/active_support_decorators.rb +19 -4
- data/lib/active_support_decorators/dependencies_patch.rb +7 -1
- data/lib/active_support_decorators/graph.rb +48 -0
- data/lib/active_support_decorators/version.rb +1 -1
- data/lib/activesupport-decorators.rb +1 -1
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec2236b08c2f672564e38201de862f16d36077b3
|
4
|
+
data.tar.gz: a0d82c89cd824d7699eafc8114d6ad5aa28b9483
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0cf349af37ab49b04a3963a0e439dadbe008494ad8aad5f2b1d344959594153811d8c52d304e69cf2118675a73383d591517d342a86a7a939c49f93553090299
|
7
|
+
data.tar.gz: 19691a0734337d8c0ff8cebe4b027be8b2154e7c55dc6d36c11d285393e87fc3ca1d0aac48ceb950b5b0299a6635133f0a7fb42d829ba8fe8fd9a280cf0f82ae
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@ the decorator pattern, you need to load the decorator after the original file ha
|
|
6
6
|
class in a Rails application, ActiveSupport will only load the first file it finds that matches the class name. This
|
7
7
|
means that you will need to manually load the additional (decorator) file. Usually you don't want to want to introduce
|
8
8
|
hard dependencies such as require statements. You also don't want to preload a bunch of classes in a Rails initializer.
|
9
|
-
This
|
9
|
+
This is a tiny gem that provides you with a simple way to specify load dependencies.
|
10
10
|
|
11
11
|
Example
|
12
12
|
=======
|
@@ -27,14 +27,14 @@ class Pet
|
|
27
27
|
end
|
28
28
|
```
|
29
29
|
|
30
|
-
|
30
|
+
Now tell ActiveSupportDecorators to load any matching file in my_engine/app/models when a file is loaded from
|
31
31
|
app/models. A convenient place to do this is in a Rails initializer in the engine:
|
32
32
|
|
33
33
|
```Ruby
|
34
34
|
module MyEngine
|
35
35
|
module Rails
|
36
36
|
class Engine < ::Rails::Engine
|
37
|
-
initializer :
|
37
|
+
initializer :decorator_dependencies do |app|
|
38
38
|
ActiveSupportDecorators.add_dependency("#{app.root}/app/models", "#{config.root}/app/models")
|
39
39
|
end
|
40
40
|
end
|
@@ -42,4 +42,13 @@ module MyEngine
|
|
42
42
|
end
|
43
43
|
```
|
44
44
|
|
45
|
-
Note that you could specify the path as "/app" to
|
45
|
+
Note that you could specify the path as "/app" to allow decorating controllers, models, helpers, etc.
|
46
|
+
|
47
|
+
Debugging
|
48
|
+
=========
|
49
|
+
|
50
|
+
Need to know which decorator files are loaded? Enable debug output:
|
51
|
+
|
52
|
+
```Ruby
|
53
|
+
ActiveSupportDecorators.debug = true
|
54
|
+
```
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support_decorators/graph'
|
2
|
+
|
1
3
|
module ActiveSupportDecorators
|
2
4
|
def self.dependencies
|
3
5
|
@dependencies ||= {}
|
@@ -20,8 +22,10 @@ module ActiveSupportDecorators
|
|
20
22
|
end
|
21
23
|
|
22
24
|
def self.load_path_order(file_name)
|
23
|
-
|
25
|
+
graph = Graph.new
|
26
|
+
graph.add(file_name)
|
24
27
|
|
28
|
+
# If an attempt is made to load the original file, ensure the decorators are loaded afterwards.
|
25
29
|
dependencies.each do |path, decorator_paths|
|
26
30
|
if file_name.starts_with?(path)
|
27
31
|
relative_name = file_name.gsub(path, '')
|
@@ -30,13 +34,24 @@ module ActiveSupportDecorators
|
|
30
34
|
decorator_file = "#{decorator_path}#{relative_name}"
|
31
35
|
|
32
36
|
if File.file?(decorator_file) || File.file?(decorator_file + '.rb')
|
33
|
-
|
34
|
-
|
37
|
+
graph.add_dependency(file_name, decorator_file)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# If an attempt is made to load a decorator file, ensure the original file is loaded first.
|
43
|
+
decorator_paths.each do |decorator_path|
|
44
|
+
if file_name.starts_with?(decorator_path)
|
45
|
+
relative_name = file_name.gsub(decorator_path, '')
|
46
|
+
decorated_file = "#{path}#{relative_name}"
|
47
|
+
|
48
|
+
if File.file?(decorated_file) || File.file?(decorated_file + '.rb')
|
49
|
+
graph.add_dependency(decorated_file, file_name)
|
35
50
|
end
|
36
51
|
end
|
37
52
|
end
|
38
53
|
end
|
39
54
|
|
40
|
-
|
55
|
+
graph.resolve_object_order
|
41
56
|
end
|
42
57
|
end
|
@@ -4,7 +4,13 @@ module ActiveSupport::Dependencies
|
|
4
4
|
alias_method :require_or_load_without_multiple, :require_or_load
|
5
5
|
|
6
6
|
def require_or_load(file_name, const_path = nil)
|
7
|
-
ActiveSupportDecorators.load_path_order(file_name)
|
7
|
+
order = ActiveSupportDecorators.load_path_order(file_name)
|
8
|
+
|
9
|
+
if ActiveSupportDecorators.debug && order.size > 1
|
10
|
+
Rails.try(:logger).try(:debug, "ActiveSupportDecorators: Loading files in order #{order.join(', ')}.")
|
11
|
+
end
|
12
|
+
|
13
|
+
order.each do |path|
|
8
14
|
require_or_load_without_multiple(path, const_path)
|
9
15
|
end
|
10
16
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
class Graph
|
2
|
+
Node = Struct.new(:object) do
|
3
|
+
def depends_on
|
4
|
+
@depends_on ||= []
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@nodes = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def resolve_object_order
|
13
|
+
result = []
|
14
|
+
|
15
|
+
until @nodes.empty?
|
16
|
+
nodes_without_dependencies = @nodes.select { |n| n.depends_on.empty? }
|
17
|
+
result += nodes_without_dependencies.map { |n| n.object }.sort
|
18
|
+
|
19
|
+
nodes_without_dependencies.each do |to_delete|
|
20
|
+
@nodes.delete(to_delete)
|
21
|
+
@nodes.each { |n| n.depends_on.delete(to_delete) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
result
|
26
|
+
end
|
27
|
+
|
28
|
+
def add(object)
|
29
|
+
find_or_add_node(object)
|
30
|
+
end
|
31
|
+
|
32
|
+
def add_dependency(from_object, to_object)
|
33
|
+
raise 'Objects are identical' if from_object == to_object
|
34
|
+
from_node = find_or_add_node(from_object)
|
35
|
+
to_node = find_or_add_node(to_object)
|
36
|
+
to_node.depends_on << from_node
|
37
|
+
raise 'EMPTY' if from_node.nil?
|
38
|
+
end
|
39
|
+
|
40
|
+
def find_or_add_node(object)
|
41
|
+
node = @nodes.find { |n| n.object == object }
|
42
|
+
unless node
|
43
|
+
node = Node.new(object)
|
44
|
+
@nodes.push(node)
|
45
|
+
end
|
46
|
+
node
|
47
|
+
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activesupport-decorators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.9'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pierre Pretorius
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4.0'
|
27
27
|
description: Useful when extending functionality with Rails engines.
|
@@ -31,12 +31,13 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
34
36
|
- lib/active_support_decorators/active_support_decorators.rb
|
35
37
|
- lib/active_support_decorators/dependencies_patch.rb
|
38
|
+
- lib/active_support_decorators/graph.rb
|
36
39
|
- lib/active_support_decorators/version.rb
|
37
40
|
- lib/activesupport-decorators.rb
|
38
|
-
- Rakefile
|
39
|
-
- README.md
|
40
41
|
homepage: https://github.com/pierre-pretorius/activesupport-decorators
|
41
42
|
licenses:
|
42
43
|
- MIT
|
@@ -47,19 +48,18 @@ require_paths:
|
|
47
48
|
- lib
|
48
49
|
required_ruby_version: !ruby/object:Gem::Requirement
|
49
50
|
requirements:
|
50
|
-
- -
|
51
|
+
- - ">="
|
51
52
|
- !ruby/object:Gem::Version
|
52
53
|
version: '0'
|
53
54
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
55
|
requirements:
|
55
|
-
- -
|
56
|
+
- - ">="
|
56
57
|
- !ruby/object:Gem::Version
|
57
58
|
version: '0'
|
58
59
|
requirements: []
|
59
60
|
rubyforge_project:
|
60
|
-
rubygems_version: 2.0
|
61
|
+
rubygems_version: 2.2.0
|
61
62
|
signing_key:
|
62
63
|
specification_version: 4
|
63
64
|
summary: Adds the decorator pattern to activesupport class loading.
|
64
65
|
test_files: []
|
65
|
-
has_rdoc:
|