Class: InterMine::Metadata::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/intermine/model.rb

Overview

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

:include:contact_header.rdoc

Constant Summary collapse

FLOAT_TYPES =
["Float", "Double", "float", "double"]
INT_TYPES =
["Integer", "int",  "long", "Long", "short", "Short"]
BOOL_TYPES =
["Boolean", "boolean"]
NUMERIC_TYPES =
FLOAT_TYPES | INT_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_data, service = nil) ⇒ Model

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)

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/intermine/model.rb', line 55

def initialize(model_data, service=nil) 
    result = JSON.parse(model_data)
    @model = result["model"]
    @is_lazy = false
    @service = service
    @name = @model["name"]
    @classes = {}
    @model["classes"].each do |k, v| 
        @classes[k] = ClassDescriptor.new(v, self)
    end
    @classes.each do |name, cld| 
        cld.fields.each do |fname, fd|
            if fd.respond_to?(:referencedType)
                refCd = self.get_cd(fd.referencedType)
                fd.referencedType = refCd
            end
        end
    end

end

Instance Attribute Details

#classesObject (readonly)

The classes within this model


42
43
44
# File 'lib/intermine/model.rb', line 42

def classes
  @classes
end

#is_lazyObject

Whether this is a lazy model


36
37
38
# File 'lib/intermine/model.rb', line 36

def is_lazy
  @is_lazy
end

#nameObject (readonly)

The name of the model


39
40
41
# File 'lib/intermine/model.rb', line 39

def name
  @name
end

#serviceObject (readonly)

The Service this model belongs to


45
46
47
# File 'lib/intermine/model.rb', line 45

def service
  @service
end

Instance Method Details

#get_cd(cls) ⇒ Object Also known as: cd, table

call-seq:

get_cd(name) => ClassDescriptor

Get a ClassDescriptor from the model by name.

If a ClassDescriptor itself is passed as the argument, it will be passed through.


84
85
86
87
88
89
90
# File 'lib/intermine/model.rb', line 84

def get_cd(cls)
    if cls.is_a?(ClassDescriptor)
        return cls
    else
        return @classes[cls.to_s]
    end
end

#make_new(class_name = nil, opts = {}) ⇒ Object

call-seq:

make_new(name=nil, opts={}) => InterMineObject

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.

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/intermine/model.rb', line 116

def make_new(class_name=nil, opts={})
    # Support calling with just opts
    if class_name.is_a?(Hash)
        opts = class_name
        class_name = nil
    end
    if class_name && opts["class"] && (class_name != opts["class"]) && !get_cd(opts["class"]).subclass_of?(class_name)
        raise ArgumentError, "class name in options hash is not compatible with passed class name: #{opts["class"]} is not a subclass of #{class_name}"
    end
    # Prefer the options value to the passed value
    cd_name = opts["class"] || class_name
    cls = get_cd(cd_name).to_class
    obj = cls.new(opts)
    obj.send(:__cd__=, get_cd(cd_name))
    return obj
end

#resolve_path(obj, path) ⇒ Object

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


137
138
139
# File 'lib/intermine/model.rb', line 137

def resolve_path(obj, path)
    return obj._resolve(path)
end