houdini/lib/retrieve/retrieve_active_record_items.rb
Bradley M. Kuhn 6772312ea7 Relicense all .rb files under new project license.
The primary license of the project is changing to:
  AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later

with some specific files to be licensed under the one of two licenses:
   CC0-1.0
   LGPL-3.0-or-later

This commit is one of the many steps to relicense the entire codebase.

Documentation granting permission for this relicensing (from all past
contributors who hold copyrights) is on file with Software Freedom
Conservancy, Inc.
2018-03-25 15:10:40 -04:00

45 lines
No EOL
1.5 KiB
Ruby

# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
module RetrieveActiveRecordItems
def self.retrieve(data, optional= false)
data.map{|k,v|
our_integer = Integer(v) rescue nil
unless (optional && v.nil?) || (our_integer && our_integer > 0)
raise ArgumentError.new("Value '#{v}' for Key '#{k}' is not valid")
end
unless k.is_a? Class
raise ArgumentError.new("Key '#{k.to_s}' is not a class")
end
ret = []
if optional && v.nil?
ret = [k, nil]
else
ret = [k, k.where('id = ?', our_integer).first]
if (ret[1] == nil)
raise ParamValidation::ValidationError.new("ID #{v} is not a valid #{k.to_s}", {key: k})
end
end
ret
}.to_h
end
def self.retrieve_from_keys(input, class_to_key_hash, optional=false)
class_to_key_hash.map{|k,v|
unless k.is_a? Class
raise ArgumentError.new("Key '#{k.to_s}' is not a class")
end
ret = nil
begin
item = retrieve({k => input[v]}, optional)
ret = [v, item[k]]
rescue ParamValidation::ValidationError
raise ParamValidation::ValidationError.new("ID #{input[v]} is not a valid #{k.to_s}", {key: v})
rescue ArgumentError
raise ParamValidation::ValidationError.new("#{input[v]} is not a valid ID for Key '#{v}'", {key: v})
rescue
raise ParamValidation::ValidationError.new("#{input[v]} is not a valid ID for Key '#{v}'", {key: v})
end
ret
}.to_h
end
end