lib/dyna_model/table.rb in dyna_model-0.0.8 vs lib/dyna_model/table.rb in dyna_model-0.0.9

- old
+ new

@@ -153,15 +153,15 @@ if @schema_loaded_from_dynamo[:table][:global_secondary_indexes] && (@schema_loaded_from_dynamo[:table][:global_secondary_indexes].dup.collect {|i| i.delete_if{|k, v| index_keys_to_reject.include?(k) }; i }.sort_by { |gsi| gsi[:index_name] } != @table_schema[:global_secondary_indexes].sort_by { |gsi| gsi[:index_name] }) raise ArgumentError, "It appears your global secondary indexes have changed from the table definition. Rebuilding the table is necessary." end if @schema_loaded_from_dynamo[:table][:provisioned_throughput][:read_capacity_units] != @table_schema[:provisioned_throughput][:read_capacity_units] - Toy::Dynamo::Config.logger.error "read_capacity_units mismatch. Need to update table?" + DynaModel::Config.logger.error "read_capacity_units mismatch. Need to update table?" end if @schema_loaded_from_dynamo[:table][:provisioned_throughput][:write_capacity_units] != @table_schema[:provisioned_throughput][:write_capacity_units] - Toy::Dynamo::Config.logger.error "write_capacity_units mismatch. Need to update table?" + DynaModel::Config.logger.error "write_capacity_units mismatch. Need to update table?" end end def hash_key_item_param(value) hash_key = @table_schema[:key_schema].find{|h| h[:key_type] == "HASH"}[:attribute_name] @@ -511,34 +511,41 @@ def attr_with_condition(attr_conditional) raise ArgumentError, "Expected a 2 element Hash for each :query_filter (ex {:age.gt => 13})" unless attr_conditional.is_a?(Hash) && attr_conditional.keys.size == 1 && attr_conditional.keys.first.is_a?(String) attr_name, comparison_operator = attr_conditional.keys.first.split(".") raise ArgumentError, "Comparison operator must be one of (#{(COMPARISON_OPERATOR.keys - COMPARISON_OPERATOR_SCAN_ONLY).join(", ")})" unless COMPARISON_OPERATOR.keys.include?(comparison_operator.to_sym) attr_key = @model.attributes[attr_name] + attr_class = attr_key.class raise ArgumentError, "#{attr_name} not a valid attribute" unless attr_key attr_type = @model.attribute_type_indicator(attr_key) raise ArgumentError, "#{attr_name} key must be a Range if using the operator BETWEEN" if comparison_operator == "between" && !attr_conditional.values.first.is_a?(Range) raise ArgumentError, ":query_filter value must be an Array if using the operator IN" if comparison_operator == "in" && !attr_conditional.values.first.is_a?(Array) attr_value = attr_conditional.values.first attribute_value_list = [] if comparison_operator == "in" - attr_conditional.values.first.each do |in_v| - attribute_value_list << { attr_type => in_v.to_s } + attr_value.each do |in_v| + attribute_value_list << { attr_type => casted_attr_value(attr_class, in_v).to_s } end elsif comparison_operator == "between" attribute_value_list << { attr_type => attr_value.min.to_s } attribute_value_list << { attr_type => attr_value.max.to_s } else - attribute_value_list = [{ attr_type => attr_value.to_s }] + attribute_value_list = [{ attr_type => casted_attr_value(attr_class, attr_value).to_s }] end attribute_comparison_hash = { comparison_operator: COMPARISON_OPERATOR[comparison_operator.to_sym] } attribute_comparison_hash.merge!(attribute_value_list: attribute_value_list) unless %w(null not_null).include?(comparison_operator) { attr_name => attribute_comparison_hash } + end + + def casted_attr_value(attr_class, val) + casted = attr_class.type_cast(val) + return nil if casted.nil? + attr_class.serialize(casted) end end end