Module: Autoselect::Operators

Included in:
Base
Defined in:
lib/autoselect/operators.rb

Overview

lib/autoselect/operators.rb

Provides operator parsing for autoselect model classes. Included by Autoselect and overrideable per-model. Operators are !-prefixed inline commands in the term string. Only the first matching operator is extracted; the rest of the string becomes the effective_term.

Order matters: more specific patterns must come before less specific ones.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

OPERATORS =
{
  recent_mine:    { pattern: /\A!u/,    client_only: false, trigger: '!u',  description: 'Recent records updated by you (last week)' },
  recent:         { pattern: /\A!r/,    client_only: false, trigger: '!r',  description: 'Recent records project-wide (last week)' },
  pinboard_top:   { pattern: /\A!!/,    client_only: false, trigger: '!!',  description: 'Current (topmost) pinboard item of this type' },
  pinboard:       { pattern: /\A!b/,    client_only: false, trigger: '!b',  description: 'All pinboard items of this type (in pin order)' },
  help:           { pattern: /\A!\?/,   client_only: true,  trigger: '!?',  description: 'Show help overlay' },
  new_record:    { pattern: /\A!n/,      client_only: true,  trigger: '!n',  description: 'Create a new record' },
  preferences:   { pattern: /\A!p/,      client_only: true,  trigger: '!p',  description: 'Open preferences (hide levels, set options)' },
  show_info:     { pattern: /\A!i/,      client_only: true,  trigger: '!i',  description: 'Toggle info display on/off' },
  external:      { pattern: /\A!e/,      client_only: true,  trigger: '!e',  description: 'Jump to leftmost external search level' },
  level_number:  { pattern: /\A!(\d+)/,  client_only: true,  trigger: '!N',  description: 'Jump to level N (e.g. !1, !2)' },
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



26
27
28
# File 'lib/autoselect/operators.rb', line 26

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#parse_operators(term) ⇒ Hash

Returns { operator: Symbol or nil, effective_term: String }.

Parameters:

  • term (String)

    the raw term including any operator prefix

Returns:

  • (Hash)

    { operator: Symbol or nil, effective_term: String }



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/autoselect/operators.rb', line 52

def parse_operators(term)
  return { operator: nil, effective_term: term.to_s.strip } if term.blank?

  self.class.operator_map.each do |key, definition|
    if m = term.match(definition[:pattern])
      effective_term = term[m[0].length..].strip
      return { operator: key, effective_term: }
    end
  end

  { operator: nil, effective_term: term.strip }
end