Class: InterMine::Metadata::Path

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

Overview

A representation of a path through the data model, starting at a table/class, and descending ultimately to an attribute. A path represents a valid sequence of joins and column accesses according to the webservice’s database schema.

In string format, a path can be represented using dotted notation:

Gene.proteins.proteinDomains.name

Which is a valid path through the data-model, starting in the gene table, following a reference to the protein table (via x-to-many relationship) and then to the protein-domain table and then finally to the name column in the protein domain table. Joins are implicitly implied.

:include:contact_header.rdoc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pathstring, model = nil, subclasses = {}) ⇒ Path

Construct a Path

The standard mechanism is to parse a string representing a path with information about the model and the subclasses that are in force. However, it is also possible to clone a path by passing a Path through as the first element, and also to construct a path from a ClassDescriptor. In both cases the new Path will inherit the model of the object used to construct it, this avoid the need for a model in these cases.



703
704
705
706
707
708
709
# File 'lib/intermine/model.rb', line 703

def initialize(pathstring, model=nil, subclasses={})
    @model = model
    @subclasses = subclasses
    @elements = []
    @rootClass = nil
    parse(pathstring)
end

Instance Attribute Details

#elementsObject (readonly)

The objects represented by each section of the path. The first is always a ClassDescriptor.



686
687
688
# File 'lib/intermine/model.rb', line 686

def elements
  @elements
end

#modelObject (readonly)

The data model that this path describes.



683
684
685
# File 'lib/intermine/model.rb', line 683

def model
  @model
end

#rootClassObject (readonly)

The root class of this path. This is the same as the first element.



692
693
694
# File 'lib/intermine/model.rb', line 692

def rootClass
  @rootClass
end

#subclassesObject (readonly)

The subclass information used to create this path.



689
690
691
# File 'lib/intermine/model.rb', line 689

def subclasses
  @subclasses
end

Instance Method Details

#==(other) ⇒ Object

Two paths can be said to be equal when they stringify to the same representation.



758
759
760
# File 'lib/intermine/model.rb', line 758

def ==(other)
    return self.to_s == other.to_s
end

#end_cdObject

call-seq:

end_cd => ClassDescriptor

Return the last ClassDescriptor mentioned in this path. eg:

Gene

Gene

Gene.symbol

Gene

Gene.proteins

Protein

Gene.proteins.name

Protein



741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'lib/intermine/model.rb', line 741

def end_cd
    last = @elements.last
    if last.is_a?(ClassDescriptor)
        return last
    elsif last.respond_to?(:referencedType)
        return last.referencedType
    else
        penult = @elements[-2]
        if penult.is_a?(ClassDescriptor)
            return penult
        else
            return penult.referencedType
        end
    end
end

#end_typeObject

call-seq:

end_type => String

Return the string that describes the kind of thing this path represents. eg:

Gene

“Gene”

Gene.symbol

“java.lang.String”

Gene.proteins

“Protein”



720
721
722
723
724
725
726
727
728
729
# File 'lib/intermine/model.rb', line 720

def end_type
    last = @elements.last
    if last.is_a?(ClassDescriptor)
        return last.name
    elsif last.respond_to?(:referencedType)
        return last.referencedType.name
    else
        return last.dataType
    end
end

#is_attribute?Boolean

Return true if the Path ends in an attribute

Returns:

  • (Boolean)


778
779
780
# File 'lib/intermine/model.rb', line 778

def is_attribute?
    return @elements.last.is_a?(AttributeDescriptor)
end

#is_class?Boolean

Return true if the last element is a class (ie. a path of length 1)

Returns:

  • (Boolean)


783
784
785
# File 'lib/intermine/model.rb', line 783

def is_class?
    return @elements.last.is_a?(ClassDescriptor)
end

#is_collection?Boolean

Return true if the last element is a collection

Returns:

  • (Boolean)


793
794
795
# File 'lib/intermine/model.rb', line 793

def is_collection?
    return @elements.last.is_a?(CollectionDescriptor)
end

#is_reference?Boolean

Return true if the last element is a reference.

Returns:

  • (Boolean)


788
789
790
# File 'lib/intermine/model.rb', line 788

def is_reference?
    return @elements.last.is_a?(ReferenceDescriptor)
end

#lengthObject

Get the number of elements in the path



763
764
765
# File 'lib/intermine/model.rb', line 763

def length
    return @elements.length
end

#to_headless_sObject

Returns a string as to_s without the first element. eg: “proteins.name”



773
774
775
# File 'lib/intermine/model.rb', line 773

def to_headless_s
    return @elements[1, @elements.size - 1].map {|x| x.name}.join(".")
end

#to_sObject

Return the string representation of this path, eg: “Gene.proteins.name”



768
769
770
# File 'lib/intermine/model.rb', line 768

def to_s 
    return @elements.map {|x| x.name}.join(".")
end