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.



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

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



336
337
338
339
340
341
342
343
344
# File 'lib/bacon.rb', line 336

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



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

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

#equal(value) ⇒ Object



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

def equal(value)         self == value      end

#flunk(reason = "Flunked") ⇒ Object

Raises:



351
352
353
# File 'lib/bacon.rb', line 351

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

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



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

def identical_to(value)  self.equal? value  end

#match(value) ⇒ Object



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

def match(value)         self =~ value      end

#not(*args, &block) ⇒ Object



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

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

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

#satisfy(*args, &block) ⇒ Object



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

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
  end
  (@negated ^ r) ? (r || true) : false
end