class Jamf::Criteriable::Criterion

This class defines a single criterion used in advanced searches and smart groups throughout the JSS module.

They are used within {Jamf::Criteriable::Criteria} instances which store an array of these objects and provides methods for working with them as a group.

The classes that mix-in {Jamf::Criteriable} each have a :criteria attribute which holds one {Jamf::Criteriable::Criteria}

See {Jamf::Criteriable} for examples

Constants

AND_OR

the acceptable symboles for and/or

SEARCH_TYPES

These are the available search-types for building criteria

Attributes

and_or[R]

@return [Symbol] :and or :or - the and_or value for associating this criterion with the previous one, defaults to :and

closing_paren[R]

@return [Boolean] Is there a closing paren after this criterion

name[RW]

@return [String] the name of the field being searched

opening_paren[R]

@return [Boolean] Is there an opening paren before this criterion

priority[RW]

@return [Integer] zero-based index of this criterion within an array of criteria

used for an advanced search or smart group.
This is maintained automaticaly by the enclosing Criteria object
search_type[R]

@return [String] the comparator between the field and the value, must be one of SEARCH_TYPES @see criteria=

value[R]

@return [String] the value being searched for in the field named by :name

Public Class Methods

new(**args) click to toggle source

@param args a hash of settings for the new criterion @option args :and_or [String, Symbol] :and, or :or. How should this criterion be join with its predecessor? @option args :name [String] the name of a Criterion as is visible in the JSS webapp. @option args :search_type [String] one of SEARCH_TYPES, the comparison between the stored value and :value @option args :value [String] the value to compare with that stored for :name

@note :priority is maintained by the Jamf::Criteriable::Criteria object holding this instance

    # File lib/jamf/api/classic/api_objects/criteriable/criterion.rb
125 def initialize(**args)
126   @priority = args[:priority]
127 
128   @and_or = (args[:and_or].downcase.to_sym if args[:and_or]) || :and
129   raise Jamf::InvalidDataError, ":and_or must be 'and' or 'or'." unless AND_OR.include? @and_or
130 
131   @name = args[:name]
132 
133   if args[:search_type]
134     raise Jamf::InvalidDataError, 'Invalid :search_type' unless SEARCH_TYPES.include? args[:search_type]
135     @search_type = args[:search_type]
136   end
137 
138   # from the API, parens come like this
139   @opening_paren = args[:opening_paren]
140   @closing_paren = args[:closing_paren]
141 
142   # but from a user, they might come as a single :paren key, which
143   # will be handled by the setter below
144   send 'paren=', args[:paren] if args.key? :paren
145 
146   # default to false
147   @opening_paren ||= false
148   @closing_paren ||= false
149 
150   @value = args[:value]
151 end

Public Instance Methods

<=>(other) click to toggle source

Comparison - allows the Comparable module to do its work

@return [Integer] -1, 0, or 1

@see Comparable

    # File lib/jamf/api/classic/api_objects/criteriable/criterion.rb
231 def <=>(other)
232   signature <=> other.signature
233 end
and_or=(new_val) click to toggle source

Set a new and_or for the criteron

@param new_val the new and_or

@return [void]

    # File lib/jamf/api/classic/api_objects/criteriable/criterion.rb
159 def and_or=(new_val)
160   @and_or = new_val.to_sym
161   raise Jamf::InvalidDataError, ":and_or must be 'and' or 'or'." unless AND_OR.include? @and_or.to_sym
162 end
paren=(new_val) click to toggle source

set the parenthesis for the criteria

@param side :opening, :closing, or nil to remove

@return [void]

    # File lib/jamf/api/classic/api_objects/criteriable/criterion.rb
169 def paren=(new_val)
170   case new_val
171   when :opening
172     @opening_paren = true
173     @closing_paren = false
174   when :closing
175     @opening_paren = false
176     @closing_paren = true
177   when nil
178     @opening_paren = false
179     @closing_paren = false
180   else
181     raise Jamf::InvalidDataError, 'paren must be :opening, :closing, or nil.'
182   end
183 end
rest_xml() click to toggle source

@api private

@return [REXML::Element] The xml element for the criterion, to be embeded in that of

a Criteria instance

@note For this class, rest_xml can’t be a private method.

    # File lib/jamf/api/classic/api_objects/criteriable/criterion.rb
242 def rest_xml
243   crn = REXML::Element.new 'criterion'
244   crn.add_element('priority').text = @priority
245   crn.add_element('and_or').text = @and_or
246   crn.add_element('name').text = @name
247   crn.add_element('search_type').text = @search_type
248   crn.add_element('value').text = @value
249   crn.add_element('opening_paren').text = @opening_paren ? 'true' : 'false'
250   crn.add_element('closing_paren').text = @closing_paren ? 'true' : 'false'
251   crn
252 end
search_type=(new_val) click to toggle source

Set a new search type for the criteron

@param new_val the new search type

@return [void]

    # File lib/jamf/api/classic/api_objects/criteriable/criterion.rb
191 def search_type=(new_val)
192   raise Jamf::InvalidDataError, 'Invalid :search_type' unless SEARCH_TYPES.include? new_val
193   @search_type = new_val
194 end
signature() click to toggle source

@return [String] All our values except priority joined together

for comparing this Criterion to another for equality and order

@see <=>

    # File lib/jamf/api/classic/api_objects/criteriable/criterion.rb
221 def signature
222   [@and_or, @name, @search_type, @value].join ','
223 end
value=(new_val) click to toggle source

Set a new value for the criteron

@param new_val the new value

@return [void]

    # File lib/jamf/api/classic/api_objects/criteriable/criterion.rb
202 def value=(new_val)
203   case @search_type
204 
205   when 'more than', 'less than', 'more than x days ago', 'less than x days ago'
206     raise Jamf::InvalidDataError, "Value must be an integer for search type '#{new_val}'" unless new_val =~ /^\d+$/
207 
208   when 'before (yyyy-mm-dd)', 'after (yyyy-mm-dd)'
209     raise Jamf::InvalidDataError, "Value must be a a date in the format yyyy-mm-dd for search type '#{new_val}'" unless new_val =~ /^\d\d\d\d-\d\d-\d\d$/
210 
211   end # case
212 
213   @value = new_val
214 end