lib/jamf/api/abstract_classes/collection_resource.rb in ruby-jss-1.2.4a4 vs lib/jamf/api/abstract_classes/collection_resource.rb in ruby-jss-1.2.6

- old
+ new

@@ -218,11 +218,11 @@ idents = identifiers - [:id] val_is_str = value.is_a? String idents.each do |ident| - match = all(cnx: cnx).select do |m| + match = all(refresh: refresh, cnx: cnx).select do |m| val_is_str ? m[ident].to_s.casecmp?(value) : m[ident] == value end.first return match[:id] if match end # identifiers.each do |ident| @@ -239,18 +239,25 @@ def self.creatable? true end # Make a new thing to be added to the API - def self.create(params, cnx: Jamf.cnx) - raise Jamf::UnsupportedError, "#{self}'s are not currently creatable via the API" unless self.creatable? - + def self.create(**params) validate_not_abstract + raise Jamf::UnsupportedError, "#{self}'s are not currently creatable via the API" unless creatable? + + cnx = params.delete :cnx + cnx ||= Jamf.cnx + params.delete :id # no such animal when .creating - validate_create_params(params, cnx) + params.keys.each do |param| + raise ArgumentError, "Unknown parameter: #{param}" unless self::OBJECT_MODEL.key? param + params[param] = validate_attr param, params[param], cnx: cnx + end + params[:creating_from_create] = true new params, cnx: cnx end # Retrieve a member of a CollectionResource from the API @@ -275,20 +282,21 @@ # # @return [CollectionResource] The ruby-instance of a Jamf object # def self.fetch(ident_value = nil, cnx: Jamf.cnx, **ident_hash) validate_not_abstract + id = if ident_value == :random all_ids.sample elsif ident_value - valid_id ident_value + valid_id ident_value, cnx: cnx elsif ident_hash.empty? nil else ident, lookup_value = ident_hash.first - valid_id ident => lookup_value + valid_id ident => lookup_value, cnx: cnx end raise Jamf::NoSuchItemError, "No matching #{self}" unless id data = cnx.get "#{rsrc_path}/#{id}" @@ -359,20 +367,9 @@ send list_method_name, refresh, cnx: cnx end # define_singleton_method end # each alias end # create_list_methods private_class_method :create_list_methods - - # validate that our .create data is OK - # - def self.validate_create_params(params, cnx) - params.keys.each do |param| - raise ArgumentError, "Unknown parameter: #{param}" unless self::OBJECT_MODEL.key? param - - params[param] = validate_attr param, params[param], cnx: cnx - end - end - private_class_method :validate_create_params # Given an indentier attr. key, and a value, # return the id where that ident has that value, or nil # def self.id_from_other_ident(ident, value, refresh = true, cnx: Jamf.cnx)