class Jamf::Criteriable::Criteria
This class stores an array of {Jamf::Criteriable::Criterion} instances and provides methods for working with them as a group.
{Jamf::APIObject} subclasses that include {Jamf::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 {Jamf::Criteriable} module and are mixed in when it’s included.
See {Jamf::Criteriable} for examples
Constants
- CRITERION_ATTRIBUTES
Criterion
instances we maintain need these attributes.s
Attributes
@return [Jamf::APIObject subclass] a reference to the object containing these Criteria
@return [Array] the group of Jamf::Criteriable::Criterion
instances making up these Criteria
Public Class Methods
@param new_criteria
# File lib/jamf/api/classic/api_objects/criteriable/criteria.rb 80 def initialize(new_criteria = []) 81 @criteria = [] 82 83 # validates the param and fills @criteria 84 self.criteria = new_criteria 85 end
Public Instance Methods
Add a new criterion to the end of the criteria
@param criterion the new Criterion
to store
@return [void]
# File lib/jamf/api/classic/api_objects/criteriable/criteria.rb 119 def append_criterion(criterion) 120 criterion_ok? criterion 121 criterion.priority = @criteria.length 122 @criteria << criterion 123 @container.should_update if @container 124 end
Remove all criterion objects
@return [void]
# File lib/jamf/api/classic/api_objects/criteriable/criteria.rb 107 def clear 108 @criteria = [] 109 @container.should_update if @container 110 end
Provide a whole new array of Jamf::Criteriable::Criterion
instances for this Criteria
@param new_criteria
@return [void]
# File lib/jamf/api/classic/api_objects/criteriable/criteria.rb 94 def criteria=(new_criteria) 95 unless new_criteria.is_a?(Array) && new_criteria.reject { |c| c.is_a?(Jamf::Criteriable::Criterion) }.empty? 96 raise Jamf::InvalidDataError, 'Argument must be an Array of Jamf::Criteriable::Criterion instances.' 97 end 98 new_criteria.each { |nc| criterion_ok? nc } 99 @criteria = new_criteria 100 set_priorities 101 @container.should_update if @container 102 end
Remove a criterion from the criteria
@param priority the priority/index of the criterion to delete
@return [void]
# File lib/jamf/api/classic/api_objects/criteriable/criteria.rb 163 def delete_criterion(priority) 164 if @criteria[priority] 165 raise Jamf::MissingDataError, "Criteria can't be empty" if @criteria.count == 1 166 @criteria.delete_at priority 167 set_priorities 168 end 169 @container.should_update if @container 170 end
Add a new criterion to the middle of the criteria
@param priority the priority/index before which to insert the new one.
@param criterion the new Criterion
to store at that index
@return [void]
# File lib/jamf/api/classic/api_objects/criteriable/criteria.rb 149 def insert_criterion(priority, criterion) 150 criterion_ok? criterion 151 @criteria.insert criterion[:priority], criterion 152 set_priorities 153 @container.should_update if @container 154 end
Add a new criterion to the beginning of the criteria
@param criterion the new Criterion
to store
@return [void]
# File lib/jamf/api/classic/api_objects/criteriable/criteria.rb 133 def prepend_criterion(criterion) 134 criterion_ok? criterion 135 @criteria.unshift criterion 136 set_priorities 137 @container.should_update if @container 138 end
Remove the various cached data from the instance_variables used to create pretty-print (pp) output.
@return [Array] the desired instance_variables
# File lib/jamf/api/classic/api_objects/criteriable/criteria.rb 206 def pretty_print_instance_variables 207 vars = instance_variables.sort 208 vars.delete :@container 209 vars 210 end
@return [REXML::Element] the xml element for the criteria
@note This can’t be a private method for this class since container classes must call it
@api private
# File lib/jamf/api/classic/api_objects/criteriable/criteria.rb 219 def rest_xml 220 cr = REXML::Element.new 'criteria' 221 @criteria.each { |c| cr << c.rest_xml } 222 cr 223 end
Change the details of one specific criterion
@param priority the priority/index of the criterion being changed.
The index must already exist. Otherwise use #append_criterion, #prepend_criterion, or #insert_criterion
@param criterion the new Criterion
to store at that index
@return [void]
# File lib/jamf/api/classic/api_objects/criteriable/criteria.rb 183 def set_criterion(priority, criterion) 184 raise Jamf::NoSuchItemError, "No current criterion with priority '#{priority}'" unless @criteria[priority] 185 criterion_ok? criterion 186 @criteria[priority] = criterion 187 set_priorities 188 @container.should_update if @container 189 end
Set the priorities of the @criteria to match their array indices
@return [void]
# File lib/jamf/api/classic/api_objects/criteriable/criteria.rb 196 def set_priorities 197 @criteria.each_index { |ci| @criteria[ci].priority = ci } 198 end
Private Instance Methods
Chech the validity of a criterion. Note that this doesn’t check the :priority which is set by methods calling this one.
Return true or raise an error about the problem
# File lib/jamf/api/classic/api_objects/criteriable/criteria.rb 237 def criterion_ok?(criterion) 238 raise Jamf::InvalidDataError, "Duplicate criterion: #{criterion.signature}" if @criteria.select { |c| c == criterion }.count > 1 239 raise Jamf::InvalidDataError, "Missing :and_or for criterion: #{criterion.signature}" unless criterion.and_or 240 raise Jamf::InvalidDataError, "Missing :name for criterion: #{criterion.signature}" unless criterion.name 241 raise Jamf::InvalidDataError, "Missing :search_type for criterion: #{criterion.signature}" unless criterion.search_type 242 raise Jamf::InvalidDataError, "Missing :value for criterion: #{criterion.signature}" unless criterion.value 243 true 244 end