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


1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
# File 'lib/intermine/query.rb', line 1122

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


1116
1117
1118
1119
1120
# File 'lib/intermine/query.rb', line 1116

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


1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
# File 'lib/intermine/query.rb', line 1151

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