Module: Spinel::Helper

Included in:
Client
Defined in:
lib/spinel/helper.rb

Instance Method Summary collapse

Instance Method Details

#document_body(doc) ⇒ Object


26
27
28
# File 'lib/spinel/helper.rb', line 26

def document_body doc
  doc[Spinel.document_key.to_sym] || doc[Spinel.document_key]
end

#document_id(doc) ⇒ Object


18
19
20
# File 'lib/spinel/helper.rb', line 18

def document_id doc
  doc[:id] || doc["id"]
end

#document_score(doc) ⇒ Object


22
23
24
# File 'lib/spinel/helper.rb', line 22

def document_score doc
  doc[:score] || doc["score"]
end

#document_validate(doc) ⇒ Object

Raises:

  • (ArgumentError)

14
15
16
# File 'lib/spinel/helper.rb', line 14

def document_validate doc
  raise ArgumentError, "documents must specify both an id and a body" unless document_id(doc) && document_body(doc)
end

#prefixes_for_phrase(phrase) ⇒ Object


4
5
6
7
8
# File 'lib/spinel/helper.rb', line 4

def prefixes_for_phrase(phrase)
  squish(phrase).split.flat_map do |w|
    (Spinel.min_complete-1..(w.length-1)).map{ |l| w[0..l] }
  end.uniq
end

#squish(str) ⇒ Object


10
11
12
# File 'lib/spinel/helper.rb', line 10

def squish str
  str.to_s.gsub(/[[:space:]]+/, ' ').strip
end