schizo 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ 0.1.2
2
+ * Allow roles to be namespaced
3
+
1
4
  0.1.1
2
5
  * Updated gemspec info
3
6
 
data/README.rdoc CHANGED
@@ -34,7 +34,7 @@ Dive in...
34
34
 
35
35
  poster.kind_of?(User) # => true
36
36
  poster.instance_of?(User) # => true
37
-
37
+
38
38
  poster.class.name # => "User"
39
39
  end
40
40
 
@@ -52,8 +52,8 @@ Dive in...
52
52
 
53
53
  == The Problem
54
54
 
55
- So what's wrong with just using <tt>Object#extend</tt>? Nothing, until you want to alter an instance's class
56
- as a side effect of adorning it with a role... which happens often when using ActiveRecord.
55
+ So what's wrong with just using <tt>Object#extend</tt>? Nothing, until you want to avoid altering an instance's class
56
+ as a side effect of adorning the instance with a role... which happens often when using ActiveRecord.
57
57
 
58
58
  Consider the following use of DCI and ActiveRecord with plain old <tt>Object#extend</tt>:
59
59
 
@@ -8,13 +8,26 @@ module Schizo
8
8
  @base, @role = base, role
9
9
  end
10
10
 
11
+ # Returns a new class derived off of +base+ and namespaced under role.
12
+ # Ex:
13
+ # builder = ClassBuilder.new(User, Poster)
14
+ # builder.product # => Schizo::Facades::User::Poster
15
+ # builder.product.kind_of?(User) # => true
11
16
  def product
12
- @product ||= role_class
17
+ @product ||= begin
18
+ if container_module.const_defined?(class_name, false)
19
+ container_module.const_get(class_name)
20
+ else
21
+ klass = Class.new(base){ include Base }
22
+ klass.class_eval(&role.extended_block) if role.extended_block
23
+ container_module.const_set(class_name, klass)
24
+ end
25
+ end
13
26
  end
14
27
 
15
28
  private
16
29
 
17
- def module_name
30
+ def base_name
18
31
  base.name || "AnonClass#{base.object_id}"
19
32
  end
20
33
 
@@ -22,29 +35,38 @@ module Schizo
22
35
  role.name || "AnonRole#{role.object_id}"
23
36
  end
24
37
 
38
+ def full_name
39
+ @full_name ||= "Schizo::Facades::#{base_name}::#{role_name}"
40
+ end
41
+
42
+ def parsed_full_name
43
+ @parsed_full_name ||= full_name.split("::")
44
+ end
45
+
46
+ def module_names
47
+ @container_module_name ||= parsed_full_name[0..-2]
48
+ end
49
+
50
+ def class_name
51
+ @class_name ||= parsed_full_name[-1]
52
+ end
53
+
25
54
  def container_module
26
55
  @container_module ||= begin
27
- if Schizo::Facades.const_defined?(module_name, false)
28
- Schizo::Facades.const_get(module_name)
29
- else
30
- Schizo::Facades.const_set(module_name, Module.new)
56
+ names = module_names
57
+ mod = get_or_create_module(Object, names.shift)
58
+ while not names.empty?
59
+ mod = get_or_create_module(mod, names.shift)
31
60
  end
61
+ mod
32
62
  end
33
63
  end
34
64
 
35
- def role_class
36
- @role_class ||= begin
37
- if container_module.const_defined?(role_name, false)
38
- container_module.const_get(role_name)
39
- else
40
- container_module.const_set(role_name, build)
41
- end
42
- end
43
- end
44
-
45
- def build
46
- Class.new(base){ include Base }.tap do |klass|
47
- klass.class_eval(&role.extended_block) if role.extended_block
65
+ def get_or_create_module(mod, name)
66
+ if mod.const_defined?(name, false)
67
+ mod.const_get(name)
68
+ else
69
+ mod.const_set(name, Module.new)
48
70
  end
49
71
  end
50
72
 
@@ -1,3 +1,3 @@
1
1
  module Schizo #:nodoc:
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -25,6 +25,15 @@ module Schizo
25
25
  ClassBuilder.new(base, role)
26
26
  end
27
27
 
28
+ it "works with namespaced roles" do
29
+ role.module_eval do
30
+ def self.name
31
+ "Namespaced::Role"
32
+ end
33
+ end
34
+ builder.product.to_s.should == "Schizo::Facades::Foo::Namespaced::Role"
35
+ end
36
+
28
37
  context "#initialize" do
29
38
 
30
39
  it "sets base and role" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schizo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
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: 2012-02-09 00:00:00.000000000 Z
12
+ date: 2012-05-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70202402544160 !ruby/object:Gem::Requirement
16
+ requirement: &70253933607880 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70202402544160
24
+ version_requirements: *70253933607880
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activerecord
27
- requirement: &70202402543320 !ruby/object:Gem::Requirement
27
+ requirement: &70253933607240 !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: *70202402543320
35
+ version_requirements: *70253933607240
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &70202402542560 !ruby/object:Gem::Requirement
38
+ requirement: &70253933606580 !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: *70202402542560
46
+ version_requirements: *70253933606580
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rdoc
49
- requirement: &70202402541760 !ruby/object:Gem::Requirement
49
+ requirement: &70253933605840 !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: *70202402541760
57
+ version_requirements: *70253933605840
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: pry
60
- requirement: &70202402706420 !ruby/object:Gem::Requirement
60
+ requirement: &70253933604920 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70202402706420
68
+ version_requirements: *70253933604920
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
- requirement: &70202402705740 !ruby/object:Gem::Requirement
71
+ requirement: &70253933604120 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70202402705740
79
+ version_requirements: *70253933604120
80
80
  description: DCI (data, context and interaction) for Ruby / Rails / ActiveRecord
81
81
  email:
82
82
  - cjbottaro@alumni.cs.utexas.edu
@@ -124,7 +124,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
124
124
  version: '0'
125
125
  segments:
126
126
  - 0
127
- hash: -910995716218782308
127
+ hash: -2531469905963506492
128
128
  required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  none: false
130
130
  requirements:
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  version: '0'
134
134
  segments:
135
135
  - 0
136
- hash: -910995716218782308
136
+ hash: -2531469905963506492
137
137
  requirements: []
138
138
  rubyforge_project: schizo
139
139
  rubygems_version: 1.8.11