Class: Algebrick::Matchers::Abstract

Inherits:
Object
  • Object
show all
Includes:
TypeCheck
Defined in:
lib/algebrick/matchers/abstract.rb

Direct Known Subclasses

AbstractLogic, Any, Array, Many, Not, Product, Wrapper

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from TypeCheck

#Child!, #Child?, #Match!, #Match?, #Type!, #Type?, error

Constructor Details

- (Abstract) initialize

Returns a new instance of Abstract



21
22
23
# File 'lib/algebrick/matchers/abstract.rb', line 21

def initialize
  @assign, @value, @matched = nil
end

Instance Attribute Details

- (Object) value (readonly)

Returns the value of attribute value



19
20
21
# File 'lib/algebrick/matchers/abstract.rb', line 19

def value
  @value
end

Instance Method Details

- (Object) !



52
53
54
# File 'lib/algebrick/matchers/abstract.rb', line 52

def !
  Not.new self
end

- (Object) &(matcher)



44
45
46
# File 'lib/algebrick/matchers/abstract.rb', line 44

def &(matcher)
  And.new self, matcher
end

- (Object) ==(other)

Raises:

  • (NotImplementedError)


102
103
104
# File 'lib/algebrick/matchers/abstract.rb', line 102

def ==(other)
  raise NotImplementedError
end

- (Object) ===(other)



82
83
84
# File 'lib/algebrick/matchers/abstract.rb', line 82

def ===(other)
  matching?(other).tap { |matched| @value = other if (@matched = matched) }
end

- (Object) >(block) Also known as: >>



31
32
33
# File 'lib/algebrick/matchers/abstract.rb', line 31

def >(block)
  return self, block
end

- (Object) assign! Also known as: ~



37
38
39
40
# File 'lib/algebrick/matchers/abstract.rb', line 37

def assign!
  @assign = true
  self
end

- (Boolean) assign?

Returns:



56
57
58
# File 'lib/algebrick/matchers/abstract.rb', line 56

def assign?
  @assign
end

- (Object) assign_to_s



86
87
88
# File 'lib/algebrick/matchers/abstract.rb', line 86

def assign_to_s
  assign? ? '~' : ''
end

- (Boolean) assigned?

Returns:



60
61
62
# File 'lib/algebrick/matchers/abstract.rb', line 60

def assigned?
  !!@value
end

- (Object) assigns



72
73
74
75
76
# File 'lib/algebrick/matchers/abstract.rb', line 72

def assigns
  collect_assigns.tap do
    return yield *assigns if block_given?
  end
end

- (Object) case(&block) Also known as: when



25
26
27
# File 'lib/algebrick/matchers/abstract.rb', line 25

def case(&block)
  return self, block
end

- (Object) children

Raises:

  • (NotImplementedError)


94
95
96
# File 'lib/algebrick/matchers/abstract.rb', line 94

def children
  raise NotImplementedError
end

- (Object) children_including_self



68
69
70
# File 'lib/algebrick/matchers/abstract.rb', line 68

def children_including_self
  children.unshift self
end

- (Object) collect_assigns (private)



116
117
118
119
# File 'lib/algebrick/matchers/abstract.rb', line 116

def collect_assigns
  mine = @assign ? [@value] : []
  children.inject(mine) { |assigns, child| assigns + child.assigns }
end

- (Object) find_children(collection) (private)



126
127
128
129
130
# File 'lib/algebrick/matchers/abstract.rb', line 126

def find_children(collection)
  collection.map do |matcher|
    matcher if matcher.kind_of? Abstract
  end.compact
end

- (Object) inspect



90
91
92
# File 'lib/algebrick/matchers/abstract.rb', line 90

def inspect
  to_s
end

- (Object) matchable!(obj) (private)

Raises:

  • (ArgumentError)


121
122
123
124
# File 'lib/algebrick/matchers/abstract.rb', line 121

def matchable!(obj)
  raise ArgumentError, 'object does not respond to :===' unless obj.respond_to? :===
  obj
end

- (Boolean) matched?

Returns:



64
65
66
# File 'lib/algebrick/matchers/abstract.rb', line 64

def matched?
  @matched
end

- (Boolean) matching?(other) (protected)

Returns:

Raises:

  • (NotImplementedError)


110
111
112
# File 'lib/algebrick/matchers/abstract.rb', line 110

def matching?(other)
  raise NotImplementedError
end

- (Object) to_a



78
79
80
# File 'lib/algebrick/matchers/abstract.rb', line 78

def to_a
  assigns
end

- (Object) to_s

Raises:

  • (NotImplementedError)


98
99
100
# File 'lib/algebrick/matchers/abstract.rb', line 98

def to_s
  raise NotImplementedError
end

- (Object) |(matcher)



48
49
50
# File 'lib/algebrick/matchers/abstract.rb', line 48

def |(matcher)
  Or.new self, matcher
end