Class: JSS::PeripheralType

Inherits:
APIObject show all
Includes:
Creatable, Updatable
Defined in:
lib/jss/api_object/peripheral_type.rb,
lib/jss.rb

Overview

A peripheral_type in the JSS.

A PeripheralType (as opposed to an individual Peripheral) is just an id, a name, and an Array of Hashes describing the fields of data to be stored for peripherals of this type.

See #fields for a desciption of how field definitions are stored.

For manipulating the fields, see #fields=, #set_field, #append_field, #prepend_field, #insert_field, and #delete_field

See Also:

Constant Summary collapse

RSRC_BASE =

The base for REST resources of this class

"peripheraltypes"
RSRC_LIST_KEY =

the hash key used for the JSON list output of all objects in the JSS

:peripheral_types
RSRC_OBJECT_KEY =

The hash key used for the JSON object output. It’s also used in various error messages

:peripheral_type
FIELD_TYPES =

field types can be one of these, either String or Symbol

[:text, :menu]
OBJECT_HISTORY_OBJECT_TYPE =

the object type for this object in the object history table. See APIObject#add_object_history_entry

11

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ PeripheralType

Initialize



92
93
94
95
96
97
98
99
100
# File 'lib/jss/api_object/peripheral_type.rb', line 92

def initialize (args = {})

  super

  @fields = []
  if @init_data[:fields]
    @init_data[:fields].each{ |f|  @fields[f[:order]] = f }
  end
end

Instance Attribute Details

#need_to_updateBoolean (readonly) Originally defined in module Updatable

Returns do we have unsaved changes?.

Returns:

  • (Boolean)

    do we have unsaved changes?

Instance Method Details

#append_field(field = {}) ⇒ void

This method returns an undefined value.

Add a new field to the end of the field list

Parameters:

  • field (Hash) (defaults to: {})

    the new field data



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

def append_field(field = {})
  field_ok? field
  @fields << field
  order_fields
  @need_to_update = true
end

#clone(new_name, api: nil) ⇒ APIObject Originally defined in module Creatable

make a clone of this API object, with a new name. The class must be creatable

Parameters:

  • name (String)

    the name for the new object

  • api (JSS::APIConnection) (defaults to: nil)

    the API in which to create the object Defaults to the API used to instantiate this object

Returns:

  • (APIObject)

    An uncreated clone of this APIObject with the given name

Raises:

#createInteger Originally defined in module Creatable

Create a new object in the JSS.

Parameters:

  • api (JSS::APIConnection)

    the API in which to create the object Defaults to the API used to instantiate this object

Returns:

  • (Integer)

    the jss ID of the newly created object

Raises:

#delete_field(order) ⇒ void

This method returns an undefined value.

Remove a field from the array of fields.

Parameters:

  • order (Integer)

    which field to remove?



228
229
230
231
232
233
234
235
# File 'lib/jss/api_object/peripheral_type.rb', line 228

def delete_field(order)
  if @fields[order]
    raise JSS::MissingDataError, "Fields can't be empty" if @fields.count == 1
    @fields.delete_at index
    order_fields
    @need_to_update = true
  end
end

#fieldsArray<Hash>

The definitions of the fields stored for this peripheral type.

Each Hash defines a field of data to store. The keys are:

  • :name, String, the name of the field

  • :type, String or Symbol, the kind of data to be stored in the field, either :text or :menu

  • :choices, Array of Strings - if type is :menu, these are the menu choices.

  • :order, the one-based index of this field amongst it’s peers.

Since Arrays are zero-based, and the field order is one-based, keeping a nil at the front of the Array will keep the :order number in sync with the Array index of each field definition. This is done automatically by the field-editing methods: #fields=, #set_field, #append_field, #prepend_field, #insert_field, and #delete_field.

So the Array from the API comes like this:

[ {:type=>"text", :order=>1, :choices=>[], :name=>"make"},
  {:type=>"text", :order=>2, :choices=>[], :name=>"model"},
  {:type=>"text", :order=>3, :choices=>[], :name=>"family"},
  {:type=>"text", :order=>4, :choices=>[], :name=>"serialnum"} ]

But will be stored in a PeripheralType instance like this:

[ nil,
  {:type=>"text", :order=>1, :choices=>[], :name=>"make"},
  {:type=>"text", :order=>2, :choices=>[], :name=>"model"},
  {:type=>"text", :order=>3, :choices=>[], :name=>"family"},
  {:type=>"text", :order=>4, :choices=>[], :name=>"serialnum"} ]

therefore

myPerifType.fields[2]

will get you the second field, which has :order => 2.

Returns:



134
135
136
# File 'lib/jss/api_object/peripheral_type.rb', line 134

def fields
  @fields
end

#fields=(new_fields) ⇒ void

This method returns an undefined value.

Replace the entire Array of field definitions. The :order of each will be set based on the indexes of the Array provided.

Parameters:

  • new_fields (Array<Hash>)

    the new field definitions



147
148
149
150
151
152
153
154
155
156
# File 'lib/jss/api_object/peripheral_type.rb', line 147

def fields= (new_fields)
  unless new_fields.kind_of? Array and  new_fields.reject{|c| c.kind_of? Hash }.empty?
    raise JSS::InvalidDataError, "Argument must be an Array of Hashes."
  end
  raise "A peripheral type can have a maximmum of 20 fields"  if new_fields.count > 20
  new_fields.each{ |f| field_ok? f }
  @fields = new_fields
  order_fields
  @need_to_update = true
end

#insert_field(order, field = {}) ⇒ void

This method returns an undefined value.

Add a new field to the middle of the fields Array.

Parameters:

  • order (Integer)

    where does the new field go?

  • field (Hash) (defaults to: {})

    the new field data



214
215
216
217
218
219
# File 'lib/jss/api_object/peripheral_type.rb', line 214

def insert_field(order,field = {})
  field_ok? field
  @fields.insert((order -1), field)
  order_fields
  @need_to_update = true
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.

Parameters:

  • newname (String)

    the new name

Raises:

#prepend_field(field = {}) ⇒ void

This method returns an undefined value.

Add a new field to the beginning of the field list

Parameters:

  • field (Hash) (defaults to: {})

    the new field data



198
199
200
201
202
203
# File 'lib/jss/api_object/peripheral_type.rb', line 198

def prepend_field(field = {})
  field_ok? field
  @fields.unshift field
  order_fields
  @need_to_update = true
end

#set_field(order, field = {}) ⇒ void

This method returns an undefined value.

Replace the details of one specific field.

The order must already exist. Otherwise use #append_field, #prepend_field, or #insert_field

Parameters:

  • order (Integer)

    which field are we replacing?

  • field (Hash) (defaults to: {})

    the new field data

Raises:



170
171
172
173
174
175
# File 'lib/jss/api_object/peripheral_type.rb', line 170

def set_field(order, field = {})
  raise JSS::NoSuchItemError, "No field with number '#{order}'. Use #append_field, #prepend_field, or #insert_field" unless @fields[order]
  field_ok? field
  @fields[order] = field
  @need_to_update = true
end

#updateBoolean Originally defined in module Updatable

Save changes to the JSS

Returns:

  • (Boolean)

    success

Raises: