MooseX is a postmodern object DSL for Ruby

This is a DSL for object creation, aspects, method delegation and much more. It is based on Perl Moose and Moo, two important modules who add a better way of Object Orientation development (and I enjoy A LOT). Using a declarative stype, using Moose/Moo you can create attributes, methods, the entire constructor and much more. But I can’t find something similar in Ruby world, so I decide port a small subset of Moose to create a powerfull DSL for object construction.

Of course, there is few similar projects in ruby like

But the objetive of MooseX is different: this is a toolbox to create Classes based on DSL, with unique features like

method delegation ( see ‘handles’)

lazy attributes

roles

parameterized roles

composable type check

events

and much more.

This rubygem is based on this modules:

See also:

Joose, a javascript port of Moose.

Perl 6 Perl 6 OO programming style.

Why MooseX? Because the namespace MooseX/MooX is open to third-party projects/plugins/extensions. You can upgrade your Moo(se) class using other components if you want. And there is one gem called ‘moose’ :/

THIS MODULE IS EXPERIMENTAL YET! BE CAREFUL!

Talk is cheap. Show me the code!

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 require 'moosex' class Point include MooseX has x : { is : :rw , # read-write (mandatory) isa : Integer , # should be Integer default : 0 , # default value is 0 (constant) } has y : { is : :rw , isa : Integer , default : lambda { 0 }, # you should specify a lambda } def clear! self . x = 0 # to run with type-check you must self . y = 0 # use the setter instad @x= end end # now you have a generic constructor p1 = Point . new # x and y will be 0 p2 = Point . new ( x : 5 ) # y will be 0 p3 = Point . new ( x : 5 , y : 4 )

Installation

Add this line to your application’s Gemfile:

gem 'moosex'

And then execute:

$ bundle

Or install it yourself as:

$ gem install moosex

You need ruby 2.0.x or superior.

Description

MooseX is an extension of Ruby object system. The main goal of MooseX is to make Ruby Object Oriented programming easier, more consistent, and less tedious. With MooseX you can think more about what you want to do and less about the mechanics of OOP. It is a port of Moose/Moo from Perl to Ruby world.

Read more about Moose on http://moose.iinteractive.com/en/

Motivation

It is fun

Usage

You just need include the MooseX module in your class and start to describe the attributes with our DSL. This module will inject one smart constructor, acessor and other necessary methods.

Instead the normal way of add accessors, constructor, validation, etc

1 2 3 4 5 6 7 8 9 10 11 12 class Foo attr_accessor :bar , :baz , :bam def initialize ( bar = 0 , baz = 0 , bam = 0 ) unless [ bar , baz , bam ]. all? { | x | x . is_a? Integer } raise "you should use only Integers to build Foo" end @bar = bar @baz = baz @bam = bam end end

you can do this:

1 2 3 4 5 6 7 8 9 class Foo include MooseX has [ :bar , :baz , :bam ] , { is : :rw , isa : Integer , default : 0 } end

Contributing