Skip to content

Commit

Permalink
get the tests passing with ar 2.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Sep 6, 2011
1 parent 300c7d4 commit 766b1d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
35 changes: 20 additions & 15 deletions lib/serialized_attributes/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ def encode(body)
formatter.encode(body)
end

def decode(data, is_new_record = false)
decoded = formatter.decode(data)
hash = ::Hash.new do |(h, key)|
type = fields[key]
h[key] = type ? type.default : nil
end
decoded.each do |k, v|
next unless include?(k)
type = fields[k]
hash[k] = type ? type.parse(v) : v
end
if decoded.blank? && is_new_record
fields.each do |key, type|
hash[key] = type.default if type.default
end
end
hash
end

def include?(key)
@fields.include?(key.to_s)
end
Expand Down Expand Up @@ -73,22 +92,8 @@ def reload(options = nil)
instance_variable_get("@#{data_field}") || begin
instance_variable_get("@#{changed_ivar}").clear if send("#{changed_ivar}?")
schema = self.class.send("#{data_field}_schema")
decoded = schema.formatter.decode(send(blob_field))
hash = ::Hash.new do |(h, key)|
type = schema.fields[key]
h[key] = type ? type.default : nil
end
hash = schema.decode(send(blob_field), new_record?)
instance_variable_set("@#{data_field}", hash)
decoded.each do |k, v|
next unless schema.include?(k)
type = schema.fields[k]
hash[k] = type ? type.parse(v) : v
end
if decoded.blank? && new_record?
schema.fields.each do |key, type|
hash[key] = type.default if type.default
end
end
hash
end
end
Expand Down
7 changes: 5 additions & 2 deletions test/serialized_attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def @record.title=(v)
@record.raw_data = self.class.format.encode(self.class.raw_hash.merge(:foo => :bar))
assert_not_nil @record.title # no undefined foo= error
assert_equal false, @record.save # extra before_save cancels the operation
assert_equal self.class.raw_hash.merge(:active => 1).stringify_keys, self.class.format.decode(@record.raw_data)
assert_equal self.class.raw_hash.merge(:active => 1).stringify_keys.keys.sort, self.class.format.decode(@record.raw_data).keys.sort
end

test "reads strings" do
Expand Down Expand Up @@ -242,7 +242,10 @@ def @record.title=(v)
assert_not_nil @record.title
@record.raw_data = nil
assert_equal false, @record.save # extra before_save cancels the operation
assert_equal self.class.raw_hash.merge(:active => 1).stringify_keys, self.class.format.decode(@record.raw_data)
expected = self.class.raw_hash.merge \
:active => true,
:birthday => Time.parse(self.class.raw_hash[:birthday])
assert_equal expected.stringify_keys, @record.class.data_schema.decode(@record.raw_data)
end

test "knows untouched record is not changed" do
Expand Down

0 comments on commit 766b1d9

Please sign in to comment.