Module: Spinel::Helper

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

Instance Method Summary collapse

Instance Method Details

#document_id(doc) ⇒ Object



20
21
22
# File 'lib/spinel/helper.rb', line 20

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

#document_index_fields(doc) ⇒ Object



28
29
30
31
32
# File 'lib/spinel/helper.rb', line 28

def document_index_fields doc
  Spinel.index_fields.map {|field|
    doc[field.to_s.to_sym] || doc[field.to_s] || doc[field]
  }.compact.join(' ')
end

#document_score(doc) ⇒ Object



24
25
26
# File 'lib/spinel/helper.rb', line 24

def document_score doc
  (doc[:score] || doc["score"]).to_f
end

#get_valid_document(doc) ⇒ Object

Raises:

  • (ArgumentError)


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

def get_valid_document doc
  id = document_id doc
  raise ArgumentError, "documents must specify id" unless id
  [id, document_index_fields(doc), document_score(doc)]
end

#prefixes(str) ⇒ Object



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

def prefixes str
  squish(str).split.flat_map do |w|
    (Spinel.minimal_word-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