modularity 0.4.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +16 -10
- data/VERSION +1 -1
- data/lib/modularity.rb +1 -0
- data/lib/modularity/as_trait.rb +2 -4
- data/lib/modularity/does.rb +3 -4
- data/modularity.gemspec +2 -2
- data/spec/as_trait_spec.rb +3 -3
- data/spec/does_spec.rb +19 -13
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -14,21 +14,21 @@ Models are often concerned with multiple themes like "authentication", "contact
|
|
14
14
|
a couple of validations and callbacks here, and some method there. Modularity lets you organize your model into multiple
|
15
15
|
partial classes, so each file can deal with a single aspect of your model:
|
16
16
|
|
17
|
-
# app/
|
17
|
+
# app/models/user.rb
|
18
18
|
class User < ActiveRecord::Base
|
19
19
|
does "user/authentication"
|
20
20
|
does "user/address"
|
21
21
|
end
|
22
22
|
|
23
|
-
# app/
|
24
|
-
module User::
|
23
|
+
# app/models/user/authentication_trait.rb
|
24
|
+
module User::AuthenticationTrait
|
25
25
|
as_trait do
|
26
26
|
# methods, validations, etc. regarding usernames and passwords go here
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
# app/
|
31
|
-
module User::
|
30
|
+
# app/models/user/permissions_trait.rb
|
31
|
+
module User::PermissionsTrait
|
32
32
|
as_trait do
|
33
33
|
# methods, validations, etc. regarding contact information go here
|
34
34
|
end
|
@@ -45,13 +45,13 @@ application, allowing you to express your application domain in both classes and
|
|
45
45
|
|
46
46
|
Here is an example of a <tt>strip_field</tt> macro, which created setter methods that remove leading and trailing whitespace from newly assigned values:
|
47
47
|
|
48
|
-
# app/
|
48
|
+
# app/models/article.rb
|
49
49
|
class Article
|
50
50
|
does "strip_fields", :name, :brand
|
51
51
|
end
|
52
52
|
|
53
|
-
# app/
|
54
|
-
module
|
53
|
+
# app/models/shared/strip_fields_trait.rb
|
54
|
+
module StripFieldsTrait
|
55
55
|
as_trait do |*fields|
|
56
56
|
fields.each do |field|
|
57
57
|
define_method("#{field}=") do |value|
|
@@ -69,12 +69,13 @@ that are re-used from multiple classes.
|
|
69
69
|
Using a module to add both instance methods and class methods is {very awkward}[http://redcorundum.blogspot.com/2006/06/mixing-in-class-methods.html].
|
70
70
|
Modularity does away with the clutter and lets you say this:
|
71
71
|
|
72
|
-
# app/
|
72
|
+
# app/models/model.rb
|
73
73
|
class Model
|
74
74
|
does "mixin"
|
75
75
|
end
|
76
76
|
|
77
|
-
|
77
|
+
# app/models/mixin_trait.rb
|
78
|
+
module MixinTrait
|
78
79
|
as_trait do
|
79
80
|
def instance_method
|
80
81
|
# ...
|
@@ -90,6 +91,11 @@ Modularity does away with the clutter and lets you say this:
|
|
90
91
|
|
91
92
|
sudo gem sources -a http://gemcutter.org
|
92
93
|
sudo gem install modularity
|
94
|
+
|
95
|
+
== Dependencies
|
96
|
+
|
97
|
+
Modularity requires Ruby 1.8.7. Earlier versions are missing <tt>class_exec</tt>. You might be able to hack in <tt>class_exec</tt>
|
98
|
+
using {this}[http://github.com/brynary/rspec/blob/f80d61a399b34f58084a378c85a43a95ff484619/lib/spec/extensions/instance_exec.rb] as a guide, but it's not pretty.
|
93
99
|
|
94
100
|
== Credits
|
95
101
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/lib/modularity.rb
CHANGED
data/lib/modularity/as_trait.rb
CHANGED
data/lib/modularity/does.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require 'modularity/as_trait'
|
2
1
|
require 'modularity/inflector'
|
2
|
+
require 'modularity/as_trait'
|
3
3
|
|
4
4
|
module Modularity
|
5
5
|
module Does
|
@@ -10,10 +10,9 @@ module Modularity
|
|
10
10
|
|
11
11
|
module ClassMethods
|
12
12
|
def does(trait_name, *args)
|
13
|
-
trait_name = Modularity::Inflector.camelize(trait_name.to_s)
|
13
|
+
trait_name = "#{Modularity::Inflector.camelize(trait_name.to_s)}Trait"
|
14
14
|
trait = Modularity::Inflector.constantize(trait_name)
|
15
|
-
|
16
|
-
macro = trait.instance_variable_get("@as_trait") or raise "Missing as_trait directive in #{trait_name}"
|
15
|
+
macro = trait.instance_variable_get("@trait_macro") or raise "Missing trait directive in #{trait_name}"
|
17
16
|
class_exec(*args, ¯o)
|
18
17
|
end
|
19
18
|
end
|
data/modularity.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{modularity}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.6.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Henning Koch"]
|
12
|
-
s.date = %q{2010-02-
|
12
|
+
s.date = %q{2010-02-18}
|
13
13
|
s.description = %q{Traits and partial classes for Ruby}
|
14
14
|
s.email = %q{github@makandra.de}
|
15
15
|
s.extra_rdoc_files = [
|
data/spec/as_trait_spec.rb
CHANGED
@@ -17,13 +17,13 @@ describe Modularity::AsTrait do
|
|
17
17
|
describe 'as_trait' do
|
18
18
|
|
19
19
|
it "should let modules save a proc upon loading" do
|
20
|
-
Trait.instance_variable_get("@
|
20
|
+
Trait.instance_variable_get("@trait_macro").call.should == "hi world"
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should let modules save a proc with parameters upon loading" do
|
24
|
-
ParametrizedTrait.instance_variable_get("@
|
24
|
+
ParametrizedTrait.instance_variable_get("@trait_macro").call("jean").should == "hi, jean"
|
25
25
|
end
|
26
26
|
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
end
|
data/spec/does_spec.rb
CHANGED
@@ -1,20 +1,26 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
|
-
module
|
4
|
-
as_trait
|
3
|
+
module SomeTrait
|
4
|
+
as_trait do
|
5
|
+
some_trait_included
|
6
|
+
end
|
5
7
|
end
|
6
8
|
|
7
|
-
module
|
8
|
-
|
9
|
+
module Some
|
10
|
+
module ChildTrait
|
11
|
+
as_trait do
|
12
|
+
some_child_trait_included
|
13
|
+
end
|
14
|
+
end
|
9
15
|
end
|
10
16
|
|
11
|
-
module
|
17
|
+
module CallMethodTrait
|
12
18
|
as_trait do |field|
|
13
19
|
send(field)
|
14
20
|
end
|
15
21
|
end
|
16
22
|
|
17
|
-
module
|
23
|
+
module VisibilityTrait
|
18
24
|
as_trait do
|
19
25
|
def public_method_from_trait
|
20
26
|
end
|
@@ -27,7 +33,7 @@ module Visibility
|
|
27
33
|
end
|
28
34
|
end
|
29
35
|
|
30
|
-
module
|
36
|
+
module DefineConstantMethodTrait
|
31
37
|
as_trait do |name, return_value|
|
32
38
|
define_method name do
|
33
39
|
return_value
|
@@ -42,17 +48,17 @@ describe Modularity::AsTrait do
|
|
42
48
|
|
43
49
|
describe 'does' do
|
44
50
|
|
45
|
-
it "should
|
46
|
-
Doer.should_receive(:
|
51
|
+
it "should apply the named module" do
|
52
|
+
Doer.should_receive(:some_trait_included)
|
47
53
|
Doer.class_eval do
|
48
|
-
does "
|
54
|
+
does "some"
|
49
55
|
end
|
50
56
|
end
|
51
57
|
|
52
|
-
it "should
|
53
|
-
Doer.should_receive(:
|
58
|
+
it "should apply a namespaced module, using slash-notation like require" do
|
59
|
+
Doer.should_receive(:some_child_trait_included)
|
54
60
|
Doer.class_eval do
|
55
|
-
does "
|
61
|
+
does "some/child"
|
56
62
|
end
|
57
63
|
end
|
58
64
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modularity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henning Koch
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-18 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|