1 class OrvosBase < ActiveRecord::Base
2 self.abstract_class = true
5 return @columns unless @columns.nil?
7 return @columns if $orvos_api_client.orvos_schema[self.to_s.to_sym].nil?
8 $orvos_api_client.orvos_schema[self.to_s.to_sym].each do |coldef|
9 k = coldef[:name].to_sym
10 if coldef[:type] == coldef[:type].downcase
11 @columns << column(k, coldef[:type].to_sym)
13 @columns << column(k, :text)
14 serialize k, coldef[:type].constantize
22 def self.column(name, sql_type = nil, default = nil, null = true)
23 ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
26 if uuid.class != String or uuid.length < 27 then
27 raise 'argument to find() must be a uuid string. Acceptable formats: warehouse locator or string with format xxxxx-xxxxx-xxxxxxxxxxxxxxx'
29 new.private_reload(uuid)
32 OrvosResourceList.new(self).where(*args)
35 OrvosResourceList.new(self).limit(*args)
38 OrvosResourceList.new(self).eager(*args)
41 OrvosResourceList.new(self).all(*args)
45 self.class.columns.each do |col|
46 obdata[col.name.to_sym] = self.send(col.name.to_sym)
50 postdata = { self.class.to_s.underscore => obdata }
52 postdata['_method'] = 'PUT'
53 resp = $orvos_api_client.api(self.class, '/' + uuid, postdata)
55 resp = $orvos_api_client.api(self.class, '', postdata)
57 return false if !resp[:etag] || !resp[:uuid]
60 self.uuid ||= resp[:uuid]
64 self.save or raise Exception.new("Save failed")
68 o.merge!(args.pop) if args[-1].is_a? Hash
69 o[:link_class] ||= args.shift
70 o[:name] ||= args.shift
71 o[:head_kind] ||= args.shift
72 o[:tail_kind] = self.kind
73 o[:tail_uuid] = self.uuid
75 return all_links.select do |m|
80 if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
88 @links = $orvos_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
89 @links = $orvos_api_client.unpack_api_response(@links)
92 return @all_links if @all_links
93 res = $orvos_api_client.api Link, '', {
101 @all_links = $orvos_api_client.unpack_api_response(res)
104 private_reload(self.uuid)
106 def private_reload(uuid_or_hash)
107 raise "No such object" if !uuid_or_hash
108 if uuid_or_hash.is_a? Hash
111 hash = $orvos_api_client.api(self.class, '/' + uuid_or_hash)
114 if self.respond_to?(k.to_s + '=')
115 self.send(k.to_s + '=', v)
117 # When OrvosApiClient#schema starts telling us what to expect
118 # in API responses (not just the server side database
119 # columns), this sort of awfulness can be avoided:
120 self.instance_variable_set('@' + k.to_s, v)
121 if !self.respond_to? k
122 singleton = class << self; self end
123 singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }