Class: Ansr::Dpla::Relation

Inherits:
Relation
  • Object
show all
Defined in:
lib/ansr_dpla/relation.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, table, values = {}) ⇒ Relation

Returns a new instance of Relation.



5
6
7
8
# File 'lib/ansr_dpla/relation.rb', line 5

def initialize(klass, table, values = {})
  raise "Cannot search nil model" if klass.nil?
  super(klass, table, values)
end

Instance Method Details

#countObject



32
33
34
35
# File 'lib/ansr_dpla/relation.rb', line 32

def count
  self.load
  @response['count']
end

#docs_from(response) ⇒ Object



53
54
55
# File 'lib/ansr_dpla/relation.rb', line 53

def docs_from(response)
  response['docs']
end

#empty?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/ansr_dpla/relation.rb', line 15

def empty?
  count == 0
end

#facet_values=(values) ⇒ Object



10
11
12
13
# File 'lib/ansr_dpla/relation.rb', line 10

def facet_values=(values)
  values.each {|value| raise "#{value.expr.name.to_sym} is not facetable" unless table.facets.include? value.expr.name.to_sym}
  super
end

#filters_from(response) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ansr_dpla/relation.rb', line 37

def filters_from(response)
  f = {}
  (response['facets'] || {}).inject(f) do |h,(k,v)|

    if v['total'] != 0
      items = v['terms'].collect do |term|
        Ansr::Facets::FacetItem.new(:value => term['term'], :hits => term['count'])
      end
      options = {:sort => 'asc', :offset => 0}
      h[k] = Ansr::Facets::FacetField.new k, items, options
    end
    h
  end
  f
end

#many?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/ansr_dpla/relation.rb', line 19

def many?
  count > 1
end

#offset!(value) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/ansr_dpla/relation.rb', line 23

def offset!(value)
  page_size = self.limit_value || default_limit_value
  if (value.to_i % page_size.to_i) != 0
    raise "Bad offset #{value} for page size #{page_size}"
  end
  self.offset_value=value
  self
end