Code Monkey home page Code Monkey logo

modularity's Introduction

modularity - Traits and partial classes for Ruby

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.

Example 1: Easy meta-programming macros

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.rb
class Article
  does "strip_fields", :name, :brand
end

# app/models/shared/strip_fields_trait.rb
module StripFieldsTrait
  as_trait do |*fields|
    fields.each do |field|
      define_method("#{field}=") do |value|
        self[field] = value.strip
      end
    end
  end
end

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.

Example 2: Mixins with class methods

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
end

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

Example 3: Splitting a model into multiple source files

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.rb
class User < ActiveRecord::Base
  does "user/authentication"
  does "user/address"
end

# app/models/user/authentication_trait.rb
module User::AuthenticationTrait
  as_trait do
    # methods, validations, etc. regarding usernames and passwords go here
  end
end

# app/models/user/permissions_trait.rb
module User::PermissionsTrait
  as_trait do
    # methods, validations, etc. regarding contact information go here
  end
end

Installation

sudo gem install modularity

Note if you’re still on Ruby 1.8.6

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.

Credits

Henning Koch

makandra.com

gem-session.com

modularity's People

Contributors

5v3n avatar kraatob avatar triskweline avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.