Class: Namespace::Filter

Inherits:
Queries::Query show all
Defined in:
lib/queries/namespace/filter.rb

Instance Attribute Summary collapse

Attributes inherited from Queries::Query

#dynamic_limit, #options, #project_id, #query_string, #terms

Instance Method Summary collapse

Methods inherited from Queries::Query

#alphabetic_strings, #attribute_exact_facet, #autocomplete, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_common_name_exact, #autocomplete_common_name_like, #autocomplete_exact_id, #autocomplete_exactly_named, #autocomplete_named, #autocomplete_ordered_wildcard_pieces_in_cached, #build_terms, #cached, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #end_wildcard, #exactly_named, #fragments, #integers, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #no_terms?, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #year_letter, #years

Constructor Details

#initialize(params) ⇒ Filter

Returns a new instance of Filter.



21
22
23
24
25
26
# File 'lib/queries/namespace/filter.rb', line 21

def initialize(params)
  @institution = params[:institution]
  @name = params[:name]
  @short_name = params[:short_name]
  @verbatim_short_name = params[:verbatim_short_name]
end

Instance Attribute Details

#institutionObject

Parameters:

  • institution (String)

    wildcarded to match institution



7
8
9
# File 'lib/queries/namespace/filter.rb', line 7

def institution
  @institution
end

#nameArray

Parameters:

  • name (Array, String)

Returns:

  • (Array)


11
12
13
# File 'lib/queries/namespace/filter.rb', line 11

def name
  @name
end

#short_nameArray

Parameters:

  • short_name (Array, String)

Returns:

  • (Array)


15
16
17
# File 'lib/queries/namespace/filter.rb', line 15

def short_name
  @short_name
end

#verbatim_short_nameArray

Parameters:

  • verbatim_short_name (Array, String)

Returns:

  • (Array)


19
20
21
# File 'lib/queries/namespace/filter.rb', line 19

def verbatim_short_name
  @verbatim_short_name
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/queries/namespace/filter.rb', line 96

def all
  a = and_clauses
  b = merge_clauses
  if a && b
    b.where(a).distinct
  elsif a
    ::Namespace.where(a).distinct
  elsif b
    b.distinct
  else
    ::Namespace.all
  end
end

#and_clausesActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/queries/namespace/filter.rb', line 65

def and_clauses
  clauses = [
    matching_name,
    matching_short_name,
    matching_verbatim_name, 

    matching_institution,
  ].compact

  return nil if clauses.empty?

  a = clauses.shift
  clauses.each do |b|
    a = a.and(b)
  end
  a
end

#matching_institutionObject



59
60
61
62
# File 'lib/queries/namespace/filter.rb', line 59

def matching_institution
  return nil if institution.nil?
  table[:institution].matches('%' + insititution + '%')
end

#matching_nameObject



44
45
46
47
# File 'lib/queries/namespace/filter.rb', line 44

def matching_name
  return nil if name.empty?
  table[:name].eq_any(name)
end

#matching_short_nameObject



49
50
51
52
# File 'lib/queries/namespace/filter.rb', line 49

def matching_short_name
  return nil if short_name.empty?
  table[:short_name].eq_any(short_name)
end

#matching_verbatim_nameObject



54
55
56
57
# File 'lib/queries/namespace/filter.rb', line 54

def matching_verbatim_name
  return nil if verbatim_short_name.empty?
  table[:verbatim_short_name].eq_any(verbatim_short_name)
end

#merge_clausesObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/queries/namespace/filter.rb', line 83

def merge_clauses
  clauses = [ ].compact

  return nil if clauses.empty?

  a = clauses.shift
  clauses.each do |b|
    a = a.merge(b)
  end
  a
end

#tableObject



40
41
42
# File 'lib/queries/namespace/filter.rb', line 40

def table
  ::Namespace.arel_table
end