Module: SoftLayer::ObjectFilterDefinitionContext

Defined in:
lib/softlayer/ObjectFilter.rb

Overview

The ObjectFilterDefinitionContext defines a bunch of methods that allow the property conditions of an object filter to be defined in a “pretty” way. Each method returns a block (a lambda, a proc) that, when called and passed the tail property of a property chain will generate a fragment of an object filter asking that that property match the given conditions.

This class, as a whole, is largely an implementation detail of object filter definitions and there is probably not a good reason to call into it directly.

Class Method Summary collapse

Class Method Details

.begins_with(value) ⇒ Object

Matches when the value is found at the beginning of the field. This search is not case sensitive



148
149
150
# File 'lib/softlayer/ObjectFilter.rb', line 148

def self.begins_with(value)
  filter_criteria('^=', value)
end

.contains(value) ⇒ Object

Matches when the value is found within the field the search is not case sensitive



142
143
144
# File 'lib/softlayer/ObjectFilter.rb', line 142

def self.contains(value)
  filter_criteria('*=', value)
end

.contains_exactly(value) ⇒ Object

Matches when the value is found within the field the search is case sensitive



233
234
235
# File 'lib/softlayer/ObjectFilter.rb', line 233

def self.contains_exactly(value)
  filter_criteria('~', value)
end

.does_not_contain(value) ⇒ Object

Matches when the value is not found within the field the search is case sensitive



239
240
241
# File 'lib/softlayer/ObjectFilter.rb', line 239

def self.does_not_contain(value)
  filter_criteria('!~', value)
end

.ends_with(value) ⇒ Object

Matches when the value is found at the end of the field. This search is not case sensitive



154
155
156
# File 'lib/softlayer/ObjectFilter.rb', line 154

def self.ends_with(value)
  filter_criteria('$=', value)
end

.is(value) ⇒ Object

Matches when the value in the field is exactly equal to the given value. This is a case-sensitive match If value is Enumerable, it is equivalent to calling is_contained_by



129
130
131
# File 'lib/softlayer/ObjectFilter.rb', line 129

def self.is(value)
  value.kind_of?(Enumerable) ? is_contained_by(value) : { 'operation' => value }
end

.is_between_dates(start_date, end_date) ⇒ Object

Matches when the key path value is a date between the start and end dates provided Dates should be strings in ‘%m/%d/%Y %T’ format or Date/DateTime instances



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/softlayer/ObjectFilter.rb', line 165

def self.is_between_dates(start_date, end_date)
  {
    'operation' => 'betweenDate',
    'options'   => [
                    {
                      'name'  => 'startDate',
                      'value' => [ start_date.kind_of?(Date) ? start_date.strftime('%m/%d/%Y %T') : DateTime.strptime(start_date.to_s, '%m/%d/%Y %T').strftime('%m/%d/%Y %T') ]
                    },
                    {
                      'name'  => 'endDate',
                      'value' => [ end_date.kind_of?(Date) ? end_date.strftime('%m/%d/%Y %T') : DateTime.strptime(end_date.to_s, '%m/%d/%Y %T').strftime('%m/%d/%Y %T') ]
                    }
                   ]
  }
end

.is_contained_by(value) ⇒ Object

Matches when key path value is equal to one of the given values in the Enumerable



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/softlayer/ObjectFilter.rb', line 182

def self.is_contained_by(value)
  raise "Expected an Enumerable value with a list of acceptable values that can be converted to strings" unless value.kind_of?(Enumerable)

  {
    'operation' => 'in',
    'options'   => [
                    {
                      'name'  => 'data',
                      'value' => value.collect { |enum_val| enum_val.to_s }
                    }
                   ]
  }
end

.is_greater_or_equal_to(value) ⇒ Object

Matches when the value in the field is greater than or equal to the given value



222
223
224
# File 'lib/softlayer/ObjectFilter.rb', line 222

def self.is_greater_or_equal_to(value)
  filter_criteria('>=', value)
end

.is_greater_than(value) ⇒ Object

