contextualize 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,6 +1,7 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem "mixology", "~> 0.2.0"
4
+ gem "i18n"
4
5
  gem "activesupport", ">= 3.0.1"
5
6
 
6
7
  # Add dependencies to develop your gem here.
@@ -4,6 +4,7 @@ GEM
4
4
  activesupport (3.0.9)
5
5
  diff-lcs (1.1.2)
6
6
  git (1.2.5)
7
+ i18n (0.6.0)
7
8
  jeweler (1.6.3)
8
9
  bundler (~> 1.0)
9
10
  git (>= 1.2.5)
@@ -26,6 +27,7 @@ PLATFORMS
26
27
  DEPENDENCIES
27
28
  activesupport (>= 3.0.1)
28
29
  bundler (~> 1.0.1)
30
+ i18n
29
31
  jeweler (~> 1.6.3)
30
32
  mixology (~> 0.2.0)
31
33
  rcov
@@ -1,27 +1,18 @@
1
- h1. contextualize
1
+ h1. Contextualize
2
2
 
3
3
  Adds the ability to add and remove specific modules to an object depending on the current context
4
4
  This can fx be used for Models in a _MVC_ pattern, where the model could have different behavior
5
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.
6
+ even depending on the scope (fx Admin) etc.
9
7
 
10
- "Decorator Pattern with Mixology":http://ruby.simapse.com/2008/08/test.html
8
+ This way, a lot of the typical scenarios for helper methods can be avoided. Instead you simply create
9
+ an 'app/contexts' folder for modules that define contextual behavior for the models. You can further
10
+ subdivision these contexts as needed. This can even be used to redefine the logic of a method depending
11
+ on the context it operates in.
11
12
 
12
- In a Rails project, you should use the following structure
13
+ The "Decorator Pattern with Mixology":http://ruby.simapse.com/2008/08/test.html has been used to great effect.
13
14
 
14
- <pre>
15
- + app
16
- + contexts
17
- project_view.rb
18
- project_control.rb
19
- + admin
20
- project_view.rb
21
-
22
- + models
23
- project.rb
24
- </pre>
15
+ h2. Basic Usage
25
16
 
26
17
  Define each context specific module
27
18
 
@@ -46,6 +37,13 @@ module Admin
46
37
  end
47
38
  end
48
39
  end
40
+
41
+ # Only in order to faciliatet demonstrating results below!
42
+ class Object
43
+ def own_methods
44
+ methods - Object.methods
45
+ end
46
+ end
49
47
  </pre>
50
48
 
51
49
  Then bind these modules to one or more contexts
@@ -53,8 +51,8 @@ Then bind these modules to one or more contexts
53
51
  <pre>
54
52
  class Project
55
53
  contextualize
56
- icontexts :view, :control # using naming conventions
57
- icontext :admin, Admin::ProjectControl
54
+ contexts :view, :control # using naming conventions
55
+ context :admin, Admin::ProjectControl
58
56
  end
59
57
  </pre>
60
58
 
@@ -64,68 +62,94 @@ Now you can operate on the object according to the context you are in
64
62
  project = Project.new
65
63
 
66
64
  # add :view context
67
- project.add_icontext :view
68
- project.add_icontexts :view, :control
65
+ project.add_context :view
66
+ project.add_contexts :view, :control
69
67
 
70
68
  # use the view context methods now available
71
69
  project.own_methods.should include('view')
72
70
  project.view.should == "view"
73
-
71
+
74
72
  # remove the :view context
75
- project.remove_icontext :view
76
- project.remove_icontexts :view, :control
73
+ project.remove_context :view
74
+ project.remove_contexts :view, :control
77
75
 
78
76
 
79
77
  # the methods of the view context are no longer available!
80
78
  lambda {project.view}.should raise_error
81
79
 
82
80
  # operate on object within one or more contexts
83
- project.icontext_scope :view do |project|
81
+ project.context_scope :view do |project|
84
82
  project.view.should == "view"
85
83
  end
86
84
 
87
85
  # contexts are automatically removed from object when block terminates
