Class: InterMine::Metadata::InterMineObject
- Inherits:
-
Object
- Object
- InterMine::Metadata::InterMineObject
- Defined in:
- lib/intermine/model.rb
Overview
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.
:include:contact_header.rdoc
Instance Attribute Summary collapse
-
#__cd__ ⇒ Object
readonly
The ClassDescriptor for this object.
-
#objectId ⇒ Object
readonly
The database internal id in the originating mine.
Instance Method Summary collapse
-
#[](key) ⇒ Object
call-seq: [key] => value.
-
#_resolve(path) ⇒ Object
call-seq: _resolve(path) => value.
-
#initialize(hash = nil) ⇒ InterMineObject
constructor
- Arguments: hash
-
The properties of this object represented as a Hash.
-
#is_a?(other) ⇒ Boolean
call-seq: is_a?(other) => bool.
-
#to_s ⇒ Object
(also: #inspect)
call-seq: to_s() => human-readable string.
Constructor Details
#initialize(hash = nil) ⇒ InterMineObject
Arguments:
- hash
-
The properties of this object represented as a Hash. Nested Arrays and Hashes are expected for collections and references.
171 172 173 174 175 176 177 178 |
# File 'lib/intermine/model.rb', line 171 def initialize(hash=nil) hash ||= {} hash.each do |key, value| if key.to_s != "class" self.send(key.to_s + "=", value) end end end |
Instance Attribute Details
#__cd__ ⇒ Object
The ClassDescriptor for this object
165 166 167 |
# File 'lib/intermine/model.rb', line 165 def __cd__ @__cd__ end |
#objectId ⇒ Object
The database internal id in the originating mine. Serves as a guarantor of object identity.
162 163 164 |
# File 'lib/intermine/model.rb', line 162 def objectId @objectId end |
Instance Method Details
#[](key) ⇒ Object
call-seq:
[key] => value
Alias property fetches as item retrieval, so the following are equivalent:
organism = gene.organism
organism = gene["organism"]
217 218 219 220 221 222 |
# File 'lib/intermine/model.rb', line 217 def [](key) if @__cd__.has_field?(key) return self.send(key) end raise IndexError, "No field #{key} found for #{@__cd__.name}" end |
#_resolve(path) ⇒ Object
call-seq:
_resolve(path) => value
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')
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/intermine/model.rb', line 237 def _resolve(path) begin parts = path.split(/(?:\.|\[|\])/).reject {|x| x.empty?} rescue NoMethodError parts = path.elements.map { |x| x.name } end root = parts.shift if !is_a?(root) raise ArgumentError, "Incompatible path '#{path}': #{self} is not a #{root}" end begin res = parts.inject(self) do |memo, part| part = part.to_i if (memo.is_a?(Array) and part.to_i.to_s == part) begin new = memo[part] rescue TypeError raise ArgumentError, "Incompatible path '#{path}' for #{self}, expected an index" end new end rescue IndexError => e raise ArgumentError, "Incompatible path '#{path}' for #{self}, #{e}" end return res end |
#is_a?(other) ⇒ Boolean
call-seq:
is_a?(other) => bool
Determine if this class is a subclass of other.
Overridden to provide support for querying against ClassDescriptors and Strings.
187 188 189 190 191 192 193 194 195 |
# File 'lib/intermine/model.rb', line 187 def is_a?(other) if other.is_a?(ClassDescriptor) return is_a?(other.to_module) elsif other.is_a?(String) return is_a?(@__cd__.model.cd(other)) else return super end end |
#to_s ⇒ Object Also known as: inspect
call-seq:
to_s() => human-readable string
Serialise to a readable representation
201 202 203 204 205 206 207 |
# File 'lib/intermine/model.rb', line 201 def to_s parts = [@__cd__.name + ':' + self.objectId.to_s] self.instance_variables.reject{|var| var.to_s.end_with?("objectId")}.each do |var| parts << "#{var}=#{self.instance_variable_get(var).inspect}" end return "<#{parts.join(' ')}>" end |