Class: JSS::Group
- Includes:
- Creatable, Criteriable, Updatable
- Defined in:
- lib/jss/api_object/group.rb,
lib/jss.rb
Overview
This is the parent class of the smart/static group objects in the JSS namely, ComputerGroup, MobileDeviceGroup, and UserGroup
It provides methods for working with the membership of static groups and, by including Criteriable, the criteria for smart groups.
When changing the criteria of a smart group, use the #criteria attribute, which is a Criteria instance.
Subclasses must define the constant MEMBER_CLASS which indicates Ruby class to which the group members belong (e.g. JSS::MobileDevice)
Direct Known Subclasses
Constant Summary collapse
- GROUP_TYPES =
the types of groups allowed for creation
[:smart, :static]
Instance Attribute Summary collapse
-
#criteria ⇒ JSS::Criteriable::Criteria
included
from Criteriable
The criteria for the instance into which we’re mixed.
-
#is_smart ⇒ Boolean
(also: #smart?)
readonly
Is this a smart group.
-
#members ⇒ Array<Hash>
Each hash contains the identifiers for a member of the group, those being: - :id, :name, and possibly :udid, :serial_number, :mac_address, :alt_mac_address, and :wifi_mac_address.
-
#need_to_update ⇒ Boolean
included
from Updatable
readonly
Do we have unsaved changes?.
-
#notify_on_change ⇒ Boolean
(also: #notify_on_change?, #notify?)
readonly
Does this group send notifications when it changes?.
-
#site ⇒ String
The :name of the site for this group.
Class Method Summary collapse
-
.all_smart(refresh = false) ⇒ Object
Returns an Array of all the smart groups.
-
.all_static(refresh = false) ⇒ Object
Returns an Array of all the static groups.
Instance Method Summary collapse
-
#add_member(m) ⇒ void
Add a member, by name or id.
-
#clear ⇒ void
Remove all members.
-
#clone(new_name) ⇒ APIObject
included
from Creatable
make a clone of this API object, with a new name.
- #create ⇒ Object
-
#criteria=(new_criteria) ⇒ Object
Apply a new set of criteria to a smart group.
- #delete ⇒ Object
-
#initialize(args = {}) ⇒ Group
constructor
When creating a new group in the JSS, you must call .new with a :type key and a value of :smart or :static, as well as a :name and the :id => :new.
-
#member_ids ⇒ Array<Integer>
The ids of the group members.
-
#member_names ⇒ Array<String>
The names of the group members.
-
#name=(newname) ⇒ void
included
from Updatable
Change the name of this item Remember to #update to push changes to the server.
-
#parse_criteria ⇒ void
included
from Criteriable
During initialization, convert the @init_data Hash into a JSS::Criteriable::Criteria instance stored in @criteria.
-
#refresh_members ⇒ Array<Hash>
Refresh the membership from the API.
-
#remove_member(m) ⇒ void
Remove a member by id, or name.
-
#should_update ⇒ void
included
from Criteriable
private
Allow our Criteria to tell us when there’s been a change that needs to be updated.
-
#size ⇒ Integer
(also: #count)
How many members of the group?.
- #update ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Group
When creating a new group in the JSS, you must call .new with a :type key and a value of :smart or :static, as well as a :name and the :id => :new
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/jss/api_object/group.rb', line 140 def initialize(args = {}) if args[:id] == :new raise JSS::InvalidDataError, "New group creation must specify a :type of :smart or :static" unless GROUP_TYPES.include? args[:type] end super args @is_smart = @init_data[:is_smart] || (args[:type] == :smart) @members = if @init_data[self.class::MEMBER_CLASS::RSRC_LIST_KEY] @init_data[self.class::MEMBER_CLASS::RSRC_LIST_KEY] else [] end @site = JSS::APIObject.get_name(@init_data[:site]) end |
Instance Attribute Details
#criteria ⇒ JSS::Criteriable::Criteria Originally defined in module Criteriable
Returns the criteria for the instance into which we’re mixed.
#is_smart ⇒ Boolean (readonly) Also known as: smart?
Returns is this a smart group.
120 121 122 |
# File 'lib/jss/api_object/group.rb', line 120 def is_smart @is_smart end |
#members ⇒ Array<Hash>
Each hash contains the identifiers for a member of the group, those being:
-
:id, :name, and possibly :udid, :serial_number, :mac_address, :alt_mac_address, and :wifi_mac_address
117 118 119 |
# File 'lib/jss/api_object/group.rb', line 117 def members @members end |
#need_to_update ⇒ Boolean (readonly) Originally defined in module Updatable
Returns do we have unsaved changes?.
#notify_on_change ⇒ Boolean (readonly) Also known as: notify_on_change?, notify?
Returns does this group send notifications when it changes?.
123 124 125 |
# File 'lib/jss/api_object/group.rb', line 123 def notify_on_change @notify_on_change end |
#site ⇒ String
Returns the :name of the site for this group.
127 128 129 |
# File 'lib/jss/api_object/group.rb', line 127 def site @site end |
Class Method Details
.all_smart(refresh = false) ⇒ Object
Returns an Array of all the smart groups.
91 92 93 |
# File 'lib/jss/api_object/group.rb', line 91 def self.all_smart(refresh = false) self.all(refresh).select{|g| g[:is_smart] } end |
.all_static(refresh = false) ⇒ Object
Returns an Array of all the static groups.
99 100 101 |
# File 'lib/jss/api_object/group.rb', line 99 def self.all_static(refresh = false) self.all(refresh).select{|g| not g[:is_smart] } end |
Instance Method Details
#add_member(m) ⇒ void
This method returns an undefined value.
Add a member, by name or id
266 267 268 269 270 |
# File 'lib/jss/api_object/group.rb', line 266 def add_member(m) raise UnsupportedError, "Smart group members can't be changed." if @is_smart @members << check_member(m) @need_to_update = true end |
#clear ⇒ void
This method returns an undefined value.
Remove all members
294 295 296 297 298 299 |
# File 'lib/jss/api_object/group.rb', line 294 def clear raise InvalidDataError, "Smart group members can't be changed." if @is_smart return if @members.empty? @members.clear @need_to_update = true end |
#clone(new_name) ⇒ APIObject Originally defined in module Creatable
make a clone of this API object, with a new name. The class must be creatable
#create ⇒ Object
167 168 169 170 171 172 173 174 |
# File 'lib/jss/api_object/group.rb', line 167 def create if @is_smart raise JSS::MissingDataError, "No criteria specified for smart group" unless @criteria end super refresh_members return @id end |
#criteria=(new_criteria) ⇒ Object
Apply a new set of criteria to a smart group
202 203 204 205 |
# File 'lib/jss/api_object/group.rb', line 202 def criteria= (new_criteria) raise InvalidDataError, "Only smart groups have criteria." unless @is_smart super end |
#delete ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/jss/api_object/group.rb', line 189 def delete super @is_smart = nil @criteria = nil @site = nil @members = [] end |
#member_ids ⇒ Array<Integer>
Returns the ids of the group members.
227 228 229 |
# File 'lib/jss/api_object/group.rb', line 227 def member_ids @members.map{|m| m[:id]} end |
#member_names ⇒ Array<String>
Returns the names of the group members.
220 221 222 |
# File 'lib/jss/api_object/group.rb', line 220 def member_names @members.map{|m| m[:name]} end |
#name=(newname) ⇒ void Originally defined in module Updatable
This method returns an undefined value.
Change the name of this item Remember to #update to push changes to the server.
#parse_criteria ⇒ void Originally defined in module Criteriable
This method returns an undefined value.
During initialization, convert the @init_data Hash into a JSS::Criteriable::Criteria instance stored in @criteria
Classes mixing in this module must call this in #initialize
#refresh_members ⇒ Array<Hash>
Refresh the membership from the API
306 307 308 |
# File 'lib/jss/api_object/group.rb', line 306 def refresh_members @members = JSS.api_connection.get_rsrc(@rest_rsrc)[self.class::RSRC_OBJECT_KEY][self.class::MEMBER_CLASS::RSRC_LIST_KEY] end |
#remove_member(m) ⇒ void
This method returns an undefined value.
Remove a member by id, or name
279 280 281 282 283 284 285 286 287 |
# File 'lib/jss/api_object/group.rb', line 279 def remove_member(m) raise InvalidDataError, "Smart group members can't be changed." if @is_smart if @members.reject!{ |mm| [mm[:id], mm[:name]].include? m } @need_to_update = true else raise JSS::NoSuchItemError, "No member matches '#{m}'" end end |
#should_update ⇒ void Originally defined in module Criteriable
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.
This method returns an undefined value.
Allow our Criteria to tell us when there’s been a change that needs to be updated.
#size ⇒ Integer Also known as: count
How many members of the group?
212 213 214 |
# File 'lib/jss/api_object/group.rb', line 212 def size @members.count end |
#update ⇒ Object
180 181 182 183 184 |
# File 'lib/jss/api_object/group.rb', line 180 def update super refresh_members true end |