Sha256: 76539a589d3e79a62a5ba9d05720e4bd12a0ce9d2d7bbfddfb98e404eec3f701

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

class Object {
  def require: require_block ensure: ensure_block body: body_block {
    requirement = Contract Requirement new: require_block
    ensurance = Contract Requirement new: ensure_block
    retval = nil
    requirement if_fullfilled: {
      retval = body_block call
    }
    ensure_block if_fullfilled: {
      retval
    }
  }

  def require: require_block body: body_block {
    requirement = Contract Requirement new: require_block
    requirement if_fullfilled: {
      body_block call
    }
  }

  def ensure: ensure_block body: body_block {
    require: ensure_block body: body_block
  }
}

class Contract {
  class RequirementError : RuntimeError
  class Requirement {
    def initialize: @block
    def if_fullfilled: block {
      @block call: [self]
      block call
    }
    def true: expr {
      { RequirementError new: "#{expr} not true" . raise! } unless: expr
    }
    def false: expr {
      { RequirementError new: "#{expr} not false" . raise! } if: expr
    }
  }
}

# # usage:
# class Fixnum {
#   def / other {
#     require: @{
#       true: $ other is_a?: Fixnum
#       true: $ other > 0
#       # better:
#       other class is: Fixnum
#       other is > 0
#
#     } body: {
#        "foo" println
#     }
#   }
# }

# f = 10 / 2
# g = 10 / 0 # requirement error

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fancy-0.6.0 lib/contracts.fy