Module: Windoo::Mixins::APICollection::ClassMethods

Defined in:
lib/windoo/mixins/api_collection.rb

Overview

Class Methods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(extender) ⇒ Object



81
82
83
# File 'lib/windoo/mixins/api_collection.rb', line 81

def self.extended(extender)
  Windoo.verbose_extend extender, self
end

Instance Method Details

#create(container: nil, cnx: Windoo.cnx, **init_data) ⇒ Object

Make a new instance on the server.

The attributes marked as required must be supplied in the keyword args. Others may be included, or may be added later. To see the required args, use the .required_attributes class method

Parameters:

  • container (Object) (defaults to: nil)

    All objects other than SoftwareTitles are contained within other objects, and created via methods within those container objects. They will pass ‘self’

  • init_data (Hasn)

    The attributes of the new item as keyword arguments. Some may be required.

Returns:

  • (Object)

    A new instance of the class, already saved to the server.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/windoo/mixins/api_collection.rb', line 103

def create(container: nil, cnx: Windoo.cnx, **init_data)
  container = Windoo::Validate.container_for_new_object(
    new_object_class: self,
    container: container
  )

  unless (required_attributes & init_data.keys) == required_attributes
    raise ArgumentError,
          "Missing one or more required attributes for #{self}: #{required_attributes.join ', '}"
  end

  # validate all init values
  json_attributes.each do |attr_name, attr_def|
    init_val = init_data[attr_name]
    if attr_def[:required]
      Windoo::Validate.not_nil(
        init_val,
        msg: "Value for #{attr_name}: must be provided"
      )
    end

    init_data[attr_name] = Windoo::Validate.json_attr init_val, attr_def: attr_def, attr_name: attr_name
  end
  # add the container if applicable
  init_data[:from_container] = container if container

  # Let other steps in the process know we are being called from #create
  init_data[:creating] = true

  # Create our instance
  obj = new(**init_data)

  # create it on the server
  obj.create_on_server cnx: cnx

  # return it
  obj
end

#delete(primary_ident, cnx: Windoo.cnx) ⇒ Object



173
174
175
176
177
178
179
180
181
182
# File 'lib/windoo/mixins/api_collection.rb', line 173

def delete(primary_ident, cnx: Windoo.cnx)
  if primary_ident.is_a? Hash
    raise ArgumentError, 'All API objects other than SoftwareTitle are deleted only by their id number'
  end

  cnx.delete("#{self::RSRC_PATH}/#{primary_ident}")
rescue Windoo::NoSuchItemError
  # wasn't there to begin with
  nil
end

#fetch(primary_ident, cnx: Windoo.cnx) ⇒ Object

Instantiate from the API directly.

Returns:

  • (Object)


146
147
148
149
150
151
152
153
154
155
# File 'lib/windoo/mixins/api_collection.rb', line 146

def fetch(primary_ident, cnx: Windoo.cnx)
  if primary_ident.is_a? Hash
    raise 'All API objects other than SoftwareTitle are fetched only by their id number'
  end

  init_data = cnx.get("#{self::RSRC_PATH}/#{primary_ident}")
  init_data[:cnx] = cnx
  init_data[:fetching] = true
  new(**init_data)
end

#instantiate_from_container(container:, **init_data) ⇒ Object

This is used by container classes to instantiate the objects they contain e.g. when when instantiating a Patch, it needs to instantiate killApps, components, and capabilites. it will do so with this method



162
163
164
165
166
167
168
169
170
# File 'lib/windoo/mixins/api_collection.rb', line 162

def instantiate_from_container(container:, **init_data)
  container = Windoo::Validate.container_for_new_object(
    new_object_class: self,
    container: container
  )
  init_data[:from_container] = container
  init_data[:cnx] = container.cnx
  new(**init_data)
end