@@ -18,7 +18,7 @@ def initialize(reader, result = SpecificationResult.new)
1818 # @param stream_name [String] name of the stream to get events from
1919 # @return [Specification]
2020 def stream ( stream_name )
21- Specification . new ( reader , result . dup { | r | r . stream = Stream . new ( stream_name ) } )
21+ Specification . new ( reader , result . with ( stream : Stream . new ( stream_name ) ) )
2222 end
2323
2424 # Limits the query to events before or after another event.
@@ -28,7 +28,8 @@ def stream(stream_name)
2828 # @return [Specification]
2929 def from ( start )
3030 raise InvalidPageStart if start . nil? || start . empty?
31- Specification . new ( reader , result . dup { |r | r . start = start } )
31+
32+ Specification . new ( reader , result . with ( start : start ) )
3233 end
3334
3435 # Limits the query to events before or after another event.
@@ -38,7 +39,8 @@ def from(start)
3839 # @return [Specification]
3940 def to ( stop )
4041 raise InvalidPageStop if stop . nil? || stop . empty?
41- Specification . new ( reader , result . dup { |r | r . stop = stop } )
42+
43+ Specification . new ( reader , result . with ( stop : stop ) )
4244 end
4345
4446 # Limits the query to events that occurred before given time.
@@ -48,13 +50,8 @@ def to(stop)
4850 # @return [Specification]
4951 def older_than ( time )
5052 raise ArgumentError unless time . respond_to? ( :to_time )
51- Specification . new (
52- reader ,
53- result . dup do |r |
54- r . older_than = time
55- r . older_than_or_equal = nil
56- end ,
57- )
53+
54+ Specification . new ( reader , result . with ( older_than : time , older_than_or_equal : nil ) )
5855 end
5956
6057 # Limits the query to events that occurred on or before given time.
@@ -64,13 +61,8 @@ def older_than(time)
6461 # @return [Specification]
6562 def older_than_or_equal ( time )
6663 raise ArgumentError unless time . respond_to? ( :to_time )
67- Specification . new (
68- reader ,
69- result . dup do |r |
70- r . older_than = nil
71- r . older_than_or_equal = time
72- end ,
73- )
64+
65+ Specification . new ( reader , result . with ( older_than : nil , older_than_or_equal : time ) )
7466 end
7567
7668 # Limits the query to events that occurred after given time.
@@ -80,13 +72,8 @@ def older_than_or_equal(time)
8072 # @return [Specification]
8173 def newer_than ( time )
8274 raise ArgumentError unless time . respond_to? ( :to_time )
83- Specification . new (
84- reader ,
85- result . dup do |r |
86- r . newer_than_or_equal = nil
87- r . newer_than = time
88- end ,
89- )
75+
76+ Specification . new ( reader , result . with ( newer_than : time , newer_than_or_equal : nil ) )
9077 end
9178
9279 # Limits the query to events that occurred on or after given time.
@@ -96,13 +83,8 @@ def newer_than(time)
9683 # @return [Specification]
9784 def newer_than_or_equal ( time )
9885 raise ArgumentError unless time . respond_to? ( :to_time )
99- Specification . new (
100- reader ,
101- result . dup do |r |
102- r . newer_than_or_equal = time
103- r . newer_than = nil
104- end ,
105- )
86+
87+ Specification . new ( reader , result . with ( newer_than : nil , newer_than_or_equal : time ) )
10688 end
10789
10890 # Limits the query to events within given time range.
@@ -123,31 +105,31 @@ def between(time_range)
123105 #
124106 # @return [Specification]
125107 def as_at
126- Specification . new ( reader , result . dup { | r | r . time_sort_by = :as_at } )
108+ Specification . new ( reader , result . with ( time_sort_by : :as_at ) )
127109 end
128110
129111 # Sets the order of time sorting using validity time
130112 # {http://railseventstore.org/docs/read/ Find out more}
131113 #
132114 # @return [Specification]
133115 def as_of
134- Specification . new ( reader , result . dup { | r | r . time_sort_by = :as_of } )
116+ Specification . new ( reader , result . with ( time_sort_by : :as_of ) )
135117 end
136118
137119 # Sets the order of reading events to ascending (forward from the start).
138120 # {http://railseventstore.org/docs/read/ Find out more}.
139121 #
140122 # @return [Specification]
141123 def forward
142- Specification . new ( reader , result . dup { | r | r . direction = :forward } )
124+ Specification . new ( reader , result . with ( direction : :forward ) )
143125 end
144126
145127 # Sets the order of reading events to descending (backward from the start).
146128 # {http://railseventstore.org/docs/read/ Find out more}.
147129 #
148130 # @return [Specification]
149131 def backward
150- Specification . new ( reader , result . dup { | r | r . direction = :backward } )
132+ Specification . new ( reader , result . with ( direction : :backward ) )
151133 end
152134
153135 # Limits the query to specified number of events.
@@ -157,7 +139,8 @@ def backward
157139 # @return [Specification]
158140 def limit ( count )
159141 raise InvalidPageSize unless count && count > 0
160- Specification . new ( reader , result . dup { |r | r . count = count } )
142+
143+ Specification . new ( reader , result . with ( limit : count ) )
161144 end
162145
163146 # Executes the query based on the specification built up to this point.
@@ -166,10 +149,10 @@ def limit(count)
166149 #
167150 # @yield [Array<Event>] batch of events
168151 # @return [Enumerator, nil] Enumerator is returned when block not given
169- def each_batch
152+ def each_batch ( & block )
170153 return to_enum ( :each_batch ) unless block_given?
171154
172- reader . each ( in_batches ( result . batch_size ) . result ) { | batch | yield batch }
155+ reader . each ( in_batches ( result . batch_size ) . result , & block )
173156 end
174157
175158 # Executes the query based on the specification built up to this point.
@@ -178,10 +161,10 @@ def each_batch
178161 #
179162 # @yield [Event] event
180163 # @return [Enumerator, nil] Enumerator is returned when block not given
181- def each
164+ def each ( & block )
182165 return to_enum unless block_given?
183166
184- each_batch { |batch | batch . each { | event | yield event } }
167+ each_batch { |batch | batch . each ( & block ) }
185168 end
186169
187170 # Executes the query based on the specification built up to this point
@@ -191,6 +174,7 @@ def each
191174 # @return [Array] of mapped result
192175 def map ( &block )
193176 raise ArgumentError . new ( "Block must be given" ) unless block_given?
177+
194178 each . map ( &block )
195179 end
196180
@@ -202,6 +186,7 @@ def map(&block)
202186 # @return reduce result as defined by block given
203187 def reduce ( accumulator = nil , &block )
204188 raise ArgumentError . new ( "Block must be given" ) unless block_given?
189+
205190 each . reduce ( accumulator , &block )
206191 end
207192
@@ -236,13 +221,7 @@ def to_a
236221 # @param batch_size [Integer] number of events to read in a single batch
237222 # @return [Specification]
238223 def in_batches ( batch_size = DEFAULT_BATCH_SIZE )
239- Specification . new (
240- reader ,
241- result . dup do |r |
242- r . read_as = :batch
243- r . batch_size = batch_size
244- end ,
245- )
224+ Specification . new ( reader , result . with ( read_as : :batch , batch_size : batch_size ) )
246225 end
247226 alias in_batches_of in_batches
248227
@@ -251,15 +230,15 @@ def in_batches(batch_size = DEFAULT_BATCH_SIZE)
251230 #
252231 # @return [Specification]
253232 def read_first
254- Specification . new ( reader , result . dup { | r | r . read_as = :first } )
233+ Specification . new ( reader , result . with ( read_as : :first ) )
255234 end
256235
257236 # Specifies that only last event should be read.
258237 # {http://railseventstore.org/docs/read/ Find out more}.
259238 #
260239 # @return [Specification]
261240 def read_last
262- Specification . new ( reader , result . dup { | r | r . read_as = :last } )
241+ Specification . new ( reader , result . with ( read_as : :last ) )
263242 end
264243
265244 # Executes the query based on the specification built up to this point.
@@ -286,17 +265,17 @@ def last
286265 # @types [Class, Array(Class)] types of event to look for.
287266 # @return [Specification]
288267 def of_type ( *types )
289- Specification . new ( reader , result . dup { | r | r . with_types = types . flatten } )
268+ Specification . new ( reader , result . with ( with_types : types . flatten . map ( & :to_s ) ) )
290269 end
291- alias_method : of_types, : of_type
270+ alias of_types of_type
292271
293272 # Limits the query to certain events by given even ids.
294273 # {http://railseventstore.org/docs/read/ Find out more}.
295274 #
296275 # @param event_ids [Array(String)] ids of event to look for.
297276 # @return [Specification]
298277 def with_id ( event_ids )
299- Specification . new ( reader , result . dup { | r | r . with_ids = event_ids } )
278+ Specification . new ( reader , result . with ( with_ids : event_ids ) )
300279 end
301280
302281 # Reads single event from repository.
0 commit comments