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 new.private_reload(uuid)
29 OrvosResourceList.new(self).where(*args)
32 OrvosResourceList.new(self).eager(*args)
35 OrvosResourceList.new(self).all(*args)
39 self.class.columns.each do |col|
40 obdata[col.name.to_sym] = self.send(col.name.to_sym)
44 postdata = { self.class.to_s.underscore => obdata }
46 postdata['_method'] = 'PUT'
47 resp = $orvos_api_client.api(self.class, '/' + uuid, postdata)
49 resp = $orvos_api_client.api(self.class, '', postdata)
51 return false if !resp[:etag] || !resp[:uuid]
54 self.uuid ||= resp[:uuid]
58 self.save or raise Exception.new("Save failed")
62 o.merge!(args.pop) if args[-1].is_a? Hash
63 o[:link_class] ||= args.shift
64 o[:name] ||= args.shift
65 o[:head_kind] ||= args.shift
66 o[:tail_kind] = self.kind
67 o[:tail_uuid] = self.uuid
69 return all_links.select do |m|
74 if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
82 @links = $orvos_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
83 @links = $orvos_api_client.unpack_api_response(@links)
86 return @all_links if @all_links
87 res = $orvos_api_client.api Link, '', {
95 @all_links = $orvos_api_client.unpack_api_response(res)
98 private_reload(self.uuid)
100 def private_reload(uuid_or_hash)
101 raise "No such object" if !uuid_or_hash
102 if uuid_or_hash.is_a? Hash
105 hash = $orvos_api_client.api(self.class, '/' + uuid_or_hash)
108 if self.respond_to?(k.to_s + '=')
109 self.send(k.to_s + '=', v)
111 # When OrvosApiClient#schema starts telling us what to expect
112 # in API responses (not just the server side database
113 # columns), this sort of awfulness can be avoided:
114 self.instance_variable_set('@' + k.to_s, v)
115 if !self.respond_to? k
116 singleton = class << self; self end
117 singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }