Class: JSS::Criteriable::Criteria

Inherits:
Object
  • Object
show all
Defined in:
lib/jss/api_object/criteriable/criteria.rb,
lib/jss.rb

Overview

This class stores an array of Criterion instances and provides methods for working with them as a group.

APIObject subclasses that include JSS::Criteriable have a :criteria attribute which holds one Criteria object.

Objects that contain Criteria objects need to

  • call '#container = self' on their Criteria object when its created.

  • implement #should_update, which the Criteria object calls when it changes.

Both of those tasks are handled by the JSS::Criteriable module and are mixed in when it's included.

See JSS::Criteriable for examples

Constant Summary collapse

CRITERION_ATTRIBUTES =

Criterion instances we maintain need these attributes.s

[:priority, :and_or, :name, :search_type, :value].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_criteria = []) ⇒ Criteria

Returns a new instance of Criteria.

Parameters:



80
81
82
83
84
85
# File 'lib/jss/api_object/criteriable/criteria.rb', line 80

def initialize(new_criteria = [])
  @criteria = []

  # validates the param and fills @criteria
  self.criteria = new_criteria
end

Instance Attribute Details

#container=(value) ⇒ JSS::APIObject subclass (writeonly)

Returns a reference to the object containing these Criteria.

Returns:

  • (JSS::APIObject subclass)

    a reference to the object containing these Criteria



75
76
77
# File 'lib/jss/api_object/criteriable/criteria.rb', line 75

def container=(value)
  @container = value
end

#criteriaArray

Returns the group of JSS::Criteriable::Criterion instances making up these Criteria.

Returns:

  • (Array)

    the group of JSS::Criteriable::Criterion instances making up these Criteria



72
73
74
# File 'lib/jss/api_object/criteriable/criteria.rb', line 72

def criteria
  @criteria
end

Instance Method Details

#append_criterion(criterion) ⇒ void

This method returns an undefined value.

Add a new criterion to the end of the criteria

Parameters:



119
120
121
122
123
124
# File 'lib/jss/api_object/criteriable/criteria.rb', line 119

def append_criterion(criterion)
  criterion_ok? criterion
  criterion.priority = @criteria.length
  @criteria << criterion
  @container.should_update if @container
end

#clearvoid

This method returns an undefined value.

Remove all criterion objects



107
108
109
110
# File 'lib/jss/api_object/criteriable/criteria.rb', line 107

def clear
  @criteria = []
  @container.should_update if @container
end

#delete_criterion(priority) ⇒ void

This method returns an undefined value.

Remove a criterion from the criteria

Parameters:

  • priority (Integer)

    the priority/index of the criterion to delete



163
164
165
166
167
168
169
170
# File 'lib/jss/api_object/criteriable/criteria.rb', line 163

def delete_criterion(priority)
  if @criteria[priority]
    raise JSS::MissingDataError, "Criteria can't be empty" if @criteria.count == 1
    @criteria.delete_at priority
    set_priorities
  end
  @container.should_update if @container
end

#insert_criterion(priority, criterion) ⇒ void

This method returns an undefined value.

Add a new criterion to the middle of the criteria

Parameters:

  • priority (Integer)

    the priority/index before which to insert the new one.

  • criterion (JSS::Criteriable::Criterion)

    the new Criterion to store at that index



149
150
151
152
153
154
# File 'lib/jss/api_object/criteriable/criteria.rb', line 149

def insert_criterion(priority, criterion)
  criterion_ok? criterion
  @criteria.insert criterion[:priority], criterion
  set_priorities
  @container.should_update if @container
end

#prepend_criterion(criterion) ⇒ void

This method returns an undefined value.

Add a new criterion to the beginning of the criteria

Parameters:



133
134
135
136
137
138
# File 'lib/jss/api_object/criteriable/criteria.rb', line 133

def prepend_criterion(criterion)
  criterion_ok? criterion
  @criteria.unshift criterion
  set_priorities
  @container.should_update if @container
end

#pretty_print_instance_variablesArray

Remove the various cached data from the instance_variables used to create pretty-print (pp) output.

Returns:

  • (Array)

    the desired instance_variables



206
207
208
209
210
# File 'lib/jss/api_object/criteriable/criteria.rb', line 206

def pretty_print_instance_variables
  vars = instance_variables.sort
  vars.delete :@container
  vars
end

#rest_xmlREXML::Element

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This can't be a private method for this class since container classes must call it

Returns the xml element for the criteria.

Returns:

  • (REXML::Element)

    the xml element for the criteria



219
220
221
222
223
# File 'lib/jss/api_object/criteriable/criteria.rb', line 219

def rest_xml
  cr = REXML::Element.new 'criteria'
  @criteria.each { |c| cr << c.rest_xml }
  cr
end

#set_criterion(priority, criterion) ⇒ void

This method returns an undefined value.

Change the details of one specific criterion

Parameters:

  • priority (Integer)

    the priority/index of the criterion being changed. The index must already exist. Otherwise use #append_criterion, #prepend_criterion, or #insert_criterion

  • criterion (JSS::Criteriable::Criterion)

    the new Criterion to store at that index

Raises:



183
184
185
186
187
188
189
# File 'lib/jss/api_object/criteriable/criteria.rb', line 183

def set_criterion(priority, criterion)
  raise JSS::NoSuchItemError, "No current criterion with priority '#{priority}'" unless @criteria[priority]
  criterion_ok? criterion
  @criteria[priority] = criterion
  set_priorities
  @container.should_update if @container
end

#set_prioritiesvoid

This method returns an undefined value.

Set the priorities of the @criteria to match their array indices



196
197
198
# File 'lib/jss/api_object/criteriable/criteria.rb', line 196

def set_priorities
  @criteria.each_index { |ci| @criteria[ci].priority = ci }
end