-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpreloaded_event_relation.rb
More file actions
97 lines (77 loc) · 1.84 KB
/
preloaded_event_relation.rb
File metadata and controls
97 lines (77 loc) · 1.84 KB
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
# frozen_string_literal: true
# Wrapper class to make preloaded event arrays compatible with ActiveRecord::Relation API
# This allows existing code that calls methods like `pluck`, `map`, `select` to work
# with in-memory arrays without modification.
class PreloadedEventRelation
include Enumerable
def initialize(events)
@events = Array(events)
end
# Delegate Enumerable methods to the underlying array
def each(&block)
@events.each(&block)
end
# Delegate common Enumerable methods explicitly
def first
@events.first
end
def last
@events.last
end
def count
@events.count
end
# Implement pluck to match ActiveRecord::Relation#pluck behavior
# Supports single or multiple column names
def pluck(*column_names)
if column_names.length == 1
column_name = column_names.first
@events.map { |event| event.public_send(column_name) }
else
@events.map { |event| column_names.map { |col| event.public_send(col) } }
end
end
# Delegate map to the underlying array
def map(&block)
@events.map(&block)
end
# Delegate select to the underlying array
def select(&block)
PreloadedEventRelation.new(@events.select(&block))
end
# Delegate other common Enumerable methods
def compact
PreloadedEventRelation.new(@events.compact)
end
def uniq
PreloadedEventRelation.new(@events.uniq)
end
def sort_by(&block)
PreloadedEventRelation.new(@events.sort_by(&block))
end
def group_by(&block)
@events.group_by(&block)
end
def inject(*args, &block)
@events.inject(*args, &block)
end
def length
@events.length
end
def empty?
@events.empty?
end
def present?
@events.present?
end
def blank?
@events.blank?
end
# Allow direct access to the underlying array
def to_a
@events
end
def to_ary
@events
end
end