Class: Kerbi::State::EntrySet
- Inherits:
-
Object
- Object
- Kerbi::State::EntrySet
- Includes:
- Mixins::EntryTagLogic
- Defined in:
- lib/state/entry_set.rb
Overview
Baby version of ActiveRecord::Relation. Holds an array of entries (i.e Kerbi::State::Entry) and exposes useful group-level operations, like sorting, find maximums, etc…
Constant Summary
Constants included from Mixins::EntryTagLogic
Mixins::EntryTagLogic::CANDIDATE_WORD, Mixins::EntryTagLogic::LATEST_WORD, Mixins::EntryTagLogic::NEW_CANDIDATE_WORD, Mixins::EntryTagLogic::OLDEST_WORD, Mixins::EntryTagLogic::RANDOM_WORD, Mixins::EntryTagLogic::SPECIAL_CHAR, Mixins::EntryTagLogic::SPECIAL_READ_WORDS, Mixins::EntryTagLogic::SPECIAL_WRITE_WORDS
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Instance Method Summary collapse
-
#candidates ⇒ Array<Kerbi::State::Entry>
Filters entries by candidate status, returning only the ones that ARE candidates.
-
#committed ⇒ Array<Kerbi::State::Entry>
Filters entries by candidate status, returning only the ones that are NOT candidates.
-
#find_by_literal_tag(tag_expr) ⇒ ?Kerbi::State::Entry
(also: #get)
Performs simple linear search for an entry whose tags matches exactly tag_expr.
-
#find_entry_for_read(tag_expr) ⇒ Kerbi::State::Entry
Given a target entry tag expression, searches underlying array for the corresponding entry.
-
#find_or_init_entry_for_write(tag_expr) ⇒ ?Kerbi::State::Entry
Given a target entry tag expression, searches underlying array for the corresponding entry.
-
#initialize(dicts) ⇒ EntrySet
constructor
A new instance of EntrySet.
-
#latest ⇒ ?Kerbi::State::Entry
Finds the most recently created/updated entry in the list that is not a candidate.
-
#latest_candidate ⇒ ?Kerbi::State::Entry
Finds the most recently created/updated entry in the list that is a candidate.
-
#oldest ⇒ ?Kerbi::State::Entry
Finds the least recently created/updated entry in the list that is not a candidate.
- #prune_candidates ⇒ Object
- #sort_by_created_at ⇒ Object
- #validate! ⇒ Object
Methods included from Mixins::EntryTagLogic
#do_resolve_tag_expr, #resolve_candidate_read_word, #resolve_candidate_write_word, #resolve_latest_word, #resolve_new_candidate_word, #resolve_oldest_word, #resolve_random_word, #resolve_read_tag_expr, #resolve_word, #resolve_write_tag_expr
Constructor Details
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
12 13 14 |
# File 'lib/state/entry_set.rb', line 12 def entries @entries end |
Instance Method Details
#candidates ⇒ Array<Kerbi::State::Entry>
Filters entries by candidate status, returning only the ones that ARE candidates.
42 43 44 |
# File 'lib/state/entry_set.rb', line 42 def candidates entries.reject(&:committed?) end |
#committed ⇒ Array<Kerbi::State::Entry>
Filters entries by candidate status, returning only the ones that are NOT candidates.
34 35 36 |
# File 'lib/state/entry_set.rb', line 34 def committed entries.select(&:committed?) end |
#find_by_literal_tag(tag_expr) ⇒ ?Kerbi::State::Entry Also known as: get
Performs simple linear search for an entry whose tags matches exactly tag_expr.
118 119 120 121 |
# File 'lib/state/entry_set.rb', line 118 def find_by_literal_tag(tag_expr) return nil unless tag_expr.present? entries.find { |e| e.tag == tag_expr } end |
#find_entry_for_read(tag_expr) ⇒ Kerbi::State::Entry
Given a target entry tag expression, searches underlying array for the corresponding entry.
Assumes the tag expression contains special interpolatable words, and thus resolves the tag expression into a literal tag first.
Invokes tag resolution logic specific to reading entries, which is different than for writing entries (see #resolve_read_tag_expr).
81 82 83 84 85 86 |
# File 'lib/state/entry_set.rb', line 81 def find_entry_for_read(tag_expr) resolved_tag = resolve_read_tag_expr(tag_expr) entry = find_by_literal_tag(resolved_tag) raise Kerbi::StateNotFoundError.new(tag_expr) unless entry entry end |
#find_or_init_entry_for_write(tag_expr) ⇒ ?Kerbi::State::Entry
Given a target entry tag expression, searches underlying array for the corresponding entry.
Assumes the tag expression contains special interpolatable words, and thus resolves the tag expression into a literal tag first.
Invokes tag resolution logic specific to writing entries, which is different than for reading entries (see #resolve_write_tag_expr).
If an entry is not found, initializes a new empty entry with the given resolved tag, and adds it to the set’s underlying array.
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/state/entry_set.rb', line 102 def find_or_init_entry_for_write(tag_expr) resolved_tag = resolve_write_tag_expr(tag_expr) if(existing_entry = find_by_literal_tag(resolved_tag)) existing_entry else entry = Kerbi::State::Entry.new(self, tag: resolved_tag) entries.unshift(entry) entry end end |
#latest ⇒ ?Kerbi::State::Entry
Finds the most recently created/updated entry in the list that is not a candidate.
50 51 52 |
# File 'lib/state/entry_set.rb', line 50 def latest committed.first end |
#latest_candidate ⇒ ?Kerbi::State::Entry
Finds the most recently created/updated entry in the list that is a candidate.
66 67 68 |
# File 'lib/state/entry_set.rb', line 66 def latest_candidate candidates.first end |
#oldest ⇒ ?Kerbi::State::Entry
Finds the least recently created/updated entry in the list that is not a candidate.
58 59 60 |
# File 'lib/state/entry_set.rb', line 58 def oldest committed.last end |
#prune_candidates ⇒ Object
125 126 127 |
# File 'lib/state/entry_set.rb', line 125 def prune_candidates entries.select!(&:committed?) end |
#sort_by_created_at ⇒ Object
129 130 131 132 133 134 |
# File 'lib/state/entry_set.rb', line 129 def sort_by_created_at entries.sort! do |a, b| both_defined = a.created_at && b.created_at both_defined ? b.created_at <=> a.created_at : 0 end end |
#validate! ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/state/entry_set.rb', line 20 def validate! entries.each(&:validate) if (bad_entries = entries.reject(&:valid?)).any? errors = Hash[bad_entries.map do |entry| [entry.tag, entry.validation_errors.deep_dup] end] raise Kerbi::EntryValidationError.new(errors) end end |