class SoftLayer::ObjectFilter

An ObjectFilter is a tool that, when passed to the SoftLayer API allows the API server to filter, or limit the result set for a call.

Constructing ObjectFilters is an art that is currently somewhat arcane. This class tries to simplify filtering for the fundamental cases, while still allowing for more complex ObjectFilters to be created.

To construct an object filter you begin with an instance of the class. At construction time, or in a “modify” call you can change the filter criteria using a fancy DSL syntax.

For example, to filter virtual servers so that you only get ones whose domains end with “layer.com” you might use:

object_filter = ObjectFilter.new do |filter|
  filter.accept(virtualGuests.domain).when_it ends_with("layer.com")
end

The set of criteria that can be included after “when_it” are defined by routines in the ObjectFilterDefinitionContext module.

Public Class Methods

new(&construction_block) click to toggle source
# File lib/softlayer/ObjectFilter.rb, line 31
def initialize(&construction_block)
  @filter_hash = {}
  self.modify(&construction_block)
  self
end

Public Instance Methods

accept(key_path) click to toggle source
# File lib/softlayer/ObjectFilter.rb, line 45
def accept(key_path)
  CriteriaAcceptor.new(self, key_path)
end
criteria_for_key_path(key_path) click to toggle source
# File lib/softlayer/ObjectFilter.rb, line 53
def criteria_for_key_path(key_path)
  raise "The key path cannot be empty when searching for criteria" if key_path.nil? || key_path.empty?

  current_level = @filter_hash
  keys = key_path.split('.')

  while current_level && keys.count > 1
    current_level = current_level[keys.shift]
  end

  if current_level
    current_level[keys[0]]
  else
    nil
  end
end
empty?() click to toggle source
# File lib/softlayer/ObjectFilter.rb, line 37
def empty?
  @filter_hash.empty?
end
modify(&construction_block) click to toggle source
# File lib/softlayer/ObjectFilter.rb, line 41
def modify(&construction_block)
  ObjectFilterDefinitionContext.module_exec(self, &construction_block) if construction_block
end
set_criteria_for_key_path(key_path, criteria) click to toggle source
# File lib/softlayer/ObjectFilter.rb, line 70
def set_criteria_for_key_path(key_path, criteria)
  current_level = @filter_hash
  keys = key_path.split('.')

  current_key = keys.shift
  while current_level && !keys.empty?
    if !current_level.has_key? current_key
      current_level[current_key] = {}
    end
    current_level = current_level[current_key]
    current_key = keys.shift
  end

  current_level[current_key] = criteria
end
to_h() click to toggle source
# File lib/softlayer/ObjectFilter.rb, line 49
def to_h
  return @filter_hash.dup
end