Class: Must::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/must/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Rule

Returns a new instance of Rule.



7
8
9
10
# File 'lib/must/rule.rb', line 7

def initialize(object)
  @object    = object
  @not       = false
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/must/rule.rb', line 5

def object
  @object
end

Instance Method Details

#aObject



12
# File 'lib/must/rule.rb', line 12

def a()  self end

#anObject



13
# File 'lib/must/rule.rb', line 13

def an() self end

#be(*args, &block) ⇒ Object



20
21
22
23
24
25
26
# 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) ⇒ Object



32
33
34
# File 'lib/must/rule.rb', line 32

def blank(&block)
  valid?(object.blank?, &block)
end

#coerced(*types, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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) ⇒ Object



86
87
88
# File 'lib/must/rule.rb', line 86

def duck(method_name, &block)
  valid?(duck?(method_name), &block)
end

#duck!(method_name, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
# 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) ⇒ Boolean

Returns:



90
91
92
93
94
95
96
# 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) ⇒ Object



28
29
30
# File 'lib/must/rule.rb', line 28

def empty(&block)
  valid?(object.empty?, &block)
end

#exist(&block) ⇒ Object



36
37
38
39
# File 'lib/must/rule.rb', line 36

def exist(&block)
  self.not()
  be(nil, &block)
end

#kind_of(*targets, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

#notObject



15
16
17
18
# File 'lib/must/rule.rb', line 15

def not
  @not = ! @not
  return self
end

#one_of(target, &block) ⇒ Object Also known as: match



57
58
59
# File 'lib/must/rule.rb', line 57

def one_of(target, &block)
  valid?(target === @object, &block)
end

#struct(target, &block) ⇒ Object



112
113
114
115
# 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) ⇒ Boolean

Returns:



108
109
110
# File 'lib/must/rule.rb', line 108

def struct?(target)
  Must::StructInfo.new(@object).same?(target)
end

#valid?(condition, &block) ⇒ Boolean

Returns:



62
63
64
65
66
67
68
# File 'lib/must/rule.rb', line 62

def valid?(condition, &block)
  if condition ^ @not
    object
  else
    block_or_throw(&block)
  end
end