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 '8i9sb' => 'orvos#job',
11 'o0j2j' => 'orvos#link',
12 '57u5n' => 'orvos#log',
13 'j58dm' => 'orvos#specimen',
14 'p5p6p' => 'orvos#pipeline_template',
15 'mxsvm' => 'orvos#pipeline_template', # legacy Pipeline objects
16 'd1hrv' => 'orvos#pipeline_instance',
17 'uo14g' => 'orvos#pipeline_instance', # legacy PipelineInstance objects
18 'j7d0g' => 'orvos#group',
19 'ldvyl' => 'orvos#group' # only needed for legacy Project objects
25 @attribute_sortkey ||= {
29 'created_at' => '002',
30 'modified_at' => '003',
31 'modified_by_user' => '004',
32 'modified_by_client' => '005',
39 'updated_at' => 'zzz-999'
44 return @columns unless @columns.nil?
46 return @columns if $orvos_api_client.orvos_schema[self.to_s.to_sym].nil?
47 $orvos_api_client.orvos_schema[self.to_s.to_sym].each do |coldef|
48 k = coldef[:name].to_sym
49 if coldef[:type] == coldef[:type].downcase
50 @columns << column(k, coldef[:type].to_sym)
52 @columns << column(k, :text)
53 serialize k, coldef[:type].constantize
61 def self.column(name, sql_type = nil, default = nil, null = true)
62 ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
65 if uuid.class != String or uuid.length < 27 then
66 raise 'argument to find() must be a uuid string. Acceptable formats: warehouse locator or string with format xxxxx-xxxxx-xxxxxxxxxxxxxxx'
68 new.private_reload(uuid)
71 OrvosResourceList.new(self).where(*args)
74 OrvosResourceList.new(self).limit(*args)
77 OrvosResourceList.new(self).eager(*args)
80 OrvosResourceList.new(self).all(*args)
84 self.class.columns.each do |col|
85 obdata[col.name.to_sym] = self.send(col.name.to_sym)
88 postdata = { self.class.to_s.underscore => obdata }
90 postdata['_method'] = 'PUT'
92 resp = $orvos_api_client.api(self.class, '/' + uuid, postdata)
94 resp = $orvos_api_client.api(self.class, '', postdata)
96 return false if !resp[:etag] || !resp[:uuid]
98 # set read-only non-database attributes
102 # these attrs can be modified by "save" -- we should update our copies
103 %w(uuid owner created_at
104 modified_at modified_by_user modified_by_client
106 self.send(attr + '=', resp[attr.to_sym])
112 self.save or raise Exception.new("Save failed")
116 o.merge!(args.pop) if args[-1].is_a? Hash
117 o[:link_class] ||= args.shift
118 o[:name] ||= args.shift
119 o[:head_kind] ||= args.shift
120 o[:tail_kind] = self.kind
121 o[:tail_uuid] = self.uuid
123 return all_links.select do |m|
128 if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
136 @links = $orvos_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
137 @links = $orvos_api_client.unpack_api_response(@links)
140 return @all_links if @all_links
141 res = $orvos_api_client.api Link, '', {
144 tail_kind: self.kind,
149 @all_links = $orvos_api_client.unpack_api_response(res)
152 private_reload(self.uuid)
154 def private_reload(uuid_or_hash)
155 raise "No such object" if !uuid_or_hash
156 if uuid_or_hash.is_a? Hash
159 hash = $orvos_api_client.api(self.class, '/' + uuid_or_hash)
162 if self.respond_to?(k.to_s + '=')
163 self.send(k.to_s + '=', v)
165 # When OrvosApiClient#schema starts telling us what to expect
166 # in API responses (not just the server side database
167 # columns), this sort of awfulness can be avoided:
168 self.instance_variable_set('@' + k.to_s, v)
169 if !self.respond_to? k
170 singleton = class << self; self end
171 singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }
182 def attributes_for_display
183 self.attributes.reject { |k,v|
184 attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
186 attribute_sortkey[k] or k
190 def self.resource_class_for_uuid(uuid, opts={})
191 if uuid.is_a? OrvosBase
194 unless uuid.is_a? String
197 if opts[:class].is_a? Class
200 if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
204 uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
205 resource_class ||= $orvos_api_client.
206 kind_class(self.uuid_infix_object_kind[re[1]])
208 if opts[:referring_object] and
209 opts[:referring_attr] and
210 opts[:referring_attr].match /_uuid$/
211 resource_class ||= $orvos_api_client.
212 kind_class(opts[:referring_object].
213 attributes[opts[:referring_attr].
214 sub(/_uuid$/, '_kind')])