easier ruby DSLs with blocks
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example
lib
spec
test
.gitignore
History.txt
Manifest.txt
README.rdoc
Rakefile
block-chainable.gemspec

README.rdoc

BlockChainable

DESCRIPTION:

BlockChainable is a module to aid in the creation of Domain Specific Languages using block structure. By including BlockChainable into your classes, you will be able to instantiate that class using the class name itself, followed by any parameters to be passed to initialize, followed by a block to be executed within the instantiated class.

BlockChainable also allows methods to search up the chain of classes, meaning that although a block is executed in the scope of the instantiated class, any methods not found in the class but found in a class “up-scope” will be called successfully on the up-scope class. This chaining of method calls allows you to assert values within the blocks as well as calling any other methods from “up-scope” classes.

FEATURES/PROBLEMS:

  • 2 major features

    • Block instantiation of classes

    • Method calls will look “up-scope” for receivers when called inside BlockChainable blocks

  • 1 minor problem

    • Stack trace should be simplified on errors within BlockChainable blocks

SYNOPSIS:

# a simple nonsense example showing the ability to call methods in outer block scopes

# create some simple classes that include BlockChainable
require 'block_chainable'

class Foo
  include BlockChainable

  def hi_from_foo
    puts "hello from foo"
  end
end

class Bar
  include BlockChainable

  def hi_from_bar
    puts "hello from bar"
  end
end

class Boo
  include BlockChainable

  def hi_from_boo
    puts "hello from boo"
  end
end

# we can now use the classes in a DSL manner. notice that calls can happen from with blocks to
# "outer-block" methods.
Foo do
  hi_from_foo

  Bar do
    hi_from_bar
    hi_from_foo

    Boo do
      hi_from_boo
      hi_from_bar
      hi_from_foo
    end
  end
end

# A slighty more complicated example.
# a simple dsl for creating classroom rosters and printing them out. code can be found in the
# example directory.

require 'block_chainable'

# first, define our roster
class Roster
  include BlockChainable

  def initialize(subject)
    @subject = subject
    @students = []
  end

  def add_student_to_roster student
    @students << student
  end

  def print_roster
    puts "Roster for #{@subject}:"
    @students.each{|s| puts "  #{s}"}
  end
end

# next, define our student. the only tricky part here is add_to_roster, which calls
# the method add_student_to_roster, which is actually defined on the Roster class.
# BlockChainable will automatically send this method to the Roster class with the
# Student instance as a parameter.
class Student
  include BlockChainable

  def add_to_roster
    add_student_to_roster self
  end

  def to_s
    "#{@last_name}, #{@first_name} - age #{@age}"
  end

  def first_name name
    @first_name = name
  end

  def last_name name
    @last_name = name
  end

  def age years
    @age = years
  end
end

# we now have all the pieces we need for creating a simple dsl for building
# and printing a class roster

Roster :Math do
  Student do
    first_name "Drew"
    last_name "Olson"
    age 25

    add_to_roster
  end

  Student do
    first_name "John"
    last_name "Doe"
    age 17

    add_to_roster
  end

  Student do
    first_name "Jane"
    last_name "Doe"
    age 19

    add_to_roster
  end

  print_roster
end

REQUIREMENTS:

  • need

  • rspec

INSTALL:

  • rubyforge - sudo gem install block-chainable

  • github - sudo gem install drewolson-block-chainable –source=gems.github.com

LICENSE:

(The MIT License)

Copyright © 2008 Drew Olson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.