Class: Should

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

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Should

Returns a new instance of Should.



263
264
265
266
# File 'lib/vendor/bacon.rb', line 263

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



305
306
307
308
309
310
311
312
313
# File 'lib/vendor/bacon.rb', line 305

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



278
279
280
281
282
283
284
285
# File 'lib/vendor/bacon.rb', line 278

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

#equal(value) ⇒ Object



315
# File 'lib/vendor/bacon.rb', line 315

def equal(value)         self == value      end

#flunk(reason = "Flunked") ⇒ Object

Raises:



320
321
322
# File 'lib/vendor/bacon.rb', line 320

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

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



317
# File 'lib/vendor/bacon.rb', line 317

def identical_to(value)  self.equal? value  end

#match(value) ⇒ Object



316
# File 'lib/vendor/bacon.rb', line 316

def match(value)         self =~ value      end

#not(*args, &block) ⇒ Object



268
269
270
271
272
273
274
275
276
# File 'lib/vendor/bacon.rb', line 268

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

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

#satisfy(*args, &block) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/vendor/bacon.rb', line 290

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 : false
end