Module: InterMine::PathQuery::AttributeConstraint

Included in:
MultiValueConstraint, SingleValueConstraint
Defined in:
lib/intermine/query.rb

Instance Method Summary collapse

Instance Method Details

#coerce_value(val) ⇒ Object



1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
# File 'lib/intermine/query.rb', line 1008

def coerce_value(val)
    nums = ["Float", "Double", "float", "double"]
    ints = ["Integer", "int"]
    bools = ["Boolean", "boolean"]
    dataType = @path.elements.last.dataType.split(".").last
    coerced = val
    if nums.include?(dataType)
        if !val.is_a?(Numeric)
            coerced = val.to_f
        end
    end
    if ints.include?(dataType)
        coerced = val.to_i
    end
    if bools.include?(dataType)
        if !val.is_a?(TrueClass) && !val.is_a?(FalseClass)
            if val == 0 or val == "0" or val.downcase == "yes" or val.downcase == "true" or val.downcase == "t"
                coerced = true
            elsif val == 1 or val == "1" or val.downcase == "no" or val.downcase == "false" or val.downcase == "f"
                coerced = false
            end
        end
    end
    if coerced == 0 and not val.to_s.start_with?("0")
       raise ArgumentError, "cannot coerce #{val} to a #{dataType}"
    end
    return coerced
end

#validateObject



1002
1003
1004
1005
1006
# File 'lib/intermine/query.rb', line 1002

def validate
    if !@path.elements.last.is_a?(InterMine::Metadata::AttributeDescriptor)
        raise ArgumentError, "Attribute constraints must be on attributes, got #{@path}"
    end
end

#validate_value(val) ⇒ Object



1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'lib/intermine/query.rb', line 1037

def validate_value(val)
    nums = ["Float", "Double", "float", "double"]
    ints = ["Integer", "int"]
    bools = ["Boolean", "boolean"]
    dataType = @path.elements.last.dataType.split(".").last
    if nums.include?(dataType)
        if !val.is_a?(Numeric)
            raise ArgumentError, "value #{val} is not numeric for #{@path}"
        end
    end
    if ints.include?(dataType)
        val = val.to_i
        if !val.is_a?(Integer)
            raise ArgumentError, "value #{val} is not an integer for #{@path}"
        end
    end
    if bools.include?(dataType)
        if !val.is_a?(TrueClass) && !val.is_a?(FalseClass)
            raise ArgumentError, "value #{val} is not a boolean value for #{@path}"
        end
    end
end