Module: Jamf::Referable
- Included in:
- Attachment, Building, Category, Computer, Department, MobileDevice, Script, Site
- Defined in:
- lib/jamf/api/mixins/referable.rb
Overview
CollectionResource Class instances with this module mixed-in can be referred to in other classes.
Doing so dynamically defines a new ‘Reference’ class, which is a subclass of GenericReference
Examples:
1) A Policy can have a category assigned to it, and the policy’s API data will contain a reference to a category.
Jamf::Policy instances have a :category attribute, which contains an instance of Jamf::Category::Reference
Mixing Referable into Jamf::Category automatically creates the class Jamf::Category::Reference
2) The API data for a Jamf::ComputerGroup contains a an Array of references to member Jamf::Computers. That Array arrives in JSON like this:
computers: [
{ id: 234, name: 'foobar' },
{ id: 698, name: 'barfoo' }
]
Mixing Referable into Jamf::Computer defines the class Jamf::Computer::Reference and allows Jamf::ComputerGroups to maintain an array of Jamf::Computer::Reference objects representing that list. When needed to send to the API, it will send this:
computers: [
{ id: 234 },
{ id: 698 }
]
Parsing the API data into Reference instances, and converting them back for the API is handled by the Reference class.
TODO: Handle if any references contain keys other than :id and :name.
Constant Summary collapse
- GENREF =
this will hopefully autoload generic_reference
Jamf::GenericReference
Class Method Summary collapse
-
.included(referent) ⇒ Object
This is run when Referable is included in some class ‘referent’ It creates class referent::Reference as a subclass of GenericReference, and sets the REFERENT_CLASS constant of referent::Reference to referent.
Instance Method Summary collapse
-
#reference ⇒ self.class::GenericReference
A reference to this object.
Class Method Details
.included(referent) ⇒ Object
This is run when Referable is included in some class ‘referent’ It creates class referent::Reference as a subclass of GenericReference, and sets the REFERENT_CLASS constant of referent::Reference to referent.
78 79 80 81 82 |
# File 'lib/jamf/api/mixins/referable.rb', line 78 def self.included(referent) raise JSS::InvalidDataError, "'#{referent}' is not a subclass of Jamf::CollectionResource, can't include 'Referable'" unless referent.ancestors.include? Jamf::CollectionResource referent.const_set :Reference, Class.new(GENREF) referent::Reference.const_set :REFERENT_CLASS, referent end |
Instance Method Details
#reference ⇒ self.class::GenericReference
Returns A reference to this object.
86 87 88 |
# File 'lib/jamf/api/mixins/referable.rb', line 86 def reference @reference ||= self.class::Reference.new self end |