Matches when the value in the field is greater than the given value



212
213
214
# File 'lib/softlayer/ObjectFilter.rb', line 212

def self.is_greater_than(value)
  filter_criteria('>', value)
end

.is_less_or_equal_to(value) ⇒ Object

Matches when the value in the field is less than or equal to the given value



227
228
229
# File 'lib/softlayer/ObjectFilter.rb', line 227

def self.is_less_or_equal_to(value)
  filter_criteria('<=', value)
end

.is_less_than(value) ⇒ Object

Matches when the value in the field is less than the given value



217
218
219
# File 'lib/softlayer/ObjectFilter.rb', line 217

def self.is_less_than(value)
  filter_criteria('<', value)
end

.is_not(value) ⇒ Object

Matches is the value in the field does not exactly equal the value passed in. If value is Enumerable, it is equivalent to calling is_not_contained_by



136
137
138
# File 'lib/softlayer/ObjectFilter.rb', line 136

def self.is_not(value)
  value.kind_of?(Enumerable) ? is_not_contained_by(value) : filter_criteria('!=', value)
end

.is_not_contained_by(value) ⇒ Object

Matches when key path value is not equal to one of the given values in the Enumerable



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/softlayer/ObjectFilter.rb', line 197

def self.is_not_contained_by(value)
  raise "Expected an Enumerable value with a list of acceptable values that can be converted to strings" unless value.kind_of?(Enumerable)

  {
    'operation' => 'not in',
    'options'   => [
                    {
                      'name'  => 'data',
                      'value' => value.collect { |enum_val| enum_val.to_s }
                    }
                   ]
  }
end

.is_not_nullObject

Matches when the property’s value is not null



249
250
251
# File 'lib/softlayer/ObjectFilter.rb', line 249

def self.is_not_null()
  { 'operation' => 'not null' }
end

.is_nullObject

Matches when the property’s value is null



244
245
246
# File 'lib/softlayer/ObjectFilter.rb', line 244

def self.is_null
  { 'operation' => 'is null' }
end

.matches_ignoring_case(value) ⇒ Object

Matches the given value in a case-insensitive way



159
160
161
# File 'lib/softlayer/ObjectFilter.rb', line 159

def self.matches_ignoring_case(value)
  filter_criteria('_=', value)
end

.matches_query(query_string) ⇒ Object

Accepts a query string defined by a simple query language. It translates strings in that language into criteria blocks

Object Filter comparisons can be done using operators. The set of accepted operators is found in the OBJECT_FILTER_OPERATORS array. The query string can consist of an operator followed by a space, followed by operand e.g.

"*= smaug"

The query language also accepts some aliases using asterisks in a regular-expression-like way. Those aliases look like:

'value'   Exact value match (translates to '_= value')
'value*'  Begins with value (translates to '^= value')
'*value'  Ends with value (translates to '$= value')
'*value*' Contains value (translates to '*= value')

This method corresponds to the query_filter method in the SoftLayer-Python API.



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/softlayer/ObjectFilter.rb', line 280

def self.matches_query(query_string)
  query = query_string.to_s.strip

  operator = OBJECT_FILTER_OPERATORS.find do | operator_string |
    query[0 ... operator_string.length] == operator_string
  end

  if operator then
    filter_criteria(operator, query[operator.length..-1])
  else
    case query
    when /\A\*(.*)\*\Z/
      contains($1)
    when /\A\*(.*)/
      ends_with($1)
    when /\A(.*)\*\Z/
      begins_with($1)
    else
      matches_ignoring_case(query)
    end #case
  end #if
end

.satisfies_the_raw_condition(condition_hash) ⇒ Object

This is a catch-all criteria matcher that allows for raw object filter conditions not covered by the more convenient methods above. The name is intentionally, annoyingly long and you should use this routine with solid knowledge and great care.



256
257
258
# File 'lib/softlayer/ObjectFilter.rb', line 256

def self.satisfies_the_raw_condition(condition_hash)
  condition_hash
end