1 class OrvosBase < ActiveRecord::Base
2 self.abstract_class = true
3 attr_accessor :attribute_sortkey
5 def self.uuid_infix_object_kind
6 @@uuid_infix_object_kind ||= {
7 '4zz18' => 'orvos#collection',
8 'tpzed' => 'orvos#user',
9 'ozdt8' => 'orvos#api_client',
10 '57u5n' => 'orvos#log',
11 'j58dm' => 'orvos#specimen',
12 'mxsvm' => 'orvos#pipeline',
13 'uo14g' => 'orvos#pipeline_invocation',
14 'ldvyl' => 'orvos#project'
20 @attribute_sortkey ||= {
24 'created_at' => '002',
25 'modified_at' => '003',
26 'modified_by_user' => '004',
27 'modified_by_client' => '005',
33 'updated_at' => 'zzz-999'
38 return @columns unless @columns.nil?
40 return @columns if $orvos_api_client.orvos_schema[self.to_s.to_sym].nil?
41 $orvos_api_client.orvos_schema[self.to_s.to_sym].each do |coldef|
42 k = coldef[:name].to_sym
43 if coldef[:type] == coldef[:type].downcase
44 @columns << column(k, coldef[:type].to_sym)
46 @columns << column(k, :text)
47 serialize k, coldef[:type].constantize
55 def self.column(name, sql_type = nil, default = nil, null = true)
56 ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
59 if uuid.class != String or uuid.length < 27 then
60 raise 'argument to find() must be a uuid string. Acceptable formats: warehouse locator or string with format xxxxx-xxxxx-xxxxxxxxxxxxxxx'
62 new.private_reload(uuid)
65 OrvosResourceList.new(self).where(*args)
68 OrvosResourceList.new(self).limit(*args)
71 OrvosResourceList.new(self).eager(*args)
74 OrvosResourceList.new(self).all(*args)
78 self.class.columns.each do |col|
79 obdata[col.name.to_sym] = self.send(col.name.to_sym)
82 postdata = { self.class.to_s.underscore => obdata }
84 postdata['_method'] = 'PUT'
86 resp = $orvos_api_client.api(self.class, '/' + uuid, postdata)
88 resp = $orvos_api_client.api(self.class, '', postdata)
90 return false if !resp[:etag] || !resp[:uuid]
92 # set read-only non-database attributes
96 # these attrs can be modified by "save" -- we should update our copies
97 %w(uuid owner created_at
98 modified_at modified_by_user modified_by_client
100 self.send(attr + '=', resp[attr.to_sym])
106 self.save or raise Exception.new("Save failed")
110 o.merge!(args.pop) if args[-1].is_a? Hash
111 o[:link_class] ||= args.shift
112 o[:name] ||= args.shift
113 o[:head_kind] ||= args.shift
114 o[:tail_kind] = self.kind
115 o[:tail_uuid] = self.uuid
117 return all_links.select do |m|
122 if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
130 @links = $orvos_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
131 @links = $orvos_api_client.unpack_api_response(@links)
134 return @all_links if @all_links
135 res = $orvos_api_client.api Link, '', {
138 tail_kind: self.kind,
143 @all_links = $orvos_api_client.unpack_api_response(res)
146 private_reload(self.uuid)
148 def private_reload(uuid_or_hash)
149 raise "No such object" if !uuid_or_hash
150 if uuid_or_hash.is_a? Hash
153 hash = $orvos_api_client.api(self.class, '/' + uuid_or_hash)
156 if self.respond_to?(k.to_s + '=')
157 self.send(k.to_s + '=', v)
159 # When OrvosApiClient#schema starts telling us what to expect
160 # in API responses (not just the server side database
161 # columns), this sort of awfulness can be avoided:
162 self.instance_variable_set('@' + k.to_s, v)
163 if !self.respond_to? k
164 singleton = class << self; self end
165 singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }
176 def attributes_for_display
177 self.attributes.reject { |k,v|
178 attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
180 attribute_sortkey[k] or k
184 def self.resource_class_for_uuid(uuid, opts={})
185 if uuid.is_a? OrvosBase
188 unless uuid.is_a? String
191 if opts[:class].is_a? Class
194 if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
198 uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
199 resource_class ||= $orvos_api_client.
200 kind_class(self.uuid_infix_object_kind[re[1]])
202 if opts[:referring_object] and
203 opts[:referring_attr] and
204 opts[:referring_attr].match /_uuid$/
205 resource_class ||= $orvos_api_client.
206 kind_class(opts[:referring_object].
207 attributes[opts[:referring_attr].
208 sub(/_uuid$/, '_kind')])