Skip to content

Latest commit

History

27 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Modularity provides traits and partial classes for Ruby. This allows very simple definition of meta-programming macros, as you might now from acts_as_something type of plugins, or the macros Rails provides for your models. This also lets you organize large models into multiple source files in a way that is less awkward than using modules.

Modularity traits are to your models what partials are for your Rails views.

Ruby allows you to construct classes using meta-programming macros like acts_as_tree or has_many :items. These macros will add methods, callbacks, etc. to the calling class. However, right now Ruby (and Rails) makes it awkward to define such macros in your project as part of your application domain.

Modularity allows you to extract common behaviour into reusable macros by defining traits with parameters. Your macros can live in your application, allowing you to express your application domain in both classes and macros.

Here is an example of a strip_field macro, which created setter methods that remove leading and trailing whitespace from newly assigned values:

# app/models/article.rbclassArticledoes"strip_fields", :name, :brandend# app/models/shared/strip_fields_trait.rbmoduleStripFieldsTraitas_traitdo|*fields|fields.eachdo|field|define_method("#{field}=") do|value|self[field] = value.stripendendendend

We like to add app/models/shared and app/controllers/shared to the load paths of our Rails projects. These are great places to store macros that are re-used from multiple classes.

Using a module to add both instance methods and class methods is very awkward. Modularity does away with the clutter and lets you say this:

# app/models/model.rb
class Model
does "mixin"
end
# app/models/mixin_trait.rb
module MixinTrait
as_trait do
def instance_method
# ...
end
def self.class_method
# ..
end
end

private and protected will also work as expected when defining a trait.

Models are often concerned with multiple themes like “authentication”, “contact info” or “permissions”, each requiring a couple of validations and callbacks here, and some method there. Modularity lets you organize your model into multiple partial classes, so each file can deal with a single aspect of your model:

# app/models/user.rbclassUser<ActiveRecord::Basedoes"user/authentication"does"user/address"end# app/models/user/authentication_trait.rbmoduleUser::AuthenticationTraitas_traitdo# methods, validations, etc. regarding usernames and passwords go hereendend# app/models/user/permissions_trait.rbmoduleUser::PermissionsTraitas_traitdo# methods, validations, etc. regarding contact information go hereendend
sudogeminstallmodularity

Modularity requires Ruby 1.8.7. Earlier versions are missing class_exec. You might be able to hack in class_exec using this as a guide, but it’s not pretty.

Henning Koch

makandra.com

gem-session.com

About

Traits and partial classes for Ruby

Resources

Stars

1 star

Watchers

16 watching

Forks

Releases

Packages

Used by

Contributors

Languages