Class: InterMine::PathQuery::ConstraintFactory

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

Direct Known Subclasses

TemplateConstraintFactory

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ ConstraintFactory

Returns a new instance of ConstraintFactory.



946
947
948
949
950
951
952
953
954
# File 'lib/intermine/query.rb', line 946

def initialize(query)
    @classes = [
        SingleValueConstraint, 
        SubClassConstraint, 
        LookupConstraint, MultiValueConstraint, RangeConstraint,
        UnaryConstraint, LoopConstraint, ListConstraint]

    @query = query
end

Instance Method Details

#make_constraint(args) ⇒ Object



956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
# File 'lib/intermine/query.rb', line 956

def make_constraint(args)
    case args.length 
    when 2
        parameters = {:path => args[0], :op => args[1]}
    when 3
        if args[2].is_a?(Array)
            parameters = {:path => args[0], :op => args[1], :values => args[2]}
        elsif LoopConstraint.valid_ops.include?(args[1])
            parameters = {:path => args[0], :op => args[1], :loopPath => args[2]}
        else
            parameters = {:path => args[0], :op => args[1], :value => args[2]}
        end
    when 4
        parameters = {:path => args[0], :op => args[1], :value => args[2], :extra_value => args[3]}
    else
        parameters = args.first
    end

    attr_keys = parameters.keys
    suitable_classes = @classes.select { |cls| 
        attr_keys.reduce(true) do |suitable, k|
            suitable && cls.method_defined?(k) && (k.to_s != "op" or cls.valid_ops.include?(parameters[k]))
        end
    }
    if suitable_classes.size > 1
        raise ArgumentError, "More than one class found for #{parameters.inspect}"
    elsif suitable_classes.size < 1
        raise ArgumentError, "No suitable classes found for #{parameters.inspect}"
    end

    cls = suitable_classes.first
    con = cls.new
    parameters.each_pair { |key, value|
        if key == :path || key == :loopPath
            value = @query.path(value)
        end
        if key == :sub_class
            value = InterMine::Metadata::Path.new(value, @query.model)
        end
        con.send(key.to_s + '=', value)
    }
    con.validate
    if con.respond_to?(:code)
        code = con.code
        if code.nil?
            con.code = @query.next_code
        else
            code = code.to_s
            unless Query.is_valid_code(code)
                raise ArgumentError, "Coded must be between A and Z, got: #{code}"
            end
            if @query.used_codes.include?(code)
                con.code = @query.next_code
            end
        end
    end

    return con
end