Class: Chewy::Journal::Entry
Overview
Describes a journal entry and provides necessary assisting methods
Constant Summary collapse
- ATTRIBUTES =
%w(index_name type_name action object_ids created_at).freeze
Class Method Summary collapse
-
.group(entries) ⇒ Object
Groups a list of entries by full type name to decrease a number of calls to Elasticsearch during journal apply.
-
.recent_timestamp(entries) ⇒ Object
Get the most recent timestamp from a list of entries.
-
.since(time, indices = []) ⇒ Object
Loads all entries since some time.
-
.subtract(from, what) ⇒ Object
Allows to filter one list of entries from another If any records with the same full type name are found then their object_ids will be subtracted.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #empty? ⇒ Boolean
- #full_type_name ⇒ Object
- #index ⇒ Object
-
#initialize(attributes = {}) ⇒ Entry
constructor
A new instance of Entry.
- #merge(other) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Entry
Returns a new instance of Entry.
9 10 11 12 13 |
# File 'lib/chewy/journal/entry.rb', line 9 def initialize(attributes = {}) attributes.slice(*ATTRIBUTES).each do |attr, value| public_send("#{attr}=", value) end end |
Class Method Details
.group(entries) ⇒ Object
Groups a list of entries by full type name to decrease a number of calls to Elasticsearch during journal apply
34 35 36 37 |
# File 'lib/chewy/journal/entry.rb', line 34 def self.group(entries) entries.group_by(&:full_type_name) .map { |_, grouped_entries| grouped_entries.reduce(:merge) } end |
.recent_timestamp(entries) ⇒ Object
Get the most recent timestamp from a list of entries
55 56 57 |
# File 'lib/chewy/journal/entry.rb', line 55 def self.(entries) entries.map { |entry| entry.created_at.to_i }.max end |
.since(time, indices = []) ⇒ Object
Loads all entries since some time
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/chewy/journal/entry.rb', line 18 def self.since(time, indices = []) query = Query.new(time, :gte, indices).to_h parameters = { index: Journal.index_name, type: Journal.type_name, body: query } size = Chewy.client.search(search_type: 'count', **parameters)['hits']['total'] if size > 0 Chewy.client .search(size: size, sort: 'created_at', **parameters)['hits']['hits'] .map { |r| new(r['_source']) } else [] end end |
.subtract(from, what) ⇒ Object
Allows to filter one list of entries from another If any records with the same full type name are found then their object_ids will be subtracted
43 44 45 46 47 48 49 50 51 |
# File 'lib/chewy/journal/entry.rb', line 43 def self.subtract(from, what) return from if what.empty? from.each do |from_entry| what.each do |what_entry| from_entry.object_ids -= what_entry.object_ids if from_entry == what_entry end end from.delete_if(&:empty?) end |
Instance Method Details
#==(other) ⇒ Object
74 75 76 |
# File 'lib/chewy/journal/entry.rb', line 74 def ==(other) full_type_name == other.try(:full_type_name) end |
#empty? ⇒ Boolean
78 79 80 |
# File 'lib/chewy/journal/entry.rb', line 78 def empty? !object_ids || object_ids.empty? end |
#full_type_name ⇒ Object
63 64 65 |
# File 'lib/chewy/journal/entry.rb', line 63 def full_type_name "#{index_name}##{type_name}" end |
#index ⇒ Object
59 60 61 |
# File 'lib/chewy/journal/entry.rb', line 59 def index @index ||= Chewy.derive_type(full_type_name) end |
#merge(other) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/chewy/journal/entry.rb', line 67 def merge(other) return self if other.nil? || full_type_name != other.full_type_name self.object_ids |= other.object_ids self.created_at = [created_at, other.created_at].compact.max self end |