Skip to content

Commit 9153062

Browse files
pimpinclaude
andcommitted
Fix type casting and precision issues
- Remove double casting in create_setters to avoid type inconsistencies - Fix Timestamp type precision by applying floor() during cast - Fix DateTimeWith3Decimal precision handling in both cast and serialize 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent b4a6280 commit 9153062

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

lib/couchbase-orm/changeable.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,8 @@ def create_dirty_methods(name, meth)
366366

367367
def create_setters(name)
368368
define_method("#{name}=") do |new_attribute_value|
369-
type = self.class.attribute_types[name.to_s]
370-
casted_value = type.cast new_attribute_value
371369
previous_value = attributes[name.to_s]
372-
ret = super(casted_value)
370+
ret = super(new_attribute_value)
373371
if previous_value != attributes[name.to_s]
374372
changed_attributes.merge!(Hash[name, [previous_value, attributes[name.to_s]]])
375373
end

lib/couchbase-orm/types/timestamp.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ module Types
33
class Timestamp < ActiveModel::Type::DateTime
44
def cast(value)
55
return nil if value.nil?
6-
return Time.at(value) if value.is_a?(Integer) || value.is_a?(Float)
7-
return Time.at(value.to_i) if value.is_a?(String) && value =~ /^[0-9]+$/
8-
return value.utc if value.is_a?(Time)
9-
super(value).utc
6+
return Time.at(value).floor if value.is_a?(Integer) || value.is_a?(Float)
7+
return Time.at(value.to_i).floor if value.is_a?(String) && value =~ /^[0-9]+$/
8+
return value.utc.floor if value.is_a?(Time)
9+
super(value).utc.floor
1010
end
1111

1212
def serialize(value)

spec/type_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
require "couchbase-orm/types"
55

66
class DateTimeWith3Decimal < CouchbaseOrm::Types::DateTime
7+
def cast(value)
8+
result = super(value)
9+
result&.floor(3)
10+
end
11+
712
def serialize(value)
813
value&.iso8601(3)
914
end

0 commit comments

Comments
 (0)