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
- 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
-
#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, api: nil) ⇒ 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
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_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
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
#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.
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 |
#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.
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.
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.
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.
#prepend_field(field = {}) ⇒ void
This method returns an undefined value.
Add a new field to the beginning of the field list
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
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 |
#update ⇒ Boolean Originally defined in module Updatable
Save changes to the JSS