88
86
  lambda {project.view}.should raise_error
89
87
 
90
- project.icontext_scope :view, :control do |project|
88
+ project.context_scope :view, :control do |project|
91
89
  # ...
92
90
  end
93
91
  </pre>
94
92
 
95
93
  h2. Usage in Rails
96
94
 
95
+ in Gemfile
96
+
97
97
  <pre>
98
- class ProjectsController < ApplicationController
99
- # "simulated" exposure
100
- def project
101
- @project ||= begin
102
- p = params[:id] ? Project.find(params[:id]) : Project.new(params[:project])
103
- p.add_icontext :view
104
- end
105
- end
106
- helper_method project
107
- hide_action project
98
+ gem 'contextualize'
99
+ </pre>
108
100
 
109
- def projects
110
- @projects ||= Project.all.add_icontext :view
111
- end
112
- helper_method projects
113
- hide_action projects
101
+ In a Rails project, you should use the following file structure
114
102
 
115
- def index
116
- end
103
+ <pre>
104
+ + app
105
+ + contexts
106
+ project_view.rb
107
+ project_control.rb
108
+ + admin
109
+ project_view.rb
110
+
111
+ + models
112
+ project.rb
113
+ </pre>
117
114
 
118
- def show
115
+ <pre>
116
+ class ProjectsController < ApplicationController
117
+ # "simulated" exposure
118
+ def project
119
+ @project ||= begin
120
+ p = params[:id] ? Project.find(params[:id]) : Project.new(params[:project])
121
+ p.add_icontext :view
119
122
  end
120
-
121
- ...
122
- end
123
+ end
124
+ helper_method project
125
+ hide_action project
126
+
127
+ def projects
128
+ @projects ||= Project.all.add_icontext :view
129
+ end
130
+ helper_method projects
131
+ hide_action projects
132
+
133
+ def index
134
+ end
135
+
136
+ def show
137
+ end
138
+ ...
139
+ end
123
140
  </pre>
124
141
 
125
142
  h2. Using decent_exposure integration
126
143
 
127
144
  Contextualize monkey-patches the _#expose_ method of the _decent_exposure_ gem to add the icontext :view to each model (if present)
128
-
145
+
146
+ in Gemfile
147
+
148
+ <pre>
149
+ gem 'decent_exposure'
150
+ gem 'contextualize'
151
+ </pre>
152
+
129
153
  <pre>
130
154
  class ProjectsController < ApplicationController
131
155
  expose(:projects) { Project.all }
@@ -166,11 +190,10 @@ class ProjectsController < ApplicationController
166
190
  end
167
191
  </pre>
168
192
 
169
-
170
193
  Enjoy!
171
194
 
172
195
  h2. Contributing to contextualize
173
-
196
+
174
197
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
175
198
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
176
199
  * Fork the project
@@ -179,7 +202,7 @@ h2. Contributing to contextualize
179
202
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
180
203
  * 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.
181
204
 
182
- == Copyright
205
+ h2. Copyright
183
206
 
184
207
  Copyright (c) 2011 Kristian Mandrup. See LICENSE.txt for
185
208
  further details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{contextualize}
8
- s.version = "0.1.1"
7
+ s.name = "contextualize"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = [%q{Kristian Mandrup}]
12
- s.date = %q{2011-07-05}
13
- s.description = %q{Add and remove behaviors defined in modules depending on context an object operates in}
14
- s.email = %q{kmandrup@gmail.com}
11
+ s.authors = ["Kristian Mandrup"]
12
+ s.date = "2011-09-21"
13
+ s.description = "Add and remove behaviors defined in modules depending on context an object operates in"
14
+ s.email = "kmandrup@gmail.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
17
  "README.textile"
@@ -32,17 +32,18 @@ Gem::Specification.new do |s|
32
32
  "spec/contextualize_spec.rb",
33
33
  "spec/spec_helper.rb"
34
34
  ]
35
- s.homepage = %q{http://github.com/kristianmandrup/contextualize}
36
- s.licenses = [%q{MIT}]
37
- s.require_paths = [%q{lib}]
38
- s.rubygems_version = %q{1.8.5}
39
- s.summary = %q{Contextual behaviors for objects for use with Rails models and many other scenarios}
35
+ s.homepage = "http://github.com/kristianmandrup/contextualize"
36
+ s.licenses = ["MIT"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = "1.8.10"
39
+ s.summary = "Contextual behaviors for objects for use with Rails models and many other scenarios"
40
40
 
41
41
  if s.respond_to? :specification_version then
42
42
  s.specification_version = 3
43
43
 
44
44
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
45
  s.add_runtime_dependency(%q<mixology>, ["~> 0.2.0"])
46
+ s.add_runtime_dependency(%q<i18n>, [">= 0"])
46
47
  s.add_runtime_dependency(%q<activesupport>, [">= 3.0.1"])
47
48
  s.add_development_dependency(%q<rspec>, [">= 2.3.0"])
48
49
  s.add_development_dependency(%q<bundler>, ["~> 1.0.1"])
@@ -50,6 +51,7 @@ Gem::Specification.new do |s|
50
51
  s.add_development_dependency(%q<rcov>, [">= 0"])
51
52
  else
52
53
  s.add_dependency(%q<mixology>, ["~> 0.2.0"])
54
+ s.add_dependency(%q<i18n>, [">= 0"])
53
55
  s.add_dependency(%q<activesupport>, [">= 3.0.1"])
54
56
  s.add_dependency(%q<rspec>, [">= 2.3.0"])
55
57
  s.add_dependency(%q<bundler>, ["~> 1.0.1"])
@@ -58,6 +60,7 @@ Gem::Specification.new do |s|
58
60
  end
59
61
  else
60
62
  s.add_dependency(%q<mixology>, ["~> 0.2.0"])
63
+ s.add_dependency(%q<i18n>, [">= 0"])
61
64
  s.add_dependency(%q<activesupport>, [">= 3.0.1"])
62
65
  s.add_dependency(%q<rspec>, [">= 2.3.0"])
63
66
  s.add_dependency(%q<bundler>, ["~> 1.0.1"])
@@ -5,21 +5,21 @@ require 'contextualize/decent_exposure'
5
5
 
6
6
  module Contextualize
7
7
  module ClassMethods
8
- attr_reader :icontext_map
8
+ attr_reader :context_map
9
9
 
10
- def icontext name, *constants
11
- @icontext_map ||= {}
10
+ def context name, *constants
11
+ @context_map ||= {}
12
12
  context_modules = if constants.flatten.empty?
13
- const_by_convention(name)
14
- else
13
+ const_by_convention(name)
14
+ else
15
15
  select_modules constants
16
16
  end
17
- @icontext_map[name.to_sym] = context_modules
17
+ @context_map[name.to_sym] = context_modules
18
18
  end
19
19
 
20
- def icontexts *names
20
+ def contexts *names
21
21
  names.flatten.each do |name|
22
- icontext name, const_by_convention(name)
22
+ context name, const_by_convention(name)
23
23
  end
24
24
  end
25
25
 
@@ -40,39 +40,43 @@ module Contextualize
40
40
  base.extend ClassMethods
41
41
  end
42
42
 
43
- def icontext_map
44
- self.class.icontext_map || {}
43
+ def context_map
44
+ self.class.context_map || {}
45
45
  end
46
46
 
47
- def add_icontexts *names
48
- names.each {|name| add_icontext(name) }
47
+ def add_contexts *names
48
+ names.each {|name| add_context(name) }
49
49
  self
50
50
  end
51
+ alias_method :enter_contexts, :add_contexts
51
52
 
52
- def remove_icontexts *names
53
- names.each {|name| remove_icontext(name) }
53
+ def remove_contexts *names
54
+ names.each {|name| remove_context(name) }
54
55
  self
55
56
  end
57
+ alias_method :exit_contexts, :remove_contexts
56
58
 
57
- def add_icontext name
58
- icontext(name).each do |const|
59
- self.send :mixin, const
59
+ def add_context name
60
+ context(name).each do |const|
61
+ self.send :mixin, const
60
62
  end
61
63
  self
62
64
  end
65
+ alias_method :enter_context, :add_context
63
66
 
64
- def remove_icontext name
65
- return if !icontext(name) || icontext(name).empty?
66
- icontext(name).each do |const|
67
+ def remove_context name
68
+ return if !context(name) || context(name).empty?
69
+ context(name).each do |const|
67
70
  self.send :unmix, const
68
71
  end
69
72
  self
70
73
  end
74
+ alias_method :exit_context, :remove_context
71
75
 
72
76
  protected
73
77
 
74
- def icontext name
75
- mods = icontext_map[name.to_sym] || []
78
+ def context name
79
+ mods = context_map[name.to_sym] || []
76
80
  [mods].flatten
77
- end
81
+ end
78
82
  end
@@ -1,22 +1,18 @@
1
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
2
+ def context_scope *names, &block
3
+ self.add_contexts *names
8
4
  yield self if block
9
- self.remove_icontexts *names
5
+ self.remove_contexts *names
10
6
  end
11
7
  end
12
8
 
13
9
  class Array
14
- def add_icontext name
15
- self.each {|item| item.add_icontext name }
10
+ def add_context name
11
+ self.each {|item| item.add_context name }
16
12
  end
17
13
 
18
- def remove_icontext name
19
- self.each {|item| item.remove_icontext name }
14
+ def remove_context name
15
+ self.each {|item| item.remove_context name }
20
16
  end
21
17
  end
22
18
 
@@ -24,10 +20,6 @@ class Module
24
20
  def contextualize
25
21
  self.send :include, Contextualize
26
22
  end
27
-
28
- def own_methods
29
- instance_methods - Object.instance_methods
30
- end
31
23
  end
32
24
 
33
25
 
@@ -8,13 +8,12 @@ module DecentExposure
8
8
  else
9
9
  instance_exec(name, &closured_exposure)
10
10
  end
11
- puts "expose contextualize"
12
- @_resources[name].add_icontext :view
11
+ @_resources[name].add_context :view
13
12
  end
14
13
  helper_method name
15
14
  hide_action name
16
15
  end
17
-
16
+
18
17
  alias_method :view_expose, :expose
19
18
  end
20
-
19
+
@@ -24,8 +24,8 @@ end
24
24
  class Project
25
25
  contextualize
26
26
 
27
- icontext :view, ProjectViewer, Admin::ProjectDetailedView
28
- icontext :control, ProjectControl
27
+ context :view, ProjectViewer, Admin::ProjectDetailedView
28
+ context :control, ProjectControl
29
29
  end
30
30
 
31
31
 
@@ -41,11 +41,16 @@ module EventControl
41
41
  end
42
42
  end
43
43
 
44
+ class Object
45
+ def own_methods
46
+ methods - Object.methods
47
+ end
48
+ end
44
49
 
45
50
  class Event
46
51
  contextualize
47
- icontext :control
48
- icontexts :view
52
+ context :control
53
+ contexts :view
49
54
  end
50
55
 
51
56
  describe "Contextualize" do
@@ -54,12 +59,12 @@ describe "Contextualize" do
54
59
  end
55
60
 
56
61
  it "gracefully handles adding an invalid context to an object without raising error" do
57
- lambda {project.add_icontext :blip}.should_not raise_error
58
- lambda {project.remove_icontext :blip}.should_not raise_error
62
+ lambda {project.add_context :blip}.should_not raise_error
63
+ lambda {project.remove_context :blip}.should_not raise_error
59
64
  end
60
65
 
61
66
  it "can add a context to an object" do
62
- project.add_icontext :view
67
+ project.add_context :view
63
68
  project.own_methods.should include(:view)
64
69
 
65
70
  project.view.should == "view"
@@ -67,16 +72,16 @@ describe "Contextualize" do
67
72
  end
68
73
 
69
74
  it "can remove a context from an object" do
70
- project.add_icontext :view
75
+ project.add_context :view
71
76
  project.own_methods.should include(:view)
72
77
 
73
- project.remove_icontext :view
78
+ project.remove_context :view
74
79
  lambda {project.view}.should raise_error
75
80
  lambda {project.detailed_view}.should raise_error
76
81
  end
77
82
 
78
83
  it 'can operate in a context scope' do
79
- project.icontext_scope :view, :control do |project|
84
+ project.context_scope :view, :control do |project|
80
85
  project.view.should == "view"
81
86
  project.control.should == "control"
82
87
  end
@@ -86,12 +91,12 @@ describe "Contextualize" do
86
91
 
87
92
  describe 'Array extension' do
88
93
  let (:events) do
89
- [Event.new, Event.new].add_icontext :view
94
+ [Event.new, Event.new].add_context :view
90
95
  end
91
96
 
92
97
  it 'should work on an Array instance' do
93
98
  events.first.own_methods.should include(:view)
94
- end
99
+ end
95
100
  end
96
101
 
97
102
  describe 'naming conventions' do
@@ -100,12 +105,15 @@ describe "Contextualize" do
100
105
  end
101
106
 
102
107
  it 'should apply naming conventions' do
103
- event.add_icontext :view
108
+ event.enter_context :view
104
109
  event.own_methods.should include(:view)
105
110
  event.view.should == "view"
106
111
 
107
- event.add_icontext :control
112
+ event.enter_context :control
108
113
  event.own_methods.should include(:control)
114
+
115
+ event.exit_context :control
116
+ event.own_methods.should_not include(:control)
109
117
  end
110
118
  end
111
119
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contextualize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
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: 2011-07-05 00:00:00.000000000Z
12
+ date: 2011-09-21 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mixology
16
- requirement: &2154964500 !ruby/object:Gem::Requirement
16
+ requirement: &2163328980 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,21 @@ dependencies:
21
21
  version: 0.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2154964500
24
+ version_requirements: *2163328980
25
+ - !ruby/object:Gem::Dependency
26
+ name: i18n
27
+ requirement: &2163328500 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2163328500
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: activesupport
27
- requirement: &2154964020 !ruby/object:Gem::Requirement
38
+ requirement: &2163328020 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: 3.0.1
33
44
  type: :runtime
34
45
  prerelease: false
35
- version_requirements: *2154964020
46
+ version_requirements: *2163328020
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: rspec
38
- requirement: &2154980680 !ruby/object:Gem::Requirement
49
+ requirement: &2163327540 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: 2.3.0
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *2154980680
57
+ version_requirements: *2163327540
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: bundler
49
- requirement: &2154980200 !ruby/object:Gem::Requirement
60
+ requirement: &2163327060 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ~>
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: 1.0.1
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *2154980200
68
+ version_requirements: *2163327060
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: jeweler
60
- requirement: &2154979720 !ruby/object:Gem::Requirement
71
+ requirement: &2163326580 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ~>
@@ -65,10 +76,10 @@ dependencies:
65
76
  version: 1.6.3
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *2154979720
79
+ version_requirements: *2163326580
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: rcov
71
- requirement: &2154979240 !ruby/object:Gem::Requirement
82
+ requirement: &2163326100 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ! '>='
@@ -76,7 +87,7 @@ dependencies:
76
87
  version: '0'
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *2154979240
90
+ version_requirements: *2163326100
80
91
  description: Add and remove behaviors defined in modules depending on context an object
81
92
  operates in
82
93
  email: kmandrup@gmail.com
@@ -115,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
126
  version: '0'
116
127
  segments:
117
128
  - 0
118
- hash: -4138437113857599099
129
+ hash: -898150994270880704
119
130
  required_rubygems_version: !ruby/object:Gem::Requirement
120
131
  none: false
121
132
  requirements:
@@ -124,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
135
  version: '0'
125
136
  requirements: []
126
137
  rubyforge_project:
127
- rubygems_version: 1.8.5
138
+ rubygems_version: 1.8.10
128
139
  signing_key:
129
140
  specification_version: 3
130
141
  summary: Contextual behaviors for objects for use with Rails models and many other