Class: Workarea::QueryString

Inherits:
Object
  • Object
show all
Includes:
GlobalID::Identification
Defined in:
app/models/workarea/query_string.rb

Constant Summary collapse

INVALID_SEARCH_REGEX =
/[\]\[:\.;\(\)\{\}^\\\/!~]|<\/?script>|(\s+)?-(\s|$)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#originalObject (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

Returns:

  • (Boolean)


50
51
52
# File 'app/models/workarea/query_string.rb', line 50

def all?
  sanitized == '*'
end

#idObject



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

Returns:

  • (Boolean)


30
31
32
# File 'app/models/workarea/query_string.rb', line 30

def phrase?
  pieces.many?
end

#piecesObject



22
23
24
# File 'app/models/workarea/query_string.rb', line 22

def pieces
  @pieces ||= sanitized.split(/\s/)
end

#prettyObject



26
27
28
# File 'app/models/workarea/query_string.rb', line 26

def pretty
  pieces.map(&:downcase).join(' ')
end

#sanitizedObject



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

Returns:

  • (Boolean)


54
55
56
# File 'app/models/workarea/query_string.rb', line 54

def short?
  id.length < 3
end