[ANN] constraint 0.1 -- ensure that object always satisfy a specifiedset of constraints

Discussion in 'Ruby' started by Thomas, Jun 10, 2005.

  1. Thomas

    Thomas Guest

    Hi,

    I would like to announce 0.1 of contraint.rb. This is a first draft.

    This library provides a way to ensure that object always satisfy a
    specified set of constraints. An object that can be constrained must be
    of kind Constraint::Shell. Constraints can be added to classes and
    single objects. It is possible to define methods that handle constraint
    violations and can make the object fit the demands.

    Project
    http://rubyforge.org/projects/constraint/
    Download
    http://rubyforge.org/frs/?group_id=748
    Support
    http://rubyforge.org/forum/?group_id=748

    In my personal experience, a certain type of error in dynamically typed
    languages is caused by pushing a wrong object to a collection or
    similar. What makes this type of error so awkward is that they often
    result in an exception in a different part of your program, which is why
    it can be unnecessarily difficult to track down what is actually causing
    the problem. The goal of this library is to raise an exception right
    where such a thing happens.

    Constrained classes should inherit from Constraint::Shell. The subclass
    should provide a way for defining new instances (by defining
    #new_constrained) and redefine #processconstrainedvalue. The
    @constrained_value instance variable contains the shell's actual values.

    Examples:

    class ConstrainedArray < Constraint::Shell
    def new_constrained
    []
    end

    def process_constrained_value
    @constrained_value.collect! {|e| yield e}
    end
    end

    class NumericArray < ConstrainedArray
    or_constraint("Numeric") {|e| e.kind_of?(Numeric)}
    end

    class EvenNumericArray < NumericArray
    and_constraint("Even") {|e| e.modulo(2) == 0}
    end

    class EvenInteger < Constraint::Shell
    and_constraint("Numeric") {|e| e.kind_of?(Integer)}
    and_constraint("Even") {|e| e.modulo(2) == 0}
    end

    enum = EvenInteger.new(2)
    enum.and_constraint("LT10") {|e| e < 10}

    or:

    class DuckPalace < ConstrainedArray
    and_constraint('Quack') {|e| e.do_you_quack?}
    and_constraint('Ticket') {|e| e.your_ticket_please == :valid}
    end

    whiteduckpalace = DuckPalace.new
    whiteduckpalace.and_constraint('White') {|e| e.colour == 'white'}
    class << whiteduckpalace
    def handle_constraint_violation(contraint_name, object)
    case contraint_name
    when 'white'
    return object.paint_me('white') if
    object.respond_to?:)paint_me)
    end
    return super
    end
    end


    Regards,
    Thomas.





    ___________________________________________________________
    Gesendet von Yahoo! Mail - Jetzt mit 1GB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de
     
    Thomas, Jun 10, 2005
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.