json_record 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGE_LOG +4 -0
- data/VERSION +1 -1
- data/json_record.gemspec +2 -2
- data/lib/json_record/attribute_methods.rb +3 -3
- data/lib/json_record/embedded_document.rb +3 -3
- data/lib/json_record/json_field.rb +11 -5
- data/lib/json_record/schema.rb +7 -5
- data/lib/json_record/serialized.rb +2 -2
- data/spec/embedded_document_spec.rb +6 -3
- data/spec/serialized_spec.rb +10 -1
- metadata +2 -2
data/CHANGE_LOG
CHANGED
@@ -11,3 +11,7 @@
|
|
11
11
|
1.0.3
|
12
12
|
- Added before and after validation callbacks on EmbeddedDocument
|
13
13
|
- Fixed bug where fields couldn't be set to false
|
14
|
+
|
15
|
+
1.0.4
|
16
|
+
- Fixed bug with tracking changes when initializing an EmbeddedDocument.
|
17
|
+
- Removed tracking changes of keys that are EmbeddedDocuments
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.4
|
data/json_record.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{json_record}
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Brian Durand"]
|
12
|
-
s.date = %q{2010-02-
|
12
|
+
s.date = %q{2010-02-22}
|
13
13
|
s.email = %q{brian@embellishedvisions.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.rdoc"
|
@@ -22,8 +22,8 @@ module JsonRecord
|
|
22
22
|
end
|
23
23
|
|
24
24
|
# Write a field. The field param must be a FieldDefinition and the context should be the record
|
25
|
-
# which is being read from.
|
26
|
-
def write_attribute (field, val,
|
25
|
+
# which is being read from.
|
26
|
+
def write_attribute (field, val, context)
|
27
27
|
if field.multivalued?
|
28
28
|
val = val.values if val.is_a?(Hash)
|
29
29
|
json_attributes[field.name] = EmbeddedDocumentArray.new(field.type, context, val)
|
@@ -32,7 +32,7 @@ module JsonRecord
|
|
32
32
|
converted_value = field.convert(val)
|
33
33
|
converted_value.parent = context if converted_value.is_a?(EmbeddedDocument)
|
34
34
|
unless old_value == converted_value
|
35
|
-
|
35
|
+
unless field.type.include?(EmbeddedDocument) or Thread.current[:do_not_track_json_field_changes]
|
36
36
|
changes = changed_attributes
|
37
37
|
if changes.include?(field.name)
|
38
38
|
changes.delete(field.name) if converted_value == changes[field.name]
|
@@ -84,7 +84,7 @@ module JsonRecord
|
|
84
84
|
@json_attributes = {}
|
85
85
|
attrs.each_pair do |name, value|
|
86
86
|
field = schema.fields[name.to_s] || FieldDefinition.new(name, :type => value.class)
|
87
|
-
write_attribute(field, value,
|
87
|
+
write_attribute(field, value, self)
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
@@ -156,8 +156,8 @@ module JsonRecord
|
|
156
156
|
read_attribute(field, self)
|
157
157
|
end
|
158
158
|
|
159
|
-
def write_json_attribute (json_field_name, field, value
|
160
|
-
write_attribute(field, value,
|
159
|
+
def write_json_attribute (json_field_name, field, value)
|
160
|
+
write_attribute(field, value, self)
|
161
161
|
end
|
162
162
|
|
163
163
|
def changed_attributes
|
@@ -33,11 +33,17 @@ module JsonRecord
|
|
33
33
|
unless @record[@name].blank?
|
34
34
|
json = @record[@name]
|
35
35
|
json = Zlib::Inflate.inflate(json) if @compressed
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
36
|
+
do_not_track_changes = Thread.current[:do_not_track_json_field_changes]
|
37
|
+
Thread.current[:do_not_track_json_field_changes] = true
|
38
|
+
begin
|
39
|
+
ActiveSupport::JSON.decode(json).each_pair do |attr_name, attr_value|
|
40
|
+
field = nil
|
41
|
+
@schemas.each{|schema| field = schema.fields[attr_name]; break if field}
|
42
|
+
field = FieldDefinition.new(attr_name, :type => attr_value.class) unless field
|
43
|
+
write_attribute(field, attr_value, @record)
|
44
|
+
end
|
45
|
+
ensure
|
46
|
+
Thread.current[:do_not_track_json_field_changes] = do_not_track_changes
|
41
47
|
end
|
42
48
|
end
|
43
49
|
end
|
data/lib/json_record/schema.rb
CHANGED
@@ -113,16 +113,18 @@ module JsonRecord
|
|
113
113
|
def define_json_accessor (field, json_field_name)
|
114
114
|
@klass.send(:define_method, field.name) {read_json_attribute(json_field_name, field)}
|
115
115
|
@klass.send(:define_method, "#{field.name}?") {!!read_json_attribute(json_field_name, field)} if field.type == Boolean
|
116
|
-
@klass.send(:define_method, "#{field.name}=") {|val| write_json_attribute(json_field_name, field, val
|
117
|
-
@klass.send(:define_method, "#{field.name}_changed?") {self.send(:attribute_changed?, field.name)}
|
118
|
-
@klass.send(:define_method, "#{field.name}_change") {self.send(:attribute_change, field.name)}
|
119
|
-
@klass.send(:define_method, "#{field.name}_was") {self.send(:attribute_was, field.name)}
|
116
|
+
@klass.send(:define_method, "#{field.name}=") {|val| write_json_attribute(json_field_name, field, val)}
|
120
117
|
@klass.send(:define_method, "#{field.name}_before_type_cast") {self.read_attribute_before_type_cast(field.name)}
|
118
|
+
unless field.type.include?(EmbeddedDocument)
|
119
|
+
@klass.send(:define_method, "#{field.name}_changed?") {self.send(:attribute_changed?, field.name)}
|
120
|
+
@klass.send(:define_method, "#{field.name}_change") {self.send(:attribute_change, field.name)}
|
121
|
+
@klass.send(:define_method, "#{field.name}_was") {self.send(:attribute_was, field.name)}
|
122
|
+
end
|
121
123
|
end
|
122
124
|
|
123
125
|
def define_many_json_accessor (field, json_field_name)
|
124
126
|
@klass.send(:define_method, field.name) {self.read_json_attribute(json_field_name, field)}
|
125
|
-
@klass.send(:define_method, "#{field.name}=") {|val| self.write_json_attribute(json_field_name, field, val
|
127
|
+
@klass.send(:define_method, "#{field.name}=") {|val| self.write_json_attribute(json_field_name, field, val)}
|
126
128
|
end
|
127
129
|
end
|
128
130
|
end
|
@@ -93,8 +93,8 @@ module JsonRecord
|
|
93
93
|
end
|
94
94
|
|
95
95
|
# Write a field value to a JsonField
|
96
|
-
def write_json_attribute (json_field_name, field, value
|
97
|
-
json_fields[json_field_name].write_attribute(field, value,
|
96
|
+
def write_json_attribute (json_field_name, field, value)
|
97
|
+
json_fields[json_field_name].write_attribute(field, value, self)
|
98
98
|
end
|
99
99
|
|
100
100
|
# Serialize the JSON in the record into JsonField objects.
|
@@ -36,17 +36,20 @@ describe JsonRecord::EmbeddedDocument do
|
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should track changes to attributes" do
|
39
|
-
trait = JsonRecord::Test::Trait.new
|
39
|
+
trait = JsonRecord::Test::Trait.new(:value => "val")
|
40
|
+
trait.value_changed?.should == true
|
41
|
+
trait.value_was.should == nil
|
42
|
+
trait.value_change.should == [nil, "val"]
|
40
43
|
trait.name = "test"
|
41
44
|
trait.name_was.should == nil
|
42
45
|
trait.name_changed?.should == true
|
43
46
|
trait.name_change.should == [nil, "test"]
|
44
|
-
trait.changes.should == {"name" => [nil, "test"]}
|
47
|
+
trait.changes.should == {"value" => [nil, "val"], "name" => [nil, "test"]}
|
45
48
|
trait.name = nil
|
46
49
|
trait.name_changed?.should == false
|
47
50
|
trait.name_was.should == nil
|
48
51
|
trait.name_change.should == nil
|
49
|
-
trait.changes.should == {}
|
52
|
+
trait.changes.should == {"value" => [nil, "val"]}
|
50
53
|
end
|
51
54
|
|
52
55
|
it "should convert to attributes to json" do
|
data/spec/serialized_spec.rb
CHANGED
@@ -228,10 +228,19 @@ describe JsonRecord::Serialized do
|
|
228
228
|
end
|
229
229
|
|
230
230
|
it "should track changes on json attributes" do
|
231
|
-
model = JsonRecord::Test::Model.
|
231
|
+
model = JsonRecord::Test::Model.new(:name => "test name", :primary_trait => {:name => "n1", :value => "v1"})
|
232
|
+
model.changes.should == {"name" => [nil, "test name"]}
|
233
|
+
model.primary_trait.changes.should == {"name" => [nil, "n1"], "value" => [nil, "v1"]}
|
234
|
+
model.save!
|
232
235
|
model.changes.should == {}
|
236
|
+
|
237
|
+
model.reload
|
238
|
+
model.changes.should == {}
|
239
|
+
model.primary_trait.name.should == "n1"
|
233
240
|
model.name = "new name"
|
234
241
|
model.value = 1
|
242
|
+
model.primary_trait = {:name => "n2", :value => "v2"}
|
243
|
+
|
235
244
|
model.changes.should == {"name" => ["test name", "new name"], "value" => [0, 1]}
|
236
245
|
model.name_changed?.should == true
|
237
246
|
model.name_was.should == "test name"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Durand
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-22 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|