cells-dependencies 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cell-dependencies.gemspec
4
+ gemspec
@@ -0,0 +1,33 @@
1
+
2
+ # Cell Dependencies
3
+
4
+ This is little gemmy what let's you declare ahead of render-time
5
+ what other cells a cell depends on.
6
+
7
+ Let's say you've got a hot banner that you put in all of your widgets
8
+
9
+ class BannerCell < Cell::Rails
10
+ #... your banner is awesome, and does great stuff.
11
+ end
12
+
13
+ You can now declare in your other cells that they use this banner cell
14
+
15
+ class WeatherCell < Cell::Rails
16
+ uses :banner
17
+ #other great stuff...
18
+ end
19
+
20
+ and then query your cell to find out which cells it depends on
21
+
22
+ WeatherCell.dependencies #=> [BannerCell]
23
+
24
+ Dependencies are transitive
25
+
26
+ class BannerCell < Cell::Rails
27
+ uses :logo
28
+ end
29
+
30
+ WeatherCell.dependencies #=> [BannerCell, LogoCell]
31
+
32
+ # Why is this useful?
33
+
@@ -0,0 +1,5 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cell/dependencies/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cells-dependencies"
7
+ s.version = Cell::Dependencies::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Charles Lowell"]
10
+ s.email = ["cowboyd@thefrontside.net"]
11
+ s.homepage = "http://github.com/cowboyd/cells-dependencies"
12
+ s.summary = "Say which cells depend on other cells"
13
+ s.description = "Sometimes its useful to know which cells depend on each other before you actually render them"
14
+
15
+ s.rubyforge_project = "cells-dependencies"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "cells", ">= 3.5.0"
23
+
24
+ s.add_development_dependency "rspec", ">= 2.0.0"
25
+ end
@@ -0,0 +1,49 @@
1
+ require 'cells'
2
+ require 'cell/dependencies/version'
3
+ require 'active_support/core_ext/class/subclasses'
4
+
5
+ module Cell
6
+ module Dependencies
7
+
8
+ def inherited(cls)
9
+ super
10
+ cls.extend(Cell::Dependencies)
11
+ end
12
+
13
+ def uses(*names)
14
+ @dependencies ||= []
15
+ @dependencies += names
16
+ end
17
+
18
+ def dependencies(seen = Set.new([self]))
19
+ @dependencies ||= []
20
+ resolved = @dependencies.map {|n| Cell::Base.class_from_cell_name(n)}
21
+ resolved.inject(resolved) do |all, dep|
22
+ if seen.include?(dep)
23
+ all
24
+ else
25
+ seen << dep
26
+ dep.dependencies(seen) - [self] + all
27
+ end
28
+ end
29
+ end
30
+
31
+ module TotalOrder
32
+ def total_order
33
+ seen = Set.new
34
+ order = []
35
+ cells = Cell::Base.descendants.sort_by {|cell| cell.cell_name}
36
+ cells.each do |cell|
37
+ next if seen.include?(cell)
38
+ order += cell.dependencies.select {|dep| !seen.include?(dep)} + [cell]
39
+ seen += order
40
+ end
41
+ return order.uniq
42
+ end
43
+ end
44
+
45
+ Cell::Rails.extend(self)
46
+ Cell::Rails.extend(TotalOrder)
47
+ end
48
+
49
+ end
@@ -0,0 +1,5 @@
1
+ module Cell
2
+ module Dependencies
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cell::Dependencies do
4
+
5
+ before do
6
+ @cells = {}
7
+ Cell::Base.stub(:class_from_cell_name) do |name|
8
+ @cells[name.to_s] or raise "no such cell #{name}"
9
+ end
10
+ end
11
+
12
+ it "starts out empty" do
13
+ mock_cell(:A).dependencies.should == []
14
+ end
15
+
16
+ it "can enumerate dependencies" do
17
+ a = mock_cell(:A)
18
+ b = mock_cell(:B) do
19
+ uses :A
20
+ end
21
+ c = mock_cell(:C) do
22
+ uses :B
23
+ end
24
+ c.dependencies.should == [a,b]
25
+ end
26
+
27
+ it "can order all cells in order of dependencies" do
28
+ a = mock_cell(:A) do
29
+ uses :B, :G
30
+ end
31
+ b = mock_cell(:B) do
32
+ uses :C
33
+ end
34
+ c = mock_cell(:C)
35
+ d = mock_cell(:D)
36
+ f = mock_cell(:F)
37
+ g = mock_cell(:G)
38
+ h = mock_cell(:H) do
39
+ uses :A,:D
40
+ end
41
+ i = mock_cell(:I) do
42
+ uses :H
43
+ end
44
+ Cell::Base.stub(:descendants).and_return(@cells.values)
45
+ Cell::Rails.total_order.should == [c,b,g,a,d,f,h,i]
46
+ end
47
+
48
+ def mock_cell(name, &body)
49
+ Class.new(Cell::Rails).tap do |cell|
50
+ mc = class << cell;self;end
51
+ mc.send(:define_method, :to_s) do
52
+ "#{name}"
53
+ end
54
+ mc.send(:define_method, :cell_name) do
55
+ "#{name}"
56
+ end
57
+ @cells[name.to_s] = cell
58
+ def cell.view_paths
59
+ ["/mock/cells"]
60
+ end
61
+ cell.class_eval(&body) if block_given?
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+
2
+ require 'bundler/setup'
3
+ require 'action_controller'
4
+ require 'rails'
5
+ require 'cell/dependencies'
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cells-dependencies
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Charles Lowell
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-23 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: cells
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 3
32
+ - 5
33
+ - 0
34
+ version: 3.5.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 15
46
+ segments:
47
+ - 2
48
+ - 0
49
+ - 0
50
+ version: 2.0.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: Sometimes its useful to know which cells depend on each other before you actually render them
54
+ email:
55
+ - cowboyd@thefrontside.net
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files: []
61
+
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - cells-dependencies.gemspec
68
+ - lib/cell/dependencies.rb
69
+ - lib/cell/dependencies/version.rb
70
+ - spec/cell/dependencies_spec.rb
71
+ - spec/spec_helper.rb
72
+ has_rdoc: true
73
+ homepage: http://github.com/cowboyd/cells-dependencies
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options: []
78
+
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project: cells-dependencies
102
+ rubygems_version: 1.5.2
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Say which cells depend on other cells
106
+ test_files:
107
+ - spec/cell/dependencies_spec.rb
108
+ - spec/spec_helper.rb