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



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

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



1024
1025
1026
1027
1028
# File 'lib/intermine/query.rb', line 1024

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



1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
# File 'lib/intermine/query.rb', line 1059

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