Class: Picky::Query::Tokens

Inherits:
Object show all
Defined in:
lib/picky/query/tokens.rb,
lib/picky/query/or.rb

Overview

This class primarily handles switching through similar token constellations.

Direct Known Subclasses

Or

Defined Under Namespace

Classes: Or

Constant Summary collapse

@@or_splitting_pattern =

Creates a new Tokens object from a number of Strings.

/\|/
@@splitter =
Splitter.new @@or_splitting_pattern

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens, ignore_unassigned = false) ⇒ Tokens

Create a new Tokens object with the array of tokens passed in.



21
22
23
24
# File 'lib/picky/query/tokens.rb', line 21

def initialize tokens, ignore_unassigned = false
  @tokens            = tokens
  @ignore_unassigned = ignore_unassigned
end

Instance Attribute Details

#ignore_unassignedObject (readonly)

Returns the value of attribute ignore_unassigned.



11
12
13
# File 'lib/picky/query/tokens.rb', line 11

def ignore_unassigned
  @ignore_unassigned
end

#tokensObject (readonly)

Returns the value of attribute tokens.



11
12
13
# File 'lib/picky/query/tokens.rb', line 11

def tokens
  @tokens
end

Class Method Details

.processed(words, originals, ignore_unassigned = false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/picky/query/tokens.rb', line 30

def self.processed words, originals, ignore_unassigned = false
  new(words.zip(originals).collect! do |word, original|
    w, *middle, rest = @@splitter.multi word
    if rest
      Or.new processed [w, *middle, rest], original.split(@@or_splitting_pattern)
    else
      Token.processed w, original
    end
  end, ignore_unassigned)
end

Instance Method Details

#+(other) ⇒ Object

Non-destructive addition.



99
100
101
# File 'lib/picky/query/tokens.rb', line 99

def + other
  self.class.new (@tokens + other.tokens), (self.ignore_unassigned || other.ignore_unassigned)
end

#==(other) ⇒ Object



93
94
95
# File 'lib/picky/query/tokens.rb', line 93

def == other
  self.tokens == other.tokens
end

#originalObject



82
83
84
# File 'lib/picky/query/tokens.rb', line 82

def original
  originals
end

#originalsObject

TODO



79
80
81
# File 'lib/picky/query/tokens.rb', line 79

def originals
  @tokens.map(&:original)
end

#partialize_lastObject

Makes the last of the tokens partial.



73
74
75
# File 'lib/picky/query/tokens.rb', line 73

def partialize_last
  @tokens.last.partial = true unless empty?
end

#possible_combinations_in(categories) ⇒ Object

Generates an array in the form of [

[combination],                           # of token 1
[combination, combination, combination], # of token 2
[combination, combination]               # of token 3

]



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/picky/query/tokens.rb', line 48

def possible_combinations_in categories
  @tokens.inject([]) do |combinations, token|
    possible_combinations = token.possible_combinations categories
    
    # Note: Optimization for ignoring tokens that allocate to nothing and
    # can be ignored.
    # For example in a special search, where "florian" is not
    # mapped to any category.
    #
    if ignore_unassigned && possible_combinations.empty?
      combinations
    else
      combinations << possible_combinations
    end
  end
end

#symbolizeObject

Symbolizes each of the tokens.



67
68
69
# File 'lib/picky/query/tokens.rb', line 67

def symbolize
  @tokens.each &:symbolize!
end

#textsObject

TODO



87
88
89
# File 'lib/picky/query/tokens.rb', line 87

def texts
  @tokens.map(&:text)
end

#to_sObject

Just join the token original texts.



105
106
107
# File 'lib/picky/query/tokens.rb', line 105

def to_s
  originals.join ' '
end