Class: Should

Inherits:
Object show all
Defined in:
lib/mac_bacon.rb

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Should

Returns a new instance of Should.



492
493
494
495
# File 'lib/mac_bacon.rb', line 492

def initialize(object)
  @object = object
  @negated = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



536
537
538
539
540
541
542
543
544
# File 'lib/mac_bacon.rb', line 536

def method_missing(name, *args, &block)
  name = "#{name}?"  if name.to_s =~ /\w[^?]\z/

  desc = @negated ? "not " : ""
  desc << @object.inspect << "." << name.to_s
  desc << "(" << args.map{|x|x.inspect}.join(", ") << ") failed"

  satisfy(desc) { |x| x.__send__(name, *args, &block) }
end

Instance Method Details

#be(*args, &block) ⇒ Object Also known as: a, an



507
508
509
510
511
512
513
514
# File 'lib/mac_bacon.rb', line 507

def be(*args, &block)
  if args.empty?
    self
  else
    block = args.shift  unless block_given?
    satisfy(*args, &block)
  end
end

#equal(value) ⇒ Object



546
# File 'lib/mac_bacon.rb', line 546

def equal(value)         self == value      end

#flunk(reason = "Flunked") ⇒ Object

Raises:



551
552
553
# File 'lib/mac_bacon.rb', line 551

def flunk(reason="Flunked")
  raise Bacon::Error.new(:failed, reason)
end

#identical_to(value) ⇒ Object Also known as: same_as



548
# File 'lib/mac_bacon.rb', line 548

def identical_to(value)  self.equal? value  end

#match(value) ⇒ Object



547
# File 'lib/mac_bacon.rb', line 547

def match(value)         self =~ value      end

#not(*args, &block) ⇒ Object



497
498
499
500
501
502
503
504
505
# File 'lib/mac_bacon.rb', line 497

def not(*args, &block)
  @negated = !@negated

  if args.empty?
    self
  else
    be(*args, &block)
  end
end

#satisfy(*args, &block) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/mac_bacon.rb', line 519

def satisfy(*args, &block)
  if args.size == 1 && String === args.first
    description = args.shift
  else
    description = ""
  end

  r = yield(@object, *args)
  if Bacon::Counter[:depth] > 0
    Bacon::Counter[:requirements] += 1
    raise Bacon::Error.new(:failed, description)  unless @negated ^ r
    r
  else
    @negated ? !r : !!r
  end
end