Class: Workarea::QueryString
- Inherits:
-
Object
- Object
- Workarea::QueryString
- Includes:
- GlobalID::Identification
- Defined in:
- app/models/workarea/query_string.rb
Constant Summary collapse
- INVALID_SEARCH_REGEX =
/[\]\[:\.;\(\)\{\}^\\\/!~]|<\/?script>|(\s+)?-(\s|$)/
Instance Attribute Summary collapse
-
#original ⇒ Object
readonly
Returns the value of attribute original.
Class Method Summary collapse
Instance Method Summary collapse
- #all? ⇒ Boolean
- #id ⇒ Object
-
#initialize(original) ⇒ QueryString
constructor
A new instance of QueryString.
- #phrase? ⇒ Boolean
- #pieces ⇒ Object
- #pretty ⇒ Object
- #sanitized ⇒ Object
- #short? ⇒ Boolean
Constructor Details
#initialize(original) ⇒ QueryString
Returns a new instance of QueryString.
14 15 16 |
# File 'app/models/workarea/query_string.rb', line 14 def initialize(original) @original = original end |
Instance Attribute Details
#original ⇒ Object (readonly)
Returns the value of attribute original.
7 8 9 |
# File 'app/models/workarea/query_string.rb', line 7 def original @original end |
Class Method Details
.find(id) ⇒ Object
10 11 12 |
# File 'app/models/workarea/query_string.rb', line 10 def self.find(id) new(id.humanize(capitalize: false)) end |
Instance Method Details
#all? ⇒ Boolean
50 51 52 |
# File 'app/models/workarea/query_string.rb', line 50 def all? sanitized == '*' end |
#id ⇒ Object
18 19 20 |
# File 'app/models/workarea/query_string.rb', line 18 def id @id ||= Array(Lingua.stemmer(pieces)).join('_').downcase.gsub(/\W/, '') end |
#phrase? ⇒ Boolean
30 31 32 |
# File 'app/models/workarea/query_string.rb', line 30 def phrase? pieces.many? end |
#pieces ⇒ Object
22 23 24 |
# File 'app/models/workarea/query_string.rb', line 22 def pieces @pieces ||= sanitized.split(/\s/) end |
#pretty ⇒ Object
26 27 28 |
# File 'app/models/workarea/query_string.rb', line 26 def pretty pieces.map(&:downcase).join(' ') end |
#sanitized ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/models/workarea/query_string.rb', line 34 def sanitized @sanitized ||= begin query = @original .to_s .gsub(INVALID_SEARCH_REGEX, ' ') .strip .squeeze(' ') query.gsub!('"', '\"') if query.count('"') == 1 query.gsub!(/(\w)( AND| OR)$/) { "#{::Regexp.last_match(1)}#{::Regexp.last_match(2).downcase}" } query end end |
#short? ⇒ Boolean
54 55 56 |
# File 'app/models/workarea/query_string.rb', line 54 def short? id.length < 3 end |