Class: Xapit::Client::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/xapit/client/collection.rb

Constant Summary collapse

DEFAULT_PER_PAGE =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clauses = []) ⇒ Collection

Returns a new instance of Collection.



8
9
10
# File 'lib/xapit/client/collection.rb', line 8

def initialize(clauses = [])
  @clauses = clauses
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



141
142
143
144
145
146
147
# File 'lib/xapit/client/collection.rb', line 141

def method_missing(method, *args, &block)
  if Array.method_defined?(method)
    records.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#clausesObject (readonly)

Returns the value of attribute clauses.



6
7
8
# File 'lib/xapit/client/collection.rb', line 6

def clauses
  @clauses
end

Instance Method Details

#applied_facet_identifiersObject



91
92
93
# File 'lib/xapit/client/collection.rb', line 91

def applied_facet_identifiers
  query[:applied_facet_options].map { |option| option[:id] }
end

#applied_facet_optionsObject



85
86
87
88
89
# File 'lib/xapit/client/collection.rb', line 85

def applied_facet_options
  query[:applied_facet_options].map do |option|
    FacetOption.new(option[:name], {:value => option[:value]}, applied_facet_identifiers)
  end
end

#current_pageObject



72
73
74
# File 'lib/xapit/client/collection.rb', line 72

def current_page
  (clause_value(:page) || 1).to_i
end

#facetsObject



95
96
97
# File 'lib/xapit/client/collection.rb', line 95

def facets
  @facets ||= fetch_facets.select { |f| f.options.size > 0 }
end

#in_classes(*classes) ⇒ Object



12
13
14
# File 'lib/xapit/client/collection.rb', line 12

def in_classes(*classes)
  scope(:in_classes, classes)
end

#include_facets(*facets) ⇒ Object



60
61
62
# File 'lib/xapit/client/collection.rb', line 60

def include_facets(*facets)
  facets.empty? ? self : scope(:include_facets, facets)
end

#limit_valueObject



76
77
78
# File 'lib/xapit/client/collection.rb', line 76

def limit_value
  (clause_value(:per_page) || DEFAULT_PER_PAGE).to_i
end

#not_in_classes(*classes) ⇒ Object



16
17
18
# File 'lib/xapit/client/collection.rb', line 16

def not_in_classes(*classes)
  scope(:not_in_classes, classes)
end

#not_where(conditions) ⇒ Object



32
33
34
# File 'lib/xapit/client/collection.rb', line 32

def not_where(conditions)
  scope(:not_where, where_conditions(conditions))
end

#num_pagesObject Also known as: total_pages



80
81
82
# File 'lib/xapit/client/collection.rb', line 80

def num_pages
  (total_entries.to_f / limit_value).ceil
end

#or_where(conditions) ⇒ Object



36
37
38
# File 'lib/xapit/client/collection.rb', line 36

def or_where(conditions)
  scope(:or_where, where_conditions(conditions))
end

#order(column, direction = :asc) ⇒ Object



40
41
42
# File 'lib/xapit/client/collection.rb', line 40

def order(column, direction = :asc)
  scope(:order, [column, direction])
end

#page(page_num) ⇒ Object



44
45
46
# File 'lib/xapit/client/collection.rb', line 44

def page(page_num)
  scope(:page, page_num)
end

#per(per_page) ⇒ Object



48
49
50
# File 'lib/xapit/client/collection.rb', line 48

def per(per_page)
  scope(:per_page, per_page)
end

#recordsObject



64
65
66
# File 'lib/xapit/client/collection.rb', line 64

def records
  @records ||= query[:records].map { |record| Kernel.const_get(record[:class]).find(record[:id]) }
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/xapit/client/collection.rb', line 103

def respond_to?(method, include_private = false)
  Array.method_defined?(method) || super
end

#search(phrase = nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/xapit/client/collection.rb', line 20

def search(phrase = nil)
  if phrase && !phrase.empty?
    scope(:search, phrase)
  else
    self
  end
end

#similar_to(member) ⇒ Object



52
53
54
# File 'lib/xapit/client/collection.rb', line 52

def similar_to(member)
  scope(:similar_to, member.class.xapit_index_builder.document_data(member))
end

#spelling_suggestionObject



99
100
101
# File 'lib/xapit/client/collection.rb', line 99

def spelling_suggestion
  @spelling_suggestion ||= Xapit.database.spelling_suggestion(@clauses)
end

#total_entriesObject



68
69
70
# File 'lib/xapit/client/collection.rb', line 68

def total_entries
  query[:total].to_i
end

#where(conditions) ⇒ Object



28
29
30
# File 'lib/xapit/client/collection.rb', line 28

def where(conditions)
  scope(:where, where_conditions(conditions))
end

#with_facets(facets) ⇒ Object



56
57
58
# File 'lib/xapit/client/collection.rb', line 56

def with_facets(facets)
  facets.to_s.length.zero? ? self : scope(:with_facets, facets.split("-"))
end