contextualize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "mixology", "~> 0.2.0"
4
+ gem "activesupport", ">= 3.0.1"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", ">= 2.3.0"
10
+ gem "bundler", "~> 1.0.1"
11
+ gem "jeweler", "~> 1.6.3"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.0.9)
5
+ diff-lcs (1.1.2)
6
+ git (1.2.5)
7
+ jeweler (1.6.3)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ mixology (0.2.0)
12
+ rake (0.9.2)
13
+ rcov (0.9.9)
14
+ rspec (2.6.0)
15
+ rspec-core (~> 2.6.0)
16
+ rspec-expectations (~> 2.6.0)
17
+ rspec-mocks (~> 2.6.0)
18
+ rspec-core (2.6.4)
19
+ rspec-expectations (2.6.0)
20
+ diff-lcs (~> 1.1.2)
21
+ rspec-mocks (2.6.0)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ activesupport (>= 3.0.1)
28
+ bundler (~> 1.0.1)
29
+ jeweler (~> 1.6.3)
30
+ mixology (~> 0.2.0)
31
+ rcov
32
+ rspec (>= 2.3.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Kristian Mandrup
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,73 @@
1
+ = contextualize
2
+
3
+ Adds the ability to add and remove specific modules to an object depending on the current context
4
+ This can fx be used for Models in a MVC pattern, where the model could have different behavior
5
+ depending on the context it operates in, fx if is currently accessed in a View or a Controller, or
6
+ even depending on the scope (fx Admin) etc. This way, a lot of the typical scenarios for helper methods
7
+ can be avoided, and instead you simply create scope folders for the model, typically one for view and
8
+ possibly one for controller
9
+
10
+ + models
11
+ project
12
+ + contextualize
13
+ project_view.rb
14
+ project_control.rb
15
+ + admin
16
+ project_view.rb
17
+
18
+ module Contextualize::ProjectView
19
+ def full_name
20
+ end
21
+ end
22
+
23
+ module Contextualize::ProjectControl
24
+ def best
25
+ end
26
+ end
27
+
28
+
29
+ class Project
30
+ contextualize
31
+ icontexts :view, :control # using naming conventions
32
+ icontext :admin, Contextualize::Admin::ProjectView
33
+ end
34
+
35
+ project = Project.new
36
+
37
+ # add :view context
38
+ project.add_icontext :view
39
+
40
+ # use the view context methods now available
41
+ project.own_methods.should include('view')
42
+ project.view.should == "view"
43
+
44
+ # remove the :view context
45
+ project.remove_icontext :view
46
+
47
+ # the methods of the view context are no longer available!
48
+ lambda {project.view}.should raise_error
49
+
50
+ # operate on object within one or more contexts
51
+ project.icontext_scope :view do |project|
52
+ project.view.should == "view"
53
+ end
54
+
55
+ # contexts are automatically removed from object when block terminates
56
+ lambda {project.view}.should raise_error
57
+
58
+
59
+ == Contributing to contextualize
60
+
61
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
62
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
63
+ * Fork the project
64
+ * Start a feature/bugfix branch
65
+ * Commit and push until you are happy with your contribution
66
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
67
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
68
+
69
+ == Copyright
70
+
71
+ Copyright (c) 2011 Kristian Mandrup. See LICENSE.txt for
72
+ further details.
73
+
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "contextualize"
18
+ gem.homepage = "http://github.com/kristianmandrup/contextualize"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Contextual behaviors for objects for use with Rails models and many other scenarios}
21
+ gem.description = %Q{Add and remove behaviors defined in modules depending on context an object operates in}
22
+ gem.email = "kmandrup@gmail.com"
23
+ gem.authors = ["Kristian Mandrup"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "contextualize #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,68 @@
1
+ require 'mixology'
2
+ require 'active_support/inflector'
3
+ require 'contextualize/core_ext'
4
+
5
+ module Contextualize
6
+
7
+ module ClassMethods
8
+ attr_reader :icontext_map
9
+
10
+ def icontext name, *constants
11
+ @icontext_map ||= {}
12
+ @icontext_map[name.to_sym] = select_modules constants
13
+ end
14
+
15
+ def icontexts *names
16
+ names.flatten.each do |name|
17
+ icontext name, const_by_convention(name)
18
+ end
19
+ end
20
+
21
+ protected
22
+
23
+ def const_by_convention name
24
+ cls_name = self.name.demodulize
25
+ const_name = "Contextualize::#{cls_name}#{name.to_s.camelize}"
26
+ const_name.constantize
27
+ end
28
+
29
+ def select_modules constants
30
+ constants.select {|c| c.is_a?(Module) && !c.respond_to?(:new) }
31
+ end
32
+ end
33
+
34
+ def self.included base
35
+ base.extend ClassMethods
36
+ end
37
+
38
+ def icontext_map
39
+ self.class.icontext_map || {}
40
+ end
41
+
42
+ def add_icontexts *names
43
+ names.each {|name| add_icontext(name) }
44
+ end
45
+
46
+ def remove_icontexts *names
47
+ names.each {|name| remove_icontext(name) }
48
+ end
49
+
50
+ def add_icontext name
51
+ icontext(name).each do |const|
52
+ self.send :mixin, const
53
+ end
54
+ end
55
+
56
+ def remove_icontext name
57
+ return if !icontext(name) || icontext(name).empty?
58
+ icontext(name).each do |const|
59
+ self.send :unmix, const
60
+ end
61
+ end
62
+
63
+ protected
64
+
65
+ def icontext name
66
+ icontext_map[name.to_sym] || []
67
+ end
68
+ end
@@ -0,0 +1,23 @@
1
+ class Object
2
+ def own_methods
3
+ methods - Object.methods
4
+ end
5
+
6
+ def icontext_scope *names, &block
7
+ self.add_icontexts *names
8
+ yield self if block
9
+ self.remove_icontexts *names
10
+ end
11
+ end
12
+
13
+ class Module
14
+ def contextualize
15
+ self.send :include, Contextualize
16
+ end
17
+
18
+ def own_methods
19
+ instance_methods - Object.instance_methods
20
+ end
21
+ end
22
+
23
+
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ module ProjectView
4
+ def view
5
+ "view"
6
+ end
7
+ end
8
+
9
+ module DetailedView
10
+ def detailed_view
11
+ "detailed_view"
12
+ end
13
+ end
14
+
15
+ module ProjectControl
16
+ def control
17
+ "control"
18
+ end
19
+ end
20
+
21
+
22
+
23
+ class Project
24
+ contextualize
25
+
26
+ icontext :view, ProjectView, DetailedView
27
+ icontext :control, ProjectControl
28
+ end
29
+
30
+ module Contextualize
31
+ module EventView
32
+ def view
33
+ "view"
34
+ end
35
+ end
36
+
37
+ module EventControl
38
+ def control
39
+ "control"
40
+ end
41
+ end
42
+ end
43
+
44
+ class Event
45
+ contextualize
46
+
47
+ icontexts :view, :control
48
+ end
49
+
50
+ describe "Contextualize" do
51
+ let (:project) do
52
+ Project.new
53
+ end
54
+
55
+ it "can add a context to an object" do
56
+ project.add_icontext :view
57
+ project.own_methods.should include('view')
58
+
59
+ project.view.should == "view"
60
+ project.detailed_view.should == "detailed_view"
61
+ end
62
+
63
+ it "can remove a context from an object" do
64
+ project.add_icontext :view
65
+ project.own_methods.should include('view')
66
+
67
+ project.remove_icontext :view
68
+ lambda {project.view}.should raise_error
69
+ lambda {project.detailed_view}.should raise_error
70
+ end
71
+
72
+ it 'can operate in a context scope' do
73
+ project.icontext_scope :view, :control do |project|
74
+ project.view.should == "view"
75
+ project.control.should == "control"
76
+ end
77
+ lambda {project.view}.should raise_error
78
+ lambda {project.control}.should raise_error
79
+ end
80
+
81
+ describe 'naming conventions' do
82
+ let (:event) do
83
+ Event.new
84
+ end
85
+
86
+ it 'should apply naming conventions' do
87
+ event.add_icontext :view
88
+ event.own_methods.should include('view')
89
+
90
+ event.view.should == "view"
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ require 'contextualize'
3
+
4
+ RSpec.configure do |config|
5
+
6
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: contextualize
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Kristian Mandrup
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-04 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ requirement: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ hash: 23
27
+ segments:
28
+ - 0
29
+ - 2
30
+ - 0
31
+ version: 0.2.0
32
+ version_requirements: *id001
33
+ type: :runtime
34
+ name: mixology
35
+ prerelease: false
36
+ - !ruby/object:Gem::Dependency
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 5
43
+ segments:
44
+ - 3
45
+ - 0
46
+ - 1
47
+ version: 3.0.1
48
+ version_requirements: *id002
49
+ type: :runtime
50
+ name: activesupport
51
+ prerelease: false
52
+ - !ruby/object:Gem::Dependency
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 2
61
+ - 3
62
+ - 0
63
+ version: 2.3.0
64
+ version_requirements: *id003
65
+ type: :development
66
+ name: rspec
67
+ prerelease: false
68
+ - !ruby/object:Gem::Dependency
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ hash: 21
75
+ segments:
76
+ - 1
77
+ - 0
78
+ - 1
79
+ version: 1.0.1
80
+ version_requirements: *id004
81
+ type: :development
82
+ name: bundler
83
+ prerelease: false
84
+ - !ruby/object:Gem::Dependency
85
+ requirement: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ~>
89
+ - !ruby/object:Gem::Version
90
+ hash: 9
91
+ segments:
92
+ - 1
93
+ - 6
94
+ - 3
95
+ version: 1.6.3
96
+ version_requirements: *id005
97
+ type: :development
98
+ name: jeweler
99
+ prerelease: false
100
+ - !ruby/object:Gem::Dependency
101
+ requirement: &id006 !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ version_requirements: *id006
111
+ type: :development
112
+ name: rcov
113
+ prerelease: false
114
+ description: Add and remove behaviors defined in modules depending on context an object operates in
115
+ email: kmandrup@gmail.com
116
+ executables: []
117
+
118
+ extensions: []
119
+
120
+ extra_rdoc_files:
121
+ - LICENSE.txt
122
+ - README.rdoc
123
+ files:
124
+ - .document
125
+ - .rspec
126
+ - Gemfile
127
+ - Gemfile.lock
128
+ - LICENSE.txt
129
+ - README.rdoc
130
+ - Rakefile
131
+ - VERSION
132
+ - lib/contextualize.rb
133
+ - lib/contextualize/core_ext.rb
134
+ - spec/contextualize_spec.rb
135
+ - spec/spec_helper.rb
136
+ homepage: http://github.com/kristianmandrup/contextualize
137
+ licenses:
138
+ - MIT
139
+ post_install_message:
140
+ rdoc_options: []
141
+
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ hash: 3
150
+ segments:
151
+ - 0
152
+ version: "0"
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ hash: 3
159
+ segments:
160
+ - 0
161
+ version: "0"
162
+ requirements: []
163
+
164
+ rubyforge_project:
165
+ rubygems_version: 1.8.5
166
+ signing_key:
167
+ specification_version: 3
168
+ summary: Contextual behaviors for objects for use with Rails models and many other scenarios
169
+ test_files: []
170
+