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.



238
239
240
241
# File 'lib/bacon.rb', line 238

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



280
281
282
283
284
285
286
287
288
# File 'lib/bacon.rb', line 280

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



253
254
255
256
257
258
259
260
# File 'lib/bacon.rb', line 253

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

#equal(value) ⇒ Object



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

def equal(value)         self == value      end

#flunk(reason = "Flunked") ⇒ Object

Raises:



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

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

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



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

def identical_to(value)  self.equal? value  end

#match(value) ⇒ Object



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

def match(value)         self =~ value      end

#not(*args, &block) ⇒ Object



243
244
245
246
247
248
249
250
251
# File 'lib/bacon.rb', line 243

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

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

#satisfy(*args, &block) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/bacon.rb', line 265

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
    raise Bacon::Error.new(:failed, description)  unless @negated ^ r
    Bacon::Counter[:requirements] += 1
  end
  @negated ^ r ? r : false
end