muack 1.2.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,100 +0,0 @@
1
-
2
- module Muack
3
- class Satisfy < Struct.new(:block, :api_args)
4
- def match actual_arg
5
- !!block.call(actual_arg)
6
- end
7
-
8
- def | rhs; Satisfy::Disj.new(self, rhs); end
9
- def & rhs; Satisfy::Conj.new(self, rhs); end
10
-
11
- class Disj < Satisfy
12
- def initialize lhs, rhs
13
- @lhs, @rhs = lhs, rhs
14
- super(lambda{ |actual_arg| lhs.match(actual_arg) ||
15
- rhs.match(actual_arg) })
16
- end
17
-
18
- def to_s; "#{@lhs} | #{@rhs}"; end
19
- alias_method :inspect, :to_s
20
- end
21
-
22
- class Conj < Satisfy
23
- def initialize lhs, rhs
24
- @lhs, @rhs = lhs, rhs
25
- super(lambda{ |actual_arg| lhs.match(actual_arg) &&
26
- rhs.match(actual_arg) })
27
- end
28
-
29
- def to_s; "#{@lhs} & #{@rhs}"; end
30
- alias_method :inspect, :to_s
31
- end
32
-
33
- def to_s
34
- "Muack::API.#{api_name}(#{api_args.map(&:inspect).join(', ')})"
35
- end
36
- alias_method :inspect, :to_s
37
-
38
- def api_name
39
- (self.class.name || 'Unknown')[/(::)*(\w+)$/, 2].
40
- gsub(/([A-Z][a-z]*)+?(?=[A-Z][a-z]*)/, '\\1_').downcase
41
- end
42
-
43
- def api_args
44
- super || [block]
45
- end
46
- end
47
-
48
- class IsA < Satisfy
49
- def initialize klass
50
- super lambda{ |actual_arg| actual_arg.kind_of?(klass) }, [klass]
51
- end
52
- end
53
-
54
- class Anything < Satisfy
55
- def initialize
56
- super lambda{ |_| true }, []
57
- end
58
- end
59
-
60
- class Match < Satisfy
61
- def initialize regexp
62
- super lambda{ |actual_arg| regexp.match(actual_arg) }, [regexp]
63
- end
64
- end
65
-
66
- class HashIncluding < Satisfy
67
- def initialize hash
68
- super lambda{ |actual_arg|
69
- actual_arg.values_at(*hash.keys).zip(hash.values).all? do |(av, ev)|
70
- if ev.kind_of?(Satisfy)
71
- ev.match(av)
72
- else
73
- ev == av
74
- end
75
- end
76
- }, [hash]
77
- end
78
- end
79
-
80
- class Including < Satisfy
81
- def initialize element
82
- super lambda{ |actual_arg|
83
- actual_arg.include?(element) }, [element]
84
- end
85
- end
86
-
87
- class Within < Satisfy
88
- def initialize range_or_array
89
- super lambda{ |actual_arg| range_or_array.include?(actual_arg) },
90
- [range_or_array]
91
- end
92
- end
93
-
94
- class RespondTo < Satisfy
95
- def initialize *messages
96
- super lambda{ |actual_arg|
97
- messages.all?{ |msg| actual_arg.respond_to?(msg) } }, messages
98
- end
99
- end
100
- end