class Must::Rule

Attributes

object[R]

Public Class Methods

new(object) click to toggle source
# File lib/must/rule.rb, line 7
def initialize(object)
  @object    = object
  @not       = false
end

Public Instance Methods

a() click to toggle source
# File lib/must/rule.rb, line 12
def a()  self end
an() click to toggle source
# File lib/must/rule.rb, line 13
def an() self end
be(*args, &block) click to toggle source
# File lib/must/rule.rb, line 20
def be(*args, &block)
  if args.empty?
    self
  else
    valid?(object == args.shift, &block)
  end
end
blank(&block) click to toggle source
# File lib/must/rule.rb, line 32
def blank(&block)
  valid?(object.blank?, &block)
end
coerced(*types, &block) click to toggle source
# File lib/must/rule.rb, line 70
def coerced(*types, &block)
  coecings        ||= types.last.is_a?(Hash) ? types.pop : {}
  already_coerced ||= Set.new
  kind_of(*types)
rescue Invalid
  block_or_throw(&block) if already_coerced.include?(@object.class)
  already_coerced << @object.class
  @object =
    case (obj = coecings[@object.class])
    when Symbol ; @object.send obj
    when Proc   ; obj.call(@object)
    else        ; obj
    end
  retry
end
duck(method_name, &block) click to toggle source
# File lib/must/rule.rb, line 86
def duck(method_name, &block)
  valid?(duck?(method_name), &block)
end
duck!(method_name, &block) click to toggle source
# File lib/must/rule.rb, line 98
def duck!(method_name, &block)
  if duck?(method_name)
    @object.__send__(method_name)
  elsif block_given?
    block.call
  else
    raise Invalid, "#{method_name} is not defined"
  end
end
duck?(method_name) click to toggle source
# File lib/must/rule.rb, line 90
def duck?(method_name)
  case method_name.to_s
  when /^\./ then method_defined?($')
  when /^#/  then instance_method_defined?($')
  else       ;    method_defined?(method_name)
  end
end
empty(&block) click to toggle source
# File lib/must/rule.rb, line 28
def empty(&block)
  valid?(object.empty?, &block)
end
exist(&block) click to toggle source
# File lib/must/rule.rb, line 36
def exist(&block)
  self.not()
  be(nil, &block)
end
kind_of(*targets, &block) click to toggle source
# File lib/must/rule.rb, line 41
def kind_of(*targets, &block)
  if instance?(@object)
    bool = targets.any?{|klass| is_a?(klass)}
  else
    # check ancestors when klass
    bool = (@object.ancestors & targets).any?
  end
  
  block ||= proc {
    target = targets.map{|i| instance?(i) ? i.class.name : i.name}.join('|')
    target = "(#{target})" if targets.size > 1
    raise Invalid, "expected #{target} but got #{object.class}"
  }
  valid?(bool, &block)
end
match(target, &block)
Alias for: one_of
not() click to toggle source
# File lib/must/rule.rb, line 15
def not
  @not = ! @not
  return self
end
one_of(target, &block) click to toggle source
# File lib/must/rule.rb, line 57
def one_of(target, &block)
  valid?(target === @object, &block)
end
Also aliased as: match
struct(target, &block) click to toggle source
# File lib/must/rule.rb, line 112
def struct(target, &block)
  block ||= proc{ raise Must::StructMismatch, Differ.new(@object, target, "").message }
  valid?(struct?(target), &block)
end
struct?(target) click to toggle source
# File lib/must/rule.rb, line 108
def struct?(target)
  Must::StructInfo.new(@object).same?(target)
end
valid?(condition, &block) click to toggle source
# File lib/must/rule.rb, line 62
def valid?(condition, &block)
  if condition ^ @not
    object
  else
    block_or_throw(&block)
  end
end

Private Instance Methods

block_or_throw(&block) click to toggle source
# File lib/must/rule.rb, line 139
def block_or_throw(&block)
  if block
    block.call
  else
    raise Invalid
  end
end
instance?(obj) click to toggle source
# File lib/must/rule.rb, line 118
def instance?(obj)
  obj.class.to_s !~ /\A(Class|Module)\Z/o
end
instance_method_defined?(method_name) click to toggle source
# File lib/must/rule.rb, line 122
def instance_method_defined?(method_name)
  return false if instance?(@object)
  !! @object.instance_methods.find { |m| m.to_s == method_name.to_s}
end
is_a?(klass) click to toggle source
# File lib/must/rule.rb, line 131
def is_a?(klass)
  if instance?(klass)
    struct?(klass)
  else
    @object.is_a? klass
  end
end
method_defined?(method_name) click to toggle source
# File lib/must/rule.rb, line 127
def method_defined?(method_name)
  @object.respond_to?(method_name, true)
end