Class: Should

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

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Should

Returns a new instance of Should.



295
296
297
298
# File 'lib/bacon.rb', line 295

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



333
334
335
336
337
338
339
340
341
# File 'lib/bacon.rb', line 333

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



310
311
312
313
314
315
316
317
# File 'lib/bacon.rb', line 310

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

#equal(value) ⇒ Object



343
# File 'lib/bacon.rb', line 343

def equal(value)         self == value      end

#flunk(reason = "Flunked") ⇒ Object

Raises:



348
349
350
# File 'lib/bacon.rb', line 348

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

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



345
# File 'lib/bacon.rb', line 345

def identical_to(value)  self.equal? value  end

#match(value) ⇒ Object



344
# File 'lib/bacon.rb', line 344

def match(value)         self =~ value      end

#not(*args, &block) ⇒ Object



300
301
302
303
304
305
306
307
308
# File 'lib/bacon.rb', line 300

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

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

#satisfy(description = "", &block) ⇒ Object



322
323
324
325
326
327
328
329
330
331
# File 'lib/bacon.rb', line 322

def satisfy(description="", &block)
  r = yield(@object)
  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