class InterMine::Metadata::Model
Description¶ ↑
A representation of the data model of an InterMine
data warehouse. This class contains access to all aspects of the model, including the tables of data stored, and the kinds of data in those tables. It is also the mechanism for creating objects which are representations of data within the data model, including records, paths and columns.
model = Model.new(data) model.classes.each do |c| puts "#{c.name} has #{c.fields.size} fields" end
- 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.
Constants
- BOOL_TYPES
- FLOAT_TYPES
- INT_TYPES
- NUMERIC_TYPES
Attributes
The classes within this model
Whether this is a lazy model
The name of the model
The Service
this model belongs to
Public Class Methods
Construct a new model from its textual json representation
Arguments:
model_data
-
The JSON serialization of the model
service
-
The
Service
this model belongs to
model = Model.new(json)
# File lib/intermine/model.rb 55 def initialize(model_data, service=nil) 56 result = JSON.parse(model_data) 57 @model = result["model"] 58 @is_lazy = false 59 @service = service 60 @name = @model["name"] 61 @classes = {} 62 @model["classes"].each do |k, v| 63 @classes[k] = ClassDescriptor.new(v, self) 64 end 65 @classes.each do |name, cld| 66 cld.fields.each do |fname, fd| 67 if fd.respond_to?(:referencedType) 68 refCd = self.get_cd(fd.referencedType) 69 fd.referencedType = refCd 70 end 71 end 72 end 73 74 end
Public Instance Methods
Get a ClassDescriptor
from the model by name.
If a ClassDescriptor
itself is passed as the argument, it will be passed through.
# File lib/intermine/model.rb 84 def get_cd(cls) 85 if cls.is_a?(ClassDescriptor) 86 return cls 87 else 88 return @classes[cls.to_s] 89 end 90 end
Make a new InterMineObject
which is an instantiation of a class a ClassDescriptor
represents
Arguments:
name
-
The name of the class to instantiate
opts
-
The values to assign to the new object
gene = model.make_new 'Gene', { "symbol" => "zen", "name" => "zerknullt", "organism => { "shortName" => "D. melanogaster", "taxonId" => 7217 } } puts gene.organism.taxonId 7217.
# File lib/intermine/model.rb 116 def make_new(class_name=nil, opts={}) 117 # Support calling with just opts 118 if class_name.is_a?(Hash) 119 opts = class_name 120 class_name = nil 121 end 122 if class_name && opts["class"] && (class_name != opts["class"]) && !get_cd(opts["class"]).subclass_of?(class_name) 123 raise ArgumentError, "class name in options hash is not compatible with passed class name: #{opts["class"]} is not a subclass of #{class_name}" 124 end 125 # Prefer the options value to the passed value 126 cd_name = opts["class"] || class_name 127 cls = get_cd(cd_name).to_class 128 obj = cls.new(opts) 129 obj.send(:__cd__=, get_cd(cd_name)) 130 return obj 131 end
Resolve the value referred to by a path on an object¶ ↑
The path may be either a string such as “Department.employees.name”, or a Path
object
# File lib/intermine/model.rb 137 def resolve_path(obj, path) 138 return obj._resolve(path) 139 end
Private Instance Methods
For mocking in tests
# File lib/intermine/model.rb 144 def set_service(service) 145 @service = service 146 end