Module: Kerbi::Mixins::EntryTagLogic
- Extended by:
- ActiveSupport::Concern
- Included in:
- State::EntrySet
- Defined in:
- lib/mixins/entry_tag_logic.rb
Overview
Mixin for handling mission critical state entry tag logic. The logic is most comprised name resolution, i.e turning special words that users pass in place of literal tags, into literal tags.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- SPECIAL_CHAR =
"@"
- CANDIDATE_WORD =
"candidate"
- NEW_CANDIDATE_WORD =
"new-candidate"
- LATEST_WORD =
"latest"
- LATEST_PLUS_WORD =
"latest+\\."
- OLDEST_WORD =
"oldest"
- RANDOM_WORD =
"random"
- SPECIAL_READ_WORDS =
[ CANDIDATE_WORD, LATEST_PLUS_WORD, LATEST_WORD, OLDEST_WORD ]
- SPECIAL_WRITE_WORDS =
[ LATEST_PLUS_WORD, LATEST_WORD, OLDEST_WORD, CANDIDATE_WORD, NEW_CANDIDATE_WORD, RANDOM_WORD ]
Instance Method Summary collapse
-
#do_resolve_tag_expr(tag_expr, verb) ⇒ String
Main logic to template state entry tag expressions (that users use to identify state entries with) into a final, usable tag.
-
#resolve_candidate_read_word ⇒ String
Single word resolver.
-
#resolve_candidate_write_word ⇒ String
Single word resolver.
-
#resolve_latest_word ⇒ String
Single word resolver.
- #resolve_new_candidate_word ⇒ Object
-
#resolve_oldest_word ⇒ String
Single word resolver.
-
#resolve_random_word ⇒ String
Single word resolver.
-
#resolve_read_tag_expr(tag_expr) ⇒ String
Calls #do_resolve_tag_expr with verb=read in order to turn an entry tag expression like @latest into 2.1.1.
-
#resolve_word(word, verb) ⇒ Object
Performs a special word substitution for an individual special word, like ‘latest’, ‘random’, or ‘candidate’.
-
#resolve_write_tag_expr(tag_expr) ⇒ String
Calls #do_resolve_tag_expr with verb=write in order to turn an entry tag expression like @candidate-new into [cand]purple-forest-new.
Instance Method Details
#do_resolve_tag_expr(tag_expr, verb) ⇒ String
Main logic to template state entry tag expressions (that users use to identify state entries with) into a final, usable tag.
The method finds special words in the tag expression, which start with the SPECIAL_CHAR ‘@’, and substitutes them one at a time with a computed value. For instance, @latest will become the actual tag of the latest state entry, and @random will become a random string.
Depending on whether the user’s request is for reading or writing an entry, different substitutions are available.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mixins/entry_tag_logic.rb', line 73 def do_resolve_tag_expr(tag_expr, verb) raise "Internal error" unless %w[read write].include?(verb) words = verb == 'read' ? SPECIAL_READ_WORDS : SPECIAL_WRITE_WORDS resolved_tag = tag_expr words.each do |special_word| part = "#{SPECIAL_CHAR}#{special_word}" if tag_expr.include?(part) resolved_word = resolve_word(special_word, verb) resolved_tag = resolved_tag.gsub(part, resolved_word) end end resolved_tag end |
#resolve_candidate_read_word ⇒ String
Single word resolver. Looks for the latest candidate state entry and returns its tag or an empty string if there is no latest candidate state.
121 122 123 |
# File 'lib/mixins/entry_tag_logic.rb', line 121 def resolve_candidate_read_word latest_candidate&.tag || "" end |
#resolve_candidate_write_word ⇒ String
Single word resolver. Outputs a non-taken random tag (given by #generate_random_tag) prefixed with candidate flag prefix [cand]-.
129 130 131 |
# File 'lib/mixins/entry_tag_logic.rb', line 129 def resolve_candidate_write_word resolve_candidate_read_word || resolve_new_candidate_word end |
#resolve_latest_word ⇒ String
Single word resolver. Looks for the latest committed state entry and returns its tag or an empty string if there is no latest committed state.
146 147 148 |
# File 'lib/mixins/entry_tag_logic.rb', line 146 def resolve_latest_word latest&.tag || "" end |
#resolve_new_candidate_word ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/mixins/entry_tag_logic.rb', line 133 def resolve_new_candidate_word prefix = Kerbi::State::Entry::CANDIDATE_PREFIX begin tag = "#{prefix}#{self.class.generate_random_tag}" end while candidates.find{ |e| e.tag == tag } tag end |
#resolve_oldest_word ⇒ String
Single word resolver. Looks for the latest committed state entry and returns its tag or an empty string if there is no latest committed state.
155 156 157 |
# File 'lib/mixins/entry_tag_logic.rb', line 155 def resolve_oldest_word oldest&.tag || "" end |
#resolve_random_word ⇒ String
Single word resolver. Outputs a non-taken random tag (given by #generate_random_tag).
163 164 165 166 167 168 |
# File 'lib/mixins/entry_tag_logic.rb', line 163 def resolve_random_word begin tag = self.class.generate_random_tag end while entries.find{ |e|e.tag == tag } tag end |
#resolve_read_tag_expr(tag_expr) ⇒ String
Calls #do_resolve_tag_expr with verb=read in order to turn an entry tag expression like @latest into 2.1.1
See documentation for #resolve_word for information on how resolution works at the word level.
56 57 58 |
# File 'lib/mixins/entry_tag_logic.rb', line 56 def resolve_read_tag_expr(tag_expr) do_resolve_tag_expr(tag_expr, "read") end |
#resolve_word(word, verb) ⇒ Object
Performs a special word substitution for an individual special word, like ‘latest’, ‘random’, or ‘candidate’. Works by looking for a corresponding the word-resolver method in self.
E.g if you pass ‘random’, it expects the method resolve_random_word to exist.
Because the same special word can have different interpretations depending on whether the mode (read or write), this method will first look for the mode-specialized version of the word-resolver function, e.g if passed ‘candidate’ in ‘read’ mode, it will first look out for the a word-resolver method called ‘resolve_candidate_read_word’ and call it instead of the less specialized ‘resolve_candidate_word’ method.
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/mixins/entry_tag_logic.rb', line 105 def resolve_word(word, verb) word = word.gsub("-", "_") if respond_to?((method = "resolve_#{word}_#{verb}_word")) send(method) elsif respond_to?((method = "resolve_#{word}_word")) send(method) else raise "What is #{word}??" end end |
#resolve_write_tag_expr(tag_expr) ⇒ String
Calls #do_resolve_tag_expr with verb=write in order to turn an entry tag expression like @candidate-new into [cand]purple-forest-new.
See documentation for #resolve_word for information on how resolution works at the word level.
44 45 46 |
# File 'lib/mixins/entry_tag_logic.rb', line 44 def resolve_write_tag_expr(tag_expr) do_resolve_tag_expr(tag_expr, "write") end |