Permalink
Browse files

Factored out assertions library into separate FailFast gem

  • Loading branch information...
1 parent f76ae5e commit fd90adff518cadb47b11b90d1b7e28be68a45bbe Avdi Grimm committed Nov 29, 2008
Showing with 9 additions and 357 deletions.
  1. +0 −4 Manifest.txt
  2. +2 −1 Rakefile
  3. +3 −1 TODO
  4. +4 −4 lib/alter_ego.rb
  5. +0 −63 lib/assertions.rb
  6. +0 −284 spec/assertions_spec.rb
View
@@ -5,15 +5,11 @@ README.rdoc
State_Design_Pattern_UML_Class_Diagram.png
Rakefile
TODO
-lib/assertions.rb
-spec/assertions_spec.rb
lib/alter_ego.rb
script/console
script/destroy
script/generate
spec/spec.opts
spec/spec_helper.rb
spec/alter_ego_spec.rb
-lib/alter_ego.rb
-spec/alter_ego_spec.rb
tasks/rspec.rake
View
@@ -8,7 +8,8 @@ $hoe = Hoe.new('alter-ego', AlterEgo::VERSION) do |p|
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
p.rubyforge_name = p.name # TODO this is default value
p.extra_deps = [
- ['activesupport','>= 2.0.2'],
+ ['activesupport','>= 2.0.0'],
+ ['fail-fast', '>= 1.0.0']
]
p.extra_dev_deps = [
['newgem', ">= #{::Newgem::VERSION}"]
View
4 TODO
@@ -1,4 +1,6 @@
-* TODO Factor assertions out into separate library
+# -*- mode: org -*-
+* DONE Factor assertions out into separate library
+ CLOSED: [2008-11-29 Sat 01:29]
* TODO Remove ActiveSupport dependency
* TODO Nested state support with history
* TODO Verify full methods/respond_to? integration
View
@@ -1,16 +1,16 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
-require File.join(File.dirname(__FILE__), 'assertions')
require 'forwardable'
require 'singleton'
require 'rubygems'
require 'activesupport'
+require 'fail_fast'
module AlterEgo
VERSION = '1.0.0'
- include Assertions
+ include FailFast::Assertions
class StateError < RuntimeError
end
@@ -70,8 +70,8 @@ def ==(other)
end
class State
- include Assertions
- extend Assertions
+ include FailFast::Assertions
+ extend FailFast::Assertions
def self.transition(options, &trans_action)
options.assert_valid_keys(:to, :on, :if)
View
@@ -1,63 +0,0 @@
-class AssertionFailureError < Exception
-end
-
-module Assertions
-
- # Assert that no +values+ are nil or false. Returns the last value.
- def assert(*values, &block)
- iterate_and_return_last(values, block) do |v|
- raise_assertion_error unless v
- end
- end
-
- # The opposite of #assert.
- def deny(*values)
- assert(*values.map{ |v| !v})
- assert(yield(*values)) if block_given?
- values.last
- end
-
- # Assert that no +values+ are nil. Returns the last value.
- def assert_exists(*values, &block)
- iterate_and_return_last(values, block) { |value| deny(value.nil?) }
- end
-
- # Assert that +values+ are collections that contain at least one element.
- # Returns the last value.
- def assert_one_or_more(*values, &block)
- iterate_and_return_last(values, block) do |value|
- assert_exists(value)
- deny(value.kind_of?(String))
- deny(value.empty?)
- end
- end
-
- def assert_keys(hash, *keys)
- assert_exists(hash)
- assert(hash.respond_to?(:[]))
- values = keys.inject([]) { |vals, k| vals << assert_exists(hash[k]) }
- assert(yield(*values)) if block_given?
- hash
- end
-
- private
-
- def iterate_and_return_last(values, block = nil)
- values.each { |v| yield(v) }
- if block
- raise_assertion_error unless block.call(*values)
- end
- values.last
- end
-
- def raise_assertion_error
- error = AssertionFailureError.new
- backtrace = caller
- trimmed_backtrace = []
- trimmed_backtrace.unshift(backtrace.pop) until
- backtrace.last.include?(__FILE__)
- error.set_backtrace(trimmed_backtrace)
- raise error
- end
-
-end
Oops, something went wrong.

0 comments on commit fd90adf

Please sign in to comment.