Class: JSS::PeripheralType
- 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
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
- VALID_DATA_KEYS =
these keys, as well as :id and :name, are present in valid API JSON data for this class
[:fields]
- FIELD_TYPES =
field types can be one of these, either String or Symbol
[:text, :menu]
Instance Attribute Summary collapse
-
#need_to_update ⇒ Boolean
included
from Updatable
readonly
Do we have unsaved changes?.
Instance Method Summary collapse
-
#append_field(field = {}) ⇒ void
Add a new field to the end of the field list.
-
#clone(new_name) ⇒ APIObject
included
from Creatable
make a clone of this API object, with a new name.
-
#create ⇒ Integer
included
from Creatable
Create a new object in the JSS.
-
#delete_field(order) ⇒ void
Remove a field from the array of fields.
-
#fields ⇒ Array<Hash>
The definitions of the fields stored for this peripheral type.
-
#fields=(new_fields) ⇒ void
Replace the entire Array of field definitions.
-
#initialize(args = {}) ⇒ PeripheralType
constructor
Initialize.
-
#insert_field(order, field = {}) ⇒ void
Add a new field to the middle of the fields Array.
-
#name=(newname) ⇒ void
included
from Updatable
Change the name of this item Remember to #update to push changes to the server.
-
#prepend_field(field = {}) ⇒ void
Add a new field to the beginning of the field list.
-
#set_field(order, field = {}) ⇒ void
Replace the details of one specific field.
-
#update ⇒ Boolean
included
from Updatable
Save changes to the JSS.
Constructor Details
#initialize(args = {}) ⇒ PeripheralType
Initialize
89 90 91 92 93 94 95 96 97 |
# File 'lib/jss/api_object/peripheral_type.rb', line 89 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_update ⇒ Boolean (readonly) Originally defined in module Updatable
Returns 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
181 182 183 184 185 186 |
# File 'lib/jss/api_object/peripheral_type.rb', line 181 def append_field(field = {}) field_ok? field @fields << field order_fields @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 ⇒ Integer Originally defined in module Creatable
Create a new object in the JSS.
#delete_field(order) ⇒ void
This method returns an undefined value.
Remove a field from the array of fields.
225 226 227 228 229 230 231 232 |
# File 'lib/jss/api_object/peripheral_type.rb', line 225 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 |
#fields ⇒ Array<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.
131 132 133 |
# File 'lib/jss/api_object/peripheral_type.rb', line 131 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.
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/jss/api_object/peripheral_type.rb', line 144 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.
211 212 213 214 215 216 |
# File 'lib/jss/api_object/peripheral_type.rb', line 211 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.
#prepend_field(field = {}) ⇒ void
This method returns an undefined value.
Add a new field to the beginning of the field list
195 196 197 198 199 200 |
# File 'lib/jss/api_object/peripheral_type.rb', line 195 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
167 168 169 170 171 172 |
# File 'lib/jss/api_object/peripheral_type.rb', line 167 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 |
#update ⇒ Boolean Originally defined in module Updatable
Save changes to the JSS