Class: RubyEventStore::ROM::Relations::Events

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_event_store/rom/relations/events.rb

Constant Summary collapse

DIRECTION_MAP =
{ forward: %i[asc > <], backward: %i[desc < >] }.freeze

Instance Method Summary collapse

Instance Method Details

#by_event_id(event_id) ⇒ Object



29
30
31
# File 'lib/ruby_event_store/rom/relations/events.rb', line 29

def by_event_id(event_id)
  where(event_id: event_id)
end

#by_event_type(types) ⇒ Object



33
34
35
# File 'lib/ruby_event_store/rom/relations/events.rb', line 33

def by_event_type(types)
  where(event_type: types)
end

#create_changeset(tuples) ⇒ Object



21
22
23
# File 'lib/ruby_event_store/rom/relations/events.rb', line 21

def create_changeset(tuples)
  events.changeset(Changesets::CreateEvents, tuples)
end

#newer_than(time, time_sort_by) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/ruby_event_store/rom/relations/events.rb', line 37

def newer_than(time, time_sort_by)
  if time_sort_by == :as_of
    where { |r| string::coalesce(r.events[:valid_at], r.events[:created_at]) > time.localtime }
  else
    where { |r| r.events[:created_at] > time.localtime }
  end
end

#newer_than_or_equal(time, time_sort_by) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/ruby_event_store/rom/relations/events.rb', line 45

def newer_than_or_equal(time, time_sort_by)
  if time_sort_by == :as_of
    where { |r| string::coalesce(r.events[:valid_at], r.events[:created_at]) >= time.localtime }
  else
    where { |r| r.events[:created_at] >= time.localtime }
  end
end

#older_than(time, time_sort_by) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/ruby_event_store/rom/relations/events.rb', line 53

def older_than(time, time_sort_by)
  if time_sort_by == :as_of
    where { |r| string::coalesce(r.events[:valid_at], r.events[:created_at]) < time.localtime }
  else
    where { |r| r.events[:created_at] < time.localtime }
  end
end

#older_than_or_equal(time, time_sort_by) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/ruby_event_store/rom/relations/events.rb', line 61

def older_than_or_equal(time, time_sort_by)
  if time_sort_by == :as_of
    where { |r| string::coalesce(r.events[:valid_at], r.events[:created_at]) <= time.localtime }
  else
    where { |r| r.events[:created_at] <= time.localtime }
  end
end

#ordered(direction, offset_entry_id = nil, stop_entry_id = nil, time_sort_by = nil) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ruby_event_store/rom/relations/events.rb', line 71

def ordered(direction, offset_entry_id = nil, stop_entry_id = nil, time_sort_by = nil)
  order, operator_offset, operator_stop = DIRECTION_MAP[direction]

  raise ArgumentError, "Direction must be :forward or :backward" if order.nil?

  event_order_columns = [:id]

  case time_sort_by
  when :as_at
    event_order_columns.unshift :created_at
  when :as_of
    event_order_columns.unshift :valid_at
  end

  query = self
  query = query.where { id.public_send(operator_offset, offset_entry_id) } if offset_entry_id
  query = query.where { id.public_send(operator_stop, stop_entry_id) } if stop_entry_id

  query.order(*event_order_columns.map { |c| events[c].public_send(order) })
end

#update_changeset(tuples) ⇒ Object



25
26
27
# File 'lib/ruby_event_store/rom/relations/events.rb', line 25

def update_changeset(tuples)
  events.changeset(Changesets::UpdateEvents, tuples)
end