Class: Orchestrate::EventType::TimeSlice
- Inherits:
-
Object
- Object
- Orchestrate::EventType::TimeSlice
- Includes:
- Enumerable
- Defined in:
- lib/orchestrate/event_source.rb
Overview
Manages events in a specified duration of time.
Instance Attribute Summary collapse
-
#kv_item ⇒ KeyValue
readonly
The associated KeyValue.
-
#type ⇒ EventType
readonly
The associated EventType.
-
#type_name ⇒ String
readonly
The associated event type name.
Instance Method Summary collapse
-
#<<(body) ⇒ Orchestrate::Event
(also: #push)
Creates a new Event of the given type for the associated KeyValue.
-
#[](ordinal) ⇒ Orchestrate::Event
Retrieves a single ID by timestamp and ordinal.
-
#after(bound) ⇒ EventType::TimeSlice
Sets the exclusive start boundary for enumeration over events.
-
#before(bound) ⇒ EventType::TimeSlice
Sets the exclusive end boundary for enumeration over events.
-
#each {|event| ... } ⇒ Object
Iterates over events belonging to the KeyValue of the specified Type within the given bounds.
-
#end(bound) ⇒ EventType::TimeSlice
Sets the inclusive end boundary for enumeration over events.
-
#initialize(type, bounds = nil) ⇒ TimeSlice
constructor
Instantiates a new TimeSlice.
-
#lazy ⇒ Object
Creates a Lazy Enumerator for the EventList.
-
#perform(api_method, *args) ⇒ API::Response
Calls a method on the associated API Client, with collection, key and event_type being provided by EventType.
-
#start(bound) ⇒ EventType::TimeSlice
Sets the inclusive start boundary for enumeration over events.
-
#take(count) ⇒ Array
Returns the first n items.
Constructor Details
#initialize(type, bounds = nil) ⇒ TimeSlice
Instantiates a new TimeSlice
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/orchestrate/event_source.rb', line 193 def initialize(type, bounds=nil) @type = type @type_name = type.type @kv_item = type.kv_item if bounds.kind_of?(Hash) @bounds = bounds else @bounds = {} @bounds[:start] = bounds if bounds end @bounds[:limit] ||= 100 end |
Instance Attribute Details
#kv_item ⇒ KeyValue (readonly)
Returns The associated KeyValue.
180 181 182 |
# File 'lib/orchestrate/event_source.rb', line 180 def kv_item @kv_item end |
#type ⇒ EventType (readonly)
Returns The associated EventType.
174 175 176 |
# File 'lib/orchestrate/event_source.rb', line 174 def type @type end |
#type_name ⇒ String (readonly)
Returns The associated event type name.
177 178 179 |
# File 'lib/orchestrate/event_source.rb', line 177 def type_name @type_name end |
Instance Method Details
#<<(body) ⇒ Orchestrate::Event Also known as: push
Creates a new Event of the given type for the associated KeyValue.
218 219 220 221 |
# File 'lib/orchestrate/event_source.rb', line 218 def <<(body) response = perform(:post_event, body, @bounds[:start]) Event.from_bodyless_response(type, body, response) end |
#[](ordinal) ⇒ Orchestrate::Event
Retrieves a single ID by timestamp and ordinal. Uses the timestamp
value from the associated bounds, by start
, before
, after
, or
end
in that order.
229 230 231 232 233 234 235 236 237 |
# File 'lib/orchestrate/event_source.rb', line 229 def [](ordinal) begin = @bounds[:start] || @bounds[:before] || @bounds[:after] || @bounds[:end] response = perform(:get_event, , ordinal) Event.new(type, response) rescue API::NotFound nil end end |
#after(bound) ⇒ EventType::TimeSlice
Sets the exclusive start boundary for enumeration over events. Overwrites any value given to #start
295 296 297 298 299 |
# File 'lib/orchestrate/event_source.rb', line 295 def after(bound) @bounds[:after] = extract_bound_from(bound) @bounds.delete(:start) self end |
#before(bound) ⇒ EventType::TimeSlice
Sets the exclusive end boundary for enumeration over events. Overwrites any value given to #end
305 306 307 308 309 |
# File 'lib/orchestrate/event_source.rb', line 305 def before(bound) @bounds[:before] = extract_bound_from(bound) @bounds.delete(:end) self end |
#each {|event| ... } ⇒ Object
Iterates over events belonging to the KeyValue of the specified Type within the given bounds. Used as the basis for enumerable methods. Events are provided in reverse chronological order by timestamp and ordinal value.
249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/orchestrate/event_source.rb', line 249 def each(&block) @response = perform(:list_events, @bounds) return enum_for(:each) unless block_given? raise ResultsNotReady.new if type.kv_item.collection.app.inside_parallel? loop do @response.results.each do |listing| yield Event.from_listing(type, listing, @response) end break unless @response.next_link @response = @response.next_results end @response = nil end |
#end(bound) ⇒ EventType::TimeSlice
Sets the inclusive end boundary for enumeration over events. Overwrites any value given to #before
315 316 317 318 319 |
# File 'lib/orchestrate/event_source.rb', line 315 def end(bound) @bounds[:end] = extract_bound_from(bound) @bounds.delete(:before) self end |
#lazy ⇒ Object
Creates a Lazy Enumerator for the EventList. If called inside the app's
#in_parallel
block, will prefetch results.
266 267 268 269 |
# File 'lib/orchestrate/event_source.rb', line 266 def lazy return each.lazy if type.kv_item.collection.app.inside_parallel? super end |
#perform(api_method, *args) ⇒ API::Response
Calls a method on the associated API Client, with collection, key and event_type being provided by EventType.
211 212 213 |
# File 'lib/orchestrate/event_source.rb', line 211 def perform(api_method, *args) type.perform(api_method, *args) end |
#start(bound) ⇒ EventType::TimeSlice
Sets the inclusive start boundary for enumeration over events. Overwrites any value given to #before.
285 286 287 288 289 |
# File 'lib/orchestrate/event_source.rb', line 285 def start(bound) @bounds[:start] = extract_bound_from(bound) @bounds.delete(:after) self end |
#take(count) ⇒ Array
Returns the first n items. Equivalent to Enumerable#take. Sets the limit
parameter on the query to Orchestrate, so we don't ask for more than is needed.
275 276 277 278 279 |
# File 'lib/orchestrate/event_source.rb', line 275 def take(count) count = 1 if count < 1 @bounds[:limit] = count > 100 ? 100 : count super(count) end |