-
Notifications
You must be signed in to change notification settings - Fork 22
/
limit.rb
261 lines (237 loc) · 6.94 KB
/
limit.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# encoding: utf-8
module Axiom
class Relation
module Operation
# A class representing a limited relation
class Limit < Relation
include Unary
include Equalizer.new(:operand, :limit)
# Return the limit
#
# @example
# limit = limited_relation.limit
#
# @return [Integer]
#
# @api public
attr_reader :limit
# The relation sort order
#
# @return [Operation::Sorted::DirectionSet]
#
# @api private
attr_reader :directions
# Instantiate a new Limit
#
# @example
# limited_relation = Limit.new(operand, limit)
#
# @param [Relation] operand
# the relation to limit
# @param [Integer] limit
# the maximum number of tuples in the limited relation
#
# @return [Limit]
#
# @api public
def self.new(operand, limit)
assert_sorted_operand(operand)
assert_valid_limit(limit)
super
end
# Assert the operand is sorted
#
# @param [Relation] operand
#
# @return [undefined]
#
# @raise [SortededRelationRequiredError]
# raised if the operand is unsorted
#
# @api private
def self.assert_sorted_operand(operand)
if operand.header.size != operand.directions.size
fail SortededRelationRequiredError, 'can only limit a sorted operand'
end
end
# Assert the limit is valid
#
# @param [Integer] limit
#
# @return [undefined]
#
# @raise [InvalidLimitError]
# raised if the limit is less than 0
#
# @api private
def self.assert_valid_limit(limit)
if limit.nil? || limit < 0
fail InvalidLimitError, "limit must be greater than or equal to 0, but was #{limit.inspect}"
end
end
private_class_method :assert_sorted_operand, :assert_valid_limit
# Initialize a Limit
#
# @param [Relation] operand
# the relation to limit
# @param [Integer] limit
# the maximum number of tuples in the limited relation
#
# @return [undefined]
#
# @api private
def initialize(operand, limit)
super(operand)
@limit = limit
@directions = operand.directions
end
# Iterate over each tuple in the set
#
# @example
# limited_relation = Limit.new(operand, limit)
# limited_relation.each { |tuple| ... }
#
# @yield [tuple]
#
# @yieldparam [Tuple] tuple
# each tuple in the set
#
# @return [self]
#
# @api public
def each
return to_enum unless block_given?
operand.each_with_index do |tuple, index|
break if @limit == index
yield tuple
end
self
end
# Raise an exception when inserting into the Limit
#
# @example
# limit.insert(other) # => ImmutableRelationError raised
#
# @return [undefined]
#
# @raise [ImmutableRelationError]
# raised when inserting into the limit
#
# @api public
def insert(*)
fail ImmutableRelationError, 'inserting into a limit is impossible'
end
# Raise an exception when deleting from the Limit
#
# @example
# limit.delete(other) # => ImmutableRelationError raised
#
# @return [undefined]
#
# @raise [ImmutableRelationError]
# raised when deleting from the limit
#
# @api public
def delete(*)
fail ImmutableRelationError, 'deleting from a limit is impossible'
end
module Methods
# Default block used in #one
DEFAULT_ONE_BLOCK = -> {}
# Maximum number of tuples to take in #one
ONE_LIMIT = 2
# Return a relation with n tuples
#
# @example
# limited_relation = relation.take(5)
#
# @param [Integer] limit
# the maximum number of tuples in the limited relation
#
# @return [Limit]
#
# @api public
def take(limit)
Limit.new(self, limit)
end
# Return a relation with the first n tuples
#
# @example with no limit
# limited_relation = relation.first
#
# @example with a limit
# limited_relation = relation.first(5)
#
# @param [Integer] limit
# optional number of tuples from the beginning of the relation
#
# @return [Limit]
#
# @api public
def first(limit = 1)
take(limit)
end
# Return a relation with the last n tuples
#
# @example with no limit
# limited_relation = relation.last
#
# @example with a limit
# limited_relation = relation.last(5)
#
# @param [Integer] limit
# optional number of tuples from the end of the relation
#
# @return [Limit]
#
# @api public
def last(limit = 1)
reverse.take(limit).reverse
end
# Return a tuple if the relation contains exactly one tuple
#
# @example without a block
# tuple = relation.one
#
# @example with a block
# tuple = relation.one { ... }
#
# @yieldreturn [Object]
#
# @return [Tuple]
#
# @raise [NoTuplesError]
# raised if no tuples are returned
# @raise [ManyTuplesError]
# raised if more than one tuple is returned
#
# @api public
def one(&block)
block ||= DEFAULT_ONE_BLOCK
tuples = take(ONE_LIMIT).to_a
assert_no_more_than_one_tuple(tuples.size)
tuples.first or block.yield or
fail NoTuplesError, 'one tuple expected, but was an empty set'
end
private
# Assert no more than one tuple is returned
#
# @return [undefined]
#
# @raise [ManyTuplesError]
# raised if more than one tuple is returned
#
# @api private
def assert_no_more_than_one_tuple(size)
return if size <= 1
fail(
ManyTuplesError,
"one tuple expected, but set contained #{count} tuples"
)
end
end # module Methods
Relation.class_eval { include Methods }
end # class Limit
end # module Operation
end # class Relation
end # module Axiom