Module: JSS::Sitable

Overview

A mix-in module that centralizes the code for handling objects which can be assigned a ‘site’ in the JSS.

Objects in the JSS present site data in the top-level :general Hash in the :site key which is a Hash with a :name and :id key.

Classes mixing in this module MUST:

  • define the constant SITE_SUBSET as either :top, :general, or whatever sub-hash of API data conaints the site info. (most are :general, but some, like advanced searches, are at the top level)

  • call #add_site_to_xml(xmldoc) from their #rest_xml method if they are Updatable or Creatable

Constant Summary collapse

SITABLE =

Module Constants

true
NO_SITE_NAME =

When no site has been assigned, this is the ‘name’ and id used

'None'.freeze
NO_SITE_ID =
-1
NON_SITES =

Setting the site to any of these values will unset the site

[
  nil,
  '',
  0,
  NO_SITE_NAME,
  NO_SITE_ID
].freeze

Instance Method Summary collapse

Instance Method Details

#site=(new_site) ⇒ void

This method returns an undefined value.

Change the site of this object. Any of the NON_SITES values will unset the site

Parameters:

  • new_site (Integer, String)

    The new site

Raises:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/jss/api_object/sitable.rb', line 120

def site=(new_site)
  return nil unless updatable? || creatable?

  # unset the site? Use nil or an empty string
  if NON_SITES.include? new_site
    unset_site
    return
  end

  new_id = JSS::Site.valid_id new_site, api: @api
  new_name = JSS::Site.map_all_ids_to(:name, api: @api)[new_id]
  # no change, go home.
  return nil if new_name == @site_name

  raise JSS::NoSuchItemError, "Site '#{new_site}' is not known to the JSS" unless new_id

  @site_name = new_name
  @site_id = new_id
  @need_to_update = true
end

#site_assigned?Boolean

Does this object have a site assigned?

Returns:

  • (Boolean)

    Does this object have a site assigned?



108
109
110
# File 'lib/jss/api_object/sitable.rb', line 108

def site_assigned?
  !@site_name.nil?
end

#site_idInteger

The id of the site for this object.

Returns:

  • (Integer)

    The id of the site for this object.



91
92
93
# File 'lib/jss/api_object/sitable.rb', line 91

def site_id
  @site_id
end

#site_nameString Also known as: site

The name of the site for this object. For backward compatibility, this is aliased to just ‘site’

Returns:

  • (String)

    The name of the site for this object.



82
83
84
# File 'lib/jss/api_object/sitable.rb', line 82

def site_name
  @site_name
end

#site_objectJSS::Site

The JSS::Site instance for this object’s site

Returns:

  • (JSS::Site)

    The JSS::Site instance for this object’s site



99
100
101
102
# File 'lib/jss/api_object/sitable.rb', line 99

def site_object
  return nil unless site_assigned?
  JSS::Site.new id: @site_id
end

#unset_sitevoid

This method returns an undefined value.

Set the site to nothing



145
146
147
148
149
150
151
# File 'lib/jss/api_object/sitable.rb', line 145

def unset_site
  # no change, go home
  return nil if @site_name.nil?
  @site_name = nil
  @site_id = nil
  @need_to_update = true
end