Class: SoftLayer::ObjectFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/softlayer/ObjectFilter.rb

Overview

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.

Defined Under Namespace

Classes: CriteriaAcceptor

Instance Method Summary collapse

Constructor Details

#initialize(&construction_block) ⇒ ObjectFilter

Returns a new instance of ObjectFilter.



31
32
33
34
35
# File 'lib/softlayer/ObjectFilter.rb', line 31

def initialize(&construction_block)
  @filter_hash = {}
  self.modify(&construction_block)
  self
end

Instance Method Details

#accept(key_path) ⇒ Object



45
46
47
# File 'lib/softlayer/ObjectFilter.rb', line 45

def accept(key_path)
  CriteriaAcceptor.new(self, key_path)
end

#criteria_for_key_path(key_path) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/softlayer/ObjectFilter.rb', line 37

def empty?
  @filter_hash.empty?
end

#modify(&construction_block) ⇒ Object



41
42
43
# 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) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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_hObject



49
50
51
# File 'lib/softlayer/ObjectFilter.rb', line 49

def to_h
  return @filter_hash.dup
end