Class: Picky::Categories

Inherits:
Object show all
Includes:
Helpers::Indexing
Defined in:
lib/picky/categories_convenience.rb,
lib/picky/categories.rb,
lib/picky/categories_indexed.rb,
lib/picky/categories_indexing.rb,
lib/picky/categories_realtime.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Indexing

#index, #timed_indexing

Methods included from Helpers::Measuring

#timed

Constructor Details

#initialize(options = {}) ⇒ Categories

A list of indexed categories.



25
26
27
# File 'lib/picky/categories.rb', line 25

def initialize options = {}
  clear_categories
end

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



5
6
7
# File 'lib/picky/categories.rb', line 5

def categories
  @categories
end

#category_hashObject (readonly)

Returns the value of attribute category_hash.



5
6
7
# File 'lib/picky/categories.rb', line 5

def category_hash
  @category_hash
end

Instance Method Details

#<<(category) ⇒ Object

Add the given category to the list of categories.



55
56
57
58
59
# File 'lib/picky/categories.rb', line 55

def << category
  reset_qualifier_mapper # TODO Have an add method on QualifierMapper?
  categories << category unless categories.include? category # TODO This is wrong, and needs to be handled in index.rb
  category_hash[category.name] = category
end

#[](category_name) ⇒ Object

Find a given category in the categories.



63
64
65
66
# File 'lib/picky/categories.rb', line 63

def [] category_name
  category_name = category_name.intern
  category_hash[category_name] || raise_not_found(category_name)
end

#clear_categoriesObject

Clears both the array of categories and the hash of categories.



31
32
33
34
# File 'lib/picky/categories.rb', line 31

def clear_categories
  @categories    = []
  @category_hash = Hash.new
end

#each_category(&block) ⇒ Object



9
10
11
# File 'lib/picky/categories_convenience.rb', line 9

def each_category &block
  categories.each &block
end

#inject_possible_for(tokens) ⇒ Object



27
28
29
30
31
32
# File 'lib/picky/categories_indexed.rb', line 27

def inject_possible_for tokens
  tokens.inject([]) do |result, token|
    possible = possible_categories token
    result + possible_for(token, possible)
  end
end

#possible_categories(token) ⇒ Object

This returns the possible categories for this token. If the user has already preselected a category for this token, like “artist:moby”, if not just return all for the given token, since all are possible.

Note: Once I thought this was called too often. But it is not (18.01.2011).



53
54
55
# File 'lib/picky/categories_indexed.rb', line 53

def possible_categories token
  token.predefined_categories(qualifier_mapper) || categories
end

#possible_for(token, preselected_categories = nil) ⇒ Object

Returns possible Combinations for the token.

Note: The preselected_categories param is an optimization. Note: Returns [] if no categories matched (will produce no result).



39
40
41
42
43
44
# File 'lib/picky/categories_indexed.rb', line 39

def possible_for token, preselected_categories = nil
  (preselected_categories || possible_categories(token)).inject([]) do |combinations, category|
    combination = token.combination_for category
    combination ? combinations << combination : combinations
  end
end

#qualifier_mapperObject

Updates the qualifier (“qualifier:searchterm”) mapping.

Example:

You dynamically add a new category to an index.
To add the qualifiers to a search, you call this
method.


43
44
45
# File 'lib/picky/categories.rb', line 43

def qualifier_mapper
  @qualifier_mapper ||= QualifierMapper.new self
end

#raise_not_found(category_name) ⇒ Object



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

def raise_not_found category_name
  raise %Q{Index category "#{category_name}" not found. Possible categories: "#{categories.map(&:name).join('", "')}".}
end

#reset_qualifier_mapperObject

Resets the qualifier mapper used.



49
50
51
# File 'lib/picky/categories.rb', line 49

def reset_qualifier_mapper
  @qualifier_mapper = nil
end

#similar_possible_for(token) ⇒ Object

Gets all similar tokens and puts together the possible combinations for each found similar token.



12
13
14
15
# File 'lib/picky/categories_indexed.rb', line 12

def similar_possible_for token
  tokens = similar_tokens_for token
  inject_possible_for tokens
end

#similar_tokens_for(token) ⇒ Object

Returns all possible similar tokens for the given token.



19
20
21
22
23
# File 'lib/picky/categories_indexed.rb', line 19

def similar_tokens_for token
  categories.inject([]) do |result, category|
    result + token.similar_tokens_for(category)
  end
end

#to_sObject



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

def to_s
  "#{self.class}(#{categories.join(', ')})"
end

#to_statsObject



71
72
73
# File 'lib/picky/categories.rb', line 71

def to_stats
  map(&:name).join(', ')
end

#to_tree_s(indent = 0) ⇒ Object



75
76
77
# File 'lib/picky/categories.rb', line 75

def to_tree_s indent = 0
  ([' ' * indent] * categories.size).zip(categories.map(&:to_tree_s)).map(&:join).join "\n"
end

#update(object, where = :unshift) ⇒ Object



13
14
15
# File 'lib/picky/categories_realtime.rb', line 13

def update object, where = :unshift
  replace object, where = :unshift
end