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.



726
727
728
729
730
731
732
# File 'lib/intermine/model.rb', line 726

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.



709
710
711
# File 'lib/intermine/model.rb', line 709

def elements
  @elements
end

#modelObject (readonly)

The data model that this path describes.



706
707
708
# File 'lib/intermine/model.rb', line 706

def model
  @model
end

#rootClassObject (readonly)

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



715
716
717
# File 'lib/intermine/model.rb', line 715

def rootClass
  @rootClass
end

#subclassesObject (readonly)

The subclass information used to create this path.



712
713
714
# File 'lib/intermine/model.rb', line 712

def subclasses
  @subclasses
end

Instance Method Details

#==(other) ⇒ Object

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



781
782
783
# File 'lib/intermine/model.rb', line 781

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



764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
# File 'lib/intermine/model.rb', line 764

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”



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

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)


801
802
803
# File 'lib/intermine/model.rb', line 801

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)


806
807
808
# File 'lib/intermine/model.rb', line 806

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

#is_collection?Boolean

Return true if the last element is a collection

Returns:

  • (Boolean)


816
817
818
# File 'lib/intermine/model.rb', line 816

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

#is_reference?Boolean

Return true if the last element is a reference.

Returns:

  • (Boolean)


811
812
813
# File 'lib/intermine/model.rb', line 811

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

#lengthObject

Get the number of elements in the path



786
787
788
# File 'lib/intermine/model.rb', line 786

def length
    return @elements.length
end

#to_headless_sObject

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



796
797
798
# File 'lib/intermine/model.rb', line 796

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”



791
792
793
# File 'lib/intermine/model.rb', line 791

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