Class: Queries::Observation::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/queries/observation/filter.rb

Overview

!! does not inherit from base query

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Filter

Returns a new instance of Filter.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/queries/observation/filter.rb', line 18

def initialize(params)
  @otu_id = params[:otu_id]
  @collection_object_id = params[:collection_object_id]
  @observation_object_global_id = params[:observation_object_global_id]
  @descriptor_id = params[:descriptor_id]
  @type = params[:type]

  @character_state_id = params[:character_state_id]

  @observation_matrix_id = params[:observation_matrix_id]
end

Instance Attribute Details

#character_state_idObject

Returns the value of attribute character_state_id.



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

def character_state_id
  @character_state_id
end

#collection_object_idObject

Returns the value of attribute collection_object_id.



9
10
11
# File 'lib/queries/observation/filter.rb', line 9

def collection_object_id
  @collection_object_id
end

#descriptor_idObject

Returns the value of attribute descriptor_id.



10
11
12
# File 'lib/queries/observation/filter.rb', line 10

def descriptor_id
  @descriptor_id
end

#observation_matrix_idInteger?

Returns not extended to Array yet.

Returns:

  • (Integer, nil)

    not extended to Array yet



16
17
18
# File 'lib/queries/observation/filter.rb', line 16

def observation_matrix_id
  @observation_matrix_id
end

#observation_object_global_idObject

Returns the value of attribute observation_object_global_id.



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

def observation_object_global_id
  @observation_object_global_id
end

#otu_idObject

Returns the value of attribute otu_id.



8
9
10
# File 'lib/queries/observation/filter.rb', line 8

def otu_id
  @otu_id
end

#typeObject

Returns the value of attribute type.



12
13
14
# File 'lib/queries/observation/filter.rb', line 12

def type
  @type
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/queries/observation/filter.rb', line 123

def all
  a = and_clauses
  b = merge_clauses

  q = nil
  if a && b
    q = b.where(a)
  elsif a
    q = ::Observation.where(a)
  elsif b
    q = b
  else
    q = ::Observation.all
  end

  q
end

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/queries/observation/filter.rb', line 51

def and_clauses
  clauses = [
    matching_descriptor_id,
    matching_otu_id,
    matching_collection_object_id,
    matching_observation_object_global_id,
    matching_character_state_id
  ].compact

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

#matching_character_state_idArel::Node?

Returns:

  • (Arel::Node, nil)


88
89
90
# File 'lib/queries/observation/filter.rb', line 88

def matching_character_state_id
  character_state_id.blank? ? nil : table[:character_state_id].eq(character_state_id) 
end

#matching_collection_object_idArel::Node?

Returns:

  • (Arel::Node, nil)


98
99
100
# File 'lib/queries/observation/filter.rb', line 98

def matching_collection_object_id
  collection_object_id.blank? ? nil : table[:collection_object_id].eq(collection_object_id) 
end

#matching_descriptor_idArel::Node?

Returns:

  • (Arel::Node, nil)


108
109
110
# File 'lib/queries/observation/filter.rb', line 108

def matching_descriptor_id
  descriptor_id.blank? ? nil : table[:descriptor_id].eq(descriptor_id) 
end

#matching_observation_object_global_idArel::Node?

Returns:

  • (Arel::Node, nil)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/queries/observation/filter.rb', line 68

def matching_observation_object_global_id
  if  observation_object_global_id.blank?
    nil
  else
  
    # TODO - make a hash method to parameterize these values 
    o = GlobalID::Locator.locate(observation_object_global_id) 

    case o.metamorphosize.class.name
    when 'Otu'
      table[:otu_id].eq(o.id) 
    when 'CollectionObject'
      table[:collection_object_id].eq(o.id) 
    else
      return nil
    end
  end
end

#matching_otu_idArel::Node?

Returns:

  • (Arel::Node, nil)


93
94
95
# File 'lib/queries/observation/filter.rb', line 93

def matching_otu_id
  otu_id.blank? ? nil : table[:otu_id].eq(otu_id) 
end

#matching_typeArel::Node?

Returns:

  • (Arel::Node, nil)


103
104
105
# File 'lib/queries/observation/filter.rb', line 103

def matching_type
  type.blank? ? nil : table[:type].eq(type) 
end

#merge_clausesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/queries/observation/filter.rb', line 35

def merge_clauses
  clauses = [
    observation_matrix_id_facet
  ].compact

  return nil if clauses.empty?

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

#observation_matrix_id_facetObject



30
31
32
33
# File 'lib/queries/observation/filter.rb', line 30

def observation_matrix_id_facet
  return nil if observation_matrix_id.nil?
  ::Observation.in_observation_matrix(observation_matrix_id)
end

#tableArel::Table

Returns:

  • (Arel::Table)


143
144
145
# File 'lib/queries/observation/filter.rb', line 143

def table
  ::Observation.arel_table
end