modularity 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +97 -0
- data/VERSION +1 -1
- data/modularity.gemspec +2 -2
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -1 +1,98 @@
|
|
1
1
|
= modularity - Traits and partial classes for Ruby
|
2
|
+
|
3
|
+
Modularity provides traits and partial classes for Ruby.
|
4
|
+
This lets you organize large models into multiple source files.
|
5
|
+
It also allows very simple definition of meta-programming macros,
|
6
|
+
as you might now from <tt>acts_as_something</tt> type of plugins,
|
7
|
+
or the macros Rails provides for your models.
|
8
|
+
|
9
|
+
Modularity traits are to your models what partials are for your Rails views.
|
10
|
+
|
11
|
+
== Example 1: Splitting a model into multiple source files
|
12
|
+
|
13
|
+
Models are often concerned with multiple themes like "authentication", "contact info" or "permissions", each requiring
|
14
|
+
a couple of validations and callbacks here, and some method there. Modularity lets you organize your model into multiple
|
15
|
+
partial classes, so each file can deal with a single aspect of your model:
|
16
|
+
|
17
|
+
# app/model/user.rb
|
18
|
+
class User < ActiveRecord::Base
|
19
|
+
does "user/authentication"
|
20
|
+
does "user/address"
|
21
|
+
end
|
22
|
+
|
23
|
+
# app/model/user/authentication.rb
|
24
|
+
module User::Authentication
|
25
|
+
as_trait do
|
26
|
+
# methods, validations, etc. regarding usernames and passwords go here
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# app/model/user/permissions.rb
|
31
|
+
module User::Permissions
|
32
|
+
as_trait do
|
33
|
+
# methods, validations, etc. regarding contact information go here
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
== Example 2: Easy meta-programming macros
|
38
|
+
|
39
|
+
Ruby allows you to construct classes using meta-programming macros like <tt>acts_as_tree</tt> or <tt>has_many :items</tt>.
|
40
|
+
These macros will add methods, callbacks, etc. to the calling class. Hoever, right now Ruby (and Rails) makes it awkward to define
|
41
|
+
such macros in your project as part of your application domain.
|
42
|
+
|
43
|
+
Modularity allows you to extract common behaviour into reusable macros by defining traits with parameters. Your macros can live in your
|
44
|
+
application, allowing you to express your application domain in both classes and macros.
|
45
|
+
|
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
|
+
|
48
|
+
# app/model/article.rb
|
49
|
+
class Article
|
50
|
+
does "strip_fields", :name, :brand
|
51
|
+
end
|
52
|
+
|
53
|
+
# app/model/shared/strip_fields.rb
|
54
|
+
module StripFields
|
55
|
+
as_trait do |*fields|
|
56
|
+
fields.each do |field|
|
57
|
+
define_method("#{field}=") do |value|
|
58
|
+
self[field] = value.strip
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
We like to add <tt>app/models/shared</tt> and <tt>app/controllers/shared</tt> to the load paths of our Rails projects. These are great places to store macros
|
65
|
+
that are re-used from multiple classes.
|
66
|
+
|
67
|
+
== Example 3: Mixins with class methods
|
68
|
+
|
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
|
+
Modularity does away with the clutter and lets you say this:
|
71
|
+
|
72
|
+
# app/model/model.rb
|
73
|
+
class Model
|
74
|
+
does "mixin"
|
75
|
+
end
|
76
|
+
|
77
|
+
module Mixins
|
78
|
+
as_trait do
|
79
|
+
def instance_method
|
80
|
+
# ...
|
81
|
+
end
|
82
|
+
def self.class_method
|
83
|
+
# ..
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
<tt>private</tt> and <tt>protected</tt> will also work as expected when defining a trait.
|
88
|
+
|
89
|
+
== Installation
|
90
|
+
|
91
|
+
sudo gem sources -a http://gemcutter.org
|
92
|
+
sudo gem install modularity
|
93
|
+
|
94
|
+
== Credits
|
95
|
+
|
96
|
+
Henning Koch
|
97
|
+
|
98
|
+
{www.makandra.de}[http://www.makandra.de/]
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
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.4.
|
8
|
+
s.version = "0.4.1"
|
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-15}
|
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 = [
|
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.
|
4
|
+
version: 0.4.1
|
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-15 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|