Class: CardDB::FilterBuilder

Inherits:
Object
  • Object
show all
Includes:
FilterOperators
Defined in:
lib/carddb/filter_builder.rb,
lib/carddb/filter_builder.rb

Overview

Make operators available in FilterBuilder instances

Defined Under Namespace

Classes: Operator

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FilterOperators

#contains, #eq, #gt, #gte, #ilike, #is_not_null, #is_null, #like, #lt, #lte, #neq, #not_within, #within

Constructor Details

#initializeFilterBuilder

Returns a new instance of FilterBuilder.



45
46
47
48
# File 'lib/carddb/filter_builder.rb', line 45

def initialize
  @conditions = []
  @or_groups = []
end

Class Method Details

.build { ... } ⇒ Hash

Build a filter using the DSL

Yields:

  • Block to define filters

Returns:

  • (Hash)

    The filter hash



39
40
41
42
43
# File 'lib/carddb/filter_builder.rb', line 39

def self.build(&block)
  builder = new
  builder.instance_eval(&block)
  builder.to_filter
end

Instance Method Details

#any { ... } ⇒ self

Add an OR group

Yields:

  • Block to define OR conditions

Returns:

  • (self)


65
66
67
68
69
70
71
# File 'lib/carddb/filter_builder.rb', line 65

def any(&block)
  or_builder = FilterBuilder.new
  or_builder.instance_eval(&block)
  or_conditions = or_builder.conditions_array
  @or_groups << { '$or' => or_conditions } if or_conditions.any?
  self
end

#conditions_arrayArray<Hash>

Get conditions as array (for OR groups)

Returns:

  • (Array<Hash>)


115
116
117
# File 'lib/carddb/filter_builder.rb', line 115

def conditions_array
  @conditions
end

#to_filterHash

Convert to filter hash

Returns:

  • (Hash)


104
105
106
107
108
109
110
# File 'lib/carddb/filter_builder.rb', line 104

def to_filter
  all_conditions = @conditions + @or_groups
  return {} if all_conditions.empty?
  return all_conditions.first if all_conditions.size == 1

  { '$and' => all_conditions }
end

#where(conditions) ⇒ self

Add a condition to the filter

Parameters:

  • conditions (Hash)

    Field conditions

Returns:

  • (self)


54
55
56
57
58
59
# File 'lib/carddb/filter_builder.rb', line 54

def where(conditions)
  conditions.each do |field, value|
    @conditions << build_condition(field, value)
  end
  self
end

#where_any(field, conditions) ⇒ self

Add an array element filter (for ARRAY of HASH fields)

Parameters:

  • field (Symbol, String)

    The array field name

  • conditions (Hash)

    Conditions that any element must match

Returns:

  • (self)


92
93
94
95
96
97
98
99
# File 'lib/carddb/filter_builder.rb', line 92

def where_any(field, conditions)
  any_filter = {}
  conditions.each do |key, value|
    any_filter[key.to_s] = value.is_a?(Operator) ? value.to_filter : value
  end
  @conditions << { "#{field}._any" => any_filter }
  self
end

Add a link filter condition

Parameters:

  • field (Symbol, String)

    The link field name

  • conditions (Hash)

    Conditions on the linked record

Returns:

  • (self)


78
79
80
81
82
83
84
85
# File 'lib/carddb/filter_builder.rb', line 78

def where_link(field, conditions)
  link_filter = {}
  conditions.each do |key, value|
    link_filter[key.to_s] = value.is_a?(Operator) ? value.to_filter : value
  end
  @conditions << { "#{field}._link" => link_filter }
  self
end