Sha256: d9cfd72b2389c477d779d7b39dfd65a6fc430a068e99a964491a63c431d7731f

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

require 'date'
require 'simplecheck'

class Person
  include Simplecheck
  include Comparable

  attr_accessor( :name, :surname, :date_of_birth )

  def initialize( name, surname, date_of_birth )
    check( name, surname, String )
    check( date_of_birth, Date )

    @name          = name
    @surname       = surname
    @date_of_birth = date_of_birth
  end

  def <=>( person )
    check( person, Person )
    check( person.date_of_birth )

    self.date_of_birth <=> person.date_of_birth
  end
end

def try
  begin
    yield
  rescue Simplecheck::CheckFailed => exception
    puts "Check Failed: #{ exception.message }"
  rescue => exception
    puts "EXCEPTION: #{ exception.message }"
  end
end

# date_of_birth is not a Date
try{ Person.new( 'Bob', 'Roberts', '1980-01-01' )} 

bob = Person.new( 'Bob', 'Roberts', Date.civil( 1970, 1, 1 )) 
joe = Person.new( 'Joe', 'Josephs', Date.civil( 1980, 1, 1 ))

# 1 is not a Person
try{ bob > 1 }

if joe > bob
  puts "Joe > Bob"
end

bob.date_of_birth = nil

# date_of_birth is not present
try{ joe > bob }

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simplecheck-1.0rc1 examples/person_example.rb
simplecheck-0.9 examples/person_example.rb