Class: Orchestrate::EventType

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/orchestrate/event_source.rb

Overview

Manages events of a specific type that belong to a specific KeyValue item.

Defined Under Namespace

Classes: TimeSlice

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kv_item, event_type) ⇒ EventType

Instantiates a new EventType.

Parameters:

  • kv_item (Orchestrate::KeyValue)

    The KeyValue this EventType manages events for.

  • event_type (#to_s)

    The type of events this EventType manages.



49
50
51
52
# File 'lib/orchestrate/event_source.rb', line 49

def initialize(kv_item, event_type)
  @kv_item = kv_item
  @type = event_type.to_s
end

Instance Attribute Details

#kv_itemOrchestrate::KeyValue (readonly)

Returns The KeyValue this EventType is managing events for.

Returns:



41
42
43
# File 'lib/orchestrate/event_source.rb', line 41

def kv_item
  @kv_item
end

#typeString (readonly)

Returns The name for the type of events this EventType manages.

Returns:

  • (String)

    The name for the type of events this EventType manages.



44
45
46
# File 'lib/orchestrate/event_source.rb', line 44

def type
  @type
end

Instance Method Details

#<<(body) ⇒ Orchestrate::Event Also known as: push

Creates a new Event of the given type for the associated KeyValue.

Parameters:

  • body (#to_json)

    The value for the event.

Returns:



89
90
91
# File 'lib/orchestrate/event_source.rb', line 89

def <<(body)
  TimeSlice.new(self).push(body)
end

#<=>(other) ⇒ nil, ...

Equivalent to String#<=>. Comapres by key_value and type.

Parameters:

Returns:

  • (nil, -1, 0, 1)


67
68
69
70
71
# File 'lib/orchestrate/event_source.rb', line 67

def <=>(other)
  return nil unless other.kind_of?(Orchestrate::EventType)
  return nil unless other.kv_item == kv_item
  other.type <=> type
end

#==(other) ⇒ true, false Also known as: eql?

Equivalent to String#==. Compares by KeyValue and Type.

Parameters:

Returns:

  • (true, false)


57
58
59
60
61
# File 'lib/orchestrate/event_source.rb', line 57

def ==(other)
  other.kind_of?(Orchestrate::EventType) && \
    other.kv_item == kv_item && \
    other.type == type
end

#[](bounds) ⇒ Object

Instantiates a new EventType::List with the given bounds. Can be used to access single events or create events with a specific timestamp.

Examples:

Accessing an existing event by timestamp/ordinal

kv.events[:checkins][timestamp][1]

Creating an Event with a specified timestamp

kv.events[:checkins][Time.now - 3600] << {"place" => "home"}

Listing events within a time range

kv.events[:checkins][{before: Time.now - 3600, after: Time.now - 24 * 3600}].to_a
# equivalent to:
# kv.events[:checkins].before(Time.now - 3600).after(Time.now - 24 * 3600).to_a

Parameters:

  • bounds (#to_s, Time, Date, Integer, Hash)

    The bounds value for the List.

See Also:

  • EventType::List#initialize


106
107
108
# File 'lib/orchestrate/event_source.rb', line 106

def [](bounds)
  TimeSlice.new(self, bounds)
end

#after(bound) ⇒ EventType::TimeSlice

Sets the exclusive start boundary for enumeration over events. Overwrites any value given to #start

Parameters:

Returns:



150
151
152
# File 'lib/orchestrate/event_source.rb', line 150

def after(bound)
  TimeSlice.new(self).after(bound)
end

#before(bound) ⇒ EventType::TimeSlice

Sets the exclusive end boundary for enumeration over events. Overwrites any value given to #end

Parameters:

Returns:



158
159
160
# File 'lib/orchestrate/event_source.rb', line 158

def before(bound)
  TimeSlice.new(self).before(bound)
end

#each {|event| ... } ⇒ Object

Iterates over events belonging to the KeyValue of the specified Type. Used as the basis for enumerable methods. Events are provided in reverse chronological order by timestamp and ordinal value.

Yield Parameters:



119
120
121
# File 'lib/orchestrate/event_source.rb', line 119

def each(&block)
  TimeSlice.new(self).each(&block)
end

#end(bound) ⇒ EventType::TimeSlice

Sets the inclusive end boundary for enumeration over events. Overwrites any value given to #before

Parameters:

Returns:



166
167
168
# File 'lib/orchestrate/event_source.rb', line 166

def end(bound)
  TimeSlice.new(self).start(bound)
end

#lazyObject

Creates a Lazy Enumerator for the EventList. If called inside the app's #in_parallel block, will prefetch results.

Returns:

  • Enumerator::Lazy



126
127
128
# File 'lib/orchestrate/event_source.rb', line 126

def lazy
  TimeSlice.new(self).lazy
end

#perform(api_method, *args) ⇒ API::Response

Calls a method on the KeyValue's Collection's API Client, providing the event type.

Parameters:

  • api_method (Symbol)

    The method on the client to call.

  • args (#to_s, #to_json, Hash)

    The remaining arguments for the specified method.

Returns:



82
83
84
# File 'lib/orchestrate/event_source.rb', line 82

def perform(api_method, *args)
  kv_item.perform(api_method, type, *args)
end

#start(bound) ⇒ EventType::TimeSlice

Sets the inclusive start boundary for enumeration over events. Overwrites any value given to #before.

Parameters:

Returns:



142
143
144
# File 'lib/orchestrate/event_source.rb', line 142

def start(bound)
  TimeSlice.new(self).start(bound)
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.

Parameters:

  • count (Integer)

    The number of events to limit to.

Returns:

  • (Array)


134
135
136
# File 'lib/orchestrate/event_source.rb', line 134

def take(count)
  TimeSlice.new(self).take(count)
end

#to_sObject

Returns A pretty-printed string representation of the EventType.

Returns:

  • A pretty-printed string representation of the EventType



74
75
76
# File 'lib/orchestrate/event_source.rb', line 74

def to_s
  "#<Orchestrate::EventType key_value=#{kv_item} type=#{type}>"
end