class InterMine::Metadata::InterMineObject
A base class for all objects instantiated from a ClassDescriptor
¶ ↑
This class described the common behaviour for all objects instantiated as representations of classes defined by a ClassDescriptor
. It is not intended to be instantiated directly, but inherited from.
- author
-
Alex Kalderimis dev@intermine.org
- homepage
- Licence
-
Copyright (C) 2002-2011 FlyMine
This code may be freely distributed and modified under the terms of the GNU Lesser General Public Licence. This should be distributed with the code. See the LICENSE file for more information or www.gnu.org/copyleft/lesser.html.
Attributes
The ClassDescriptor
for this object
The database internal id in the originating mine. Serves as a guarantor of object identity.
Public Class Methods
Arguments:
- hash
-
The properties of this object represented as a Hash. Nested Arrays and Hashes are expected for collections and references.
# File lib/intermine/model.rb 171 def initialize(hash=nil) 172 hash ||= {} 173 hash.each do |key, value| 174 if key.to_s != "class" 175 self.send(key.to_s + "=", value) 176 end 177 end 178 end
Public Instance Methods
Alias property fetches as item retrieval, so the following are equivalent:
organism = gene.organism organism = gene["organism"]
# File lib/intermine/model.rb 217 def [](key) 218 if @__cd__.has_field?(key) 219 return self.send(key) 220 end 221 raise IndexError, "No field #{key} found for #{@__cd__.name}" 222 end
Resolve a path represented as a String
or as a Path
into a value
This is designed to automate access to values in deeply nested objects. So:
name = gene._resolve('Gene.organism.name')
Array
indices are supported:
symbol = gene._resolve('Gene.alleles[3].symbol')
# File lib/intermine/model.rb 237 def _resolve(path) 238 begin 239 parts = path.split(/(?:\.|\[|\])/).reject {|x| x.empty?} 240 rescue NoMethodError 241 parts = path.elements.map { |x| x.name } 242 end 243 root = parts.shift 244 if !is_a?(root) 245 raise ArgumentError, "Incompatible path '#{path}': #{self} is not a #{root}" 246 end 247 begin 248 res = parts.inject(self) do |memo, part| 249 part = part.to_i if (memo.is_a?(Array) and part.to_i.to_s == part) 250 begin 251 new = memo[part] 252 rescue TypeError 253 raise ArgumentError, "Incompatible path '#{path}' for #{self}, expected an index" 254 end 255 new 256 end 257 rescue IndexError => e 258 raise ArgumentError, "Incompatible path '#{path}' for #{self}, #{e}" 259 end 260 return res 261 end
Determine if this class is a subclass of other.
Overridden to provide support for querying against ClassDescriptors and Strings.
# File lib/intermine/model.rb 187 def is_a?(other) 188 if other.is_a?(ClassDescriptor) 189 return is_a?(other.to_module) 190 elsif other.is_a?(String) 191 return is_a?(@__cd__.model.cd(other)) 192 else 193 return super 194 end 195 end
Serialise to a readable representation
# File lib/intermine/model.rb 201 def to_s 202 parts = [@__cd__.name + ':' + self.objectId.to_s] 203 self.instance_variables.reject{|var| var.to_s.end_with?("objectId")}.each do |var| 204 parts << "#{var}=#{self.instance_variable_get(var).inspect}" 205 end 206 return "<#{parts.join(' ')}>" 207 end
Private Instance Methods
# File lib/intermine/model.rb 267 def __cd__=(cld) 268 @__cd__ = cld 269 end
# File lib/intermine/model.rb 271 def objectId=(val) 272 @objectId = val 273 end