1 class ArvadosBase < ActiveRecord::Base
2 self.abstract_class = true
3 attr_accessor :attribute_sortkey
5 def self.uuid_infix_object_kind
6 @@uuid_infix_object_kind ||=
9 $arvados_api_client.discovery[:schemas].each do |name, schema|
10 if schema[:uuidPrefix]
11 infix_kind[schema[:uuidPrefix]] =
12 'arvados#' + name.to_s.camelcase(:lower)
16 # Recognize obsolete types.
18 merge('mxsvm' => 'arvados#pipelineTemplate', # Pipeline
19 'uo14g' => 'arvados#pipelineInstance', # PipelineInvocation
20 'ldvyl' => 'arvados#group') # Project
26 @attribute_sortkey ||= {
29 'owner_uuid' => '001',
30 'created_at' => '002',
31 'modified_at' => '003',
32 'modified_by_user_uuid' => '004',
33 'modified_by_client_uuid' => '005',
40 'updated_at' => 'zzz-999'
45 return @columns unless @columns.nil?
47 @attribute_info ||= {}
48 return @columns if $arvados_api_client.arvados_schema[self.to_s.to_sym].nil?
49 $arvados_api_client.arvados_schema[self.to_s.to_sym].each do |coldef|
50 k = coldef[:name].to_sym
51 if coldef[:type] == coldef[:type].downcase
52 @columns << column(k, coldef[:type].to_sym)
54 @columns << column(k, :text)
55 serialize k, coldef[:type].constantize
58 @attribute_info[k] = coldef
64 def self.column(name, sql_type = nil, default = nil, null = true)
65 ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
67 def self.attribute_info
72 if uuid.class != String or uuid.length < 27 then
73 raise 'argument to find() must be a uuid string. Acceptable formats: warehouse locator or string with format xxxxx-xxxxx-xxxxxxxxxxxxxxx'
75 new.private_reload(uuid)
78 ArvadosResourceList.new(self).order(*args)
81 ArvadosResourceList.new(self).where(*args)
84 ArvadosResourceList.new(self).limit(*args)
87 ArvadosResourceList.new(self).eager(*args)
90 ArvadosResourceList.new(self).all(*args)
94 self.class.columns.each do |col|
95 obdata[col.name.to_sym] = self.send(col.name.to_sym)
98 postdata = { self.class.to_s.underscore => obdata }
100 postdata['_method'] = 'PUT'
102 resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
104 resp = $arvados_api_client.api(self.class, '', postdata)
106 return false if !resp[:etag] || !resp[:uuid]
108 # set read-only non-database attributes
112 # these attrs can be modified by "save" -- we should update our copies
113 %w(uuid owner_uuid created_at
114 modified_at modified_by_user_uuid modified_by_client_uuid
116 if self.respond_to? "#{attr}=".to_sym
117 self.send(attr + '=', resp[attr.to_sym])
124 self.save or raise Exception.new("Save failed")
129 postdata = { '_method' => 'DELETE' }
130 resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
131 resp[:etag] && resp[:uuid] && resp
139 o.merge!(args.pop) if args[-1].is_a? Hash
140 o[:link_class] ||= args.shift
141 o[:name] ||= args.shift
142 o[:head_kind] ||= args.shift
143 o[:tail_kind] = self.kind
144 o[:tail_uuid] = self.uuid
146 return all_links.select do |m|
151 if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
159 @links = $arvados_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
160 @links = $arvados_api_client.unpack_api_response(@links)
163 return @all_links if @all_links
164 res = $arvados_api_client.api Link, '', {
167 tail_kind: self.kind,
172 @all_links = $arvados_api_client.unpack_api_response(res)
175 private_reload(self.uuid)
177 def private_reload(uuid_or_hash)
178 raise "No such object" if !uuid_or_hash
179 if uuid_or_hash.is_a? Hash
182 hash = $arvados_api_client.api(self.class, '/' + uuid_or_hash)
185 if self.respond_to?(k.to_s + '=')
186 self.send(k.to_s + '=', v)
188 # When ArvadosApiClient#schema starts telling us what to expect
189 # in API responses (not just the server side database
190 # columns), this sort of awfulness can be avoided:
191 self.instance_variable_set('@' + k.to_s, v)
192 if !self.respond_to? k
193 singleton = class << self; self end
194 singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }
205 def attributes_for_display
206 self.attributes.reject { |k,v|
207 attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
209 attribute_sortkey[k] or k
218 (current_user and current_user.is_active and
219 (current_user.is_admin or
220 current_user.uuid == self.owner_uuid))
223 def attribute_editable?(attr)
224 if "created_at modified_at modified_by_user_uuid modified_by_client_uuid updated_at".index(attr.to_s)
226 elsif not (current_user.andand.is_active)
228 elsif "uuid owner_uuid".index(attr.to_s) or current_user.is_admin
229 current_user.is_admin
231 current_user.uuid == self.owner_uuid or current_user.uuid == self.uuid
235 def self.resource_class_for_uuid(uuid, opts={})
236 if uuid.is_a? ArvadosBase
239 unless uuid.is_a? String
242 if opts[:class].is_a? Class
245 if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
249 uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
250 resource_class ||= $arvados_api_client.
251 kind_class(self.uuid_infix_object_kind[re[1]])
253 if opts[:referring_object] and
254 opts[:referring_attr] and
255 opts[:referring_attr].match /_uuid$/
256 resource_class ||= $arvados_api_client.
257 kind_class(opts[:referring_object].
258 attributes[opts[:referring_attr].
259 sub(/_uuid$/, '_kind')])
264 def friendly_link_name
265 (name if self.respond_to? :name) || uuid
276 def self.current_user
277 Thread.current[:user] ||= User.current if Thread.current[:arvados_api_token]
278 Thread.current[:user]
281 self.class.current_user