json_record 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGE_LOG +5 -1
- data/VERSION +1 -1
- data/json_record.gemspec +2 -2
- data/lib/json_record/embedded_document.rb +18 -1
- data/lib/json_record/serialized.rb +32 -6
- data/spec/embedded_document_spec.rb +18 -0
- data/spec/serialized_spec.rb +28 -4
- data/spec/test_models.rb +20 -0
- metadata +2 -2
data/CHANGE_LOG
CHANGED
@@ -17,4 +17,8 @@
|
|
17
17
|
- Removed tracking changes of keys that are EmbeddedDocuments
|
18
18
|
|
19
19
|
1.0.5
|
20
|
-
- Fixed bug with initializing new EmbeddedDocumentArray with the proper parent when there is no data to deserialize.
|
20
|
+
- Fixed bug with initializing new EmbeddedDocumentArray with the proper parent when there is no data to deserialize.
|
21
|
+
|
22
|
+
1.0.6
|
23
|
+
- Allow EmbeddedDocument.new to call all accessors and not just those defined in the JSON schema.
|
24
|
+
- Allow getting and setting json attributes with [] and []=
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.6
|
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.6"
|
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-24}
|
13
13
|
s.email = %q{brian@embellishedvisions.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.rdoc"
|
@@ -84,7 +84,12 @@ 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
|
-
|
87
|
+
writer = "#{name}=".to_sym
|
88
|
+
if respond_to?(writer)
|
89
|
+
send(writer, value)
|
90
|
+
else
|
91
|
+
write_attribute(field, value, self)
|
92
|
+
end
|
88
93
|
end
|
89
94
|
end
|
90
95
|
|
@@ -113,6 +118,18 @@ module JsonRecord
|
|
113
118
|
changed.inject({}) {|h, attr| h[attr] = attribute_change(attr); h}
|
114
119
|
end
|
115
120
|
|
121
|
+
# Get a field from the schema with the specified name.
|
122
|
+
def [] (name)
|
123
|
+
field = schema.fields[name.to_s]
|
124
|
+
read_attribute(field, self) if field
|
125
|
+
end
|
126
|
+
|
127
|
+
# Set a field from the schema with the specified name.
|
128
|
+
def []= (name, value)
|
129
|
+
field = schema.fields[name.to_s] || FieldDefinition.new(name, :type => value.class)
|
130
|
+
write_attribute(field, value, self)
|
131
|
+
end
|
132
|
+
|
116
133
|
def to_json (*args)
|
117
134
|
@json_attributes.to_json(*args)
|
118
135
|
end
|
@@ -31,14 +31,18 @@ module JsonRecord
|
|
31
31
|
end
|
32
32
|
|
33
33
|
module ClassMethods
|
34
|
-
# Get the field definition of the JSON field from the schema it is defined in.
|
34
|
+
# Get the json field and the field definition of the JSON field from the schema it is defined in.
|
35
35
|
def json_field_definition (name)
|
36
|
-
field = nil
|
37
36
|
if json_serialized_fields
|
38
37
|
name = name.to_s
|
39
|
-
json_serialized_fields.
|
38
|
+
json_serialized_fields.each_pair do |fname, schemas|
|
39
|
+
schemas.each do |schema|
|
40
|
+
field = schema.fields[name];
|
41
|
+
return [fname, field] if field
|
42
|
+
end
|
43
|
+
end
|
40
44
|
end
|
41
|
-
return
|
45
|
+
return nil
|
42
46
|
end
|
43
47
|
end
|
44
48
|
|
@@ -47,6 +51,8 @@ module JsonRecord
|
|
47
51
|
base.before_save :serialize_json_attributes
|
48
52
|
base.alias_method_chain :reload, :serialized_json
|
49
53
|
base.alias_method_chain :attributes, :serialized_json
|
54
|
+
base.alias_method_chain :read_attribute, :serialized_json
|
55
|
+
base.alias_method_chain :write_attribute, :serialized_json
|
50
56
|
end
|
51
57
|
|
52
58
|
# Get the JsonField objects for the record.
|
@@ -72,6 +78,26 @@ module JsonRecord
|
|
72
78
|
return attrs
|
73
79
|
end
|
74
80
|
|
81
|
+
def read_attribute_with_serialized_json (name)
|
82
|
+
name = name.to_s
|
83
|
+
json_field, field_definition = self.class.json_field_definition(name)
|
84
|
+
if field_definition
|
85
|
+
read_json_attribute(json_field, field_definition)
|
86
|
+
else
|
87
|
+
read_attribute_without_serialized_json(name)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def write_attribute_with_serialized_json (name, value)
|
92
|
+
name = name.to_s
|
93
|
+
json_field, field_definition = self.class.json_field_definition(name)
|
94
|
+
if field_definition
|
95
|
+
write_json_attribute(json_field, field_definition, value)
|
96
|
+
else
|
97
|
+
write_attribute_without_serialized_json(name, value)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
75
101
|
protected
|
76
102
|
|
77
103
|
# Returns a hash of all the JsonField objects merged together.
|
@@ -87,12 +113,12 @@ module JsonRecord
|
|
87
113
|
@json_field_names = json_serialized_fields.values.flatten.collect{|s| s.fields.keys}.flatten
|
88
114
|
end
|
89
115
|
|
90
|
-
# Read a field value from a JsonField
|
116
|
+
# Read a field value from a JsonField.
|
91
117
|
def read_json_attribute (json_field_name, field)
|
92
118
|
json_fields[json_field_name].read_attribute(field, self)
|
93
119
|
end
|
94
120
|
|
95
|
-
# Write a field value to a JsonField
|
121
|
+
# Write a field value to a JsonField.
|
96
122
|
def write_json_attribute (json_field_name, field, value)
|
97
123
|
json_fields[json_field_name].write_attribute(field, value, self)
|
98
124
|
end
|
@@ -65,4 +65,22 @@ describe JsonRecord::EmbeddedDocument do
|
|
65
65
|
(trait_1.hash == trait_2.hash).should == true
|
66
66
|
end
|
67
67
|
|
68
|
+
it "should read json attributes with []" do
|
69
|
+
dimension = JsonRecord::Test::Dimension.new(:width => 100)
|
70
|
+
dimension[:width].should == 100
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should be able to write json attributes with []=" do
|
74
|
+
dimension = JsonRecord::Test::Dimension.new
|
75
|
+
dimension[:width] = 100
|
76
|
+
dimension.width.should == 100
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should set all accessors on initialize" do
|
80
|
+
dimension = JsonRecord::Test::Dimension.new(:width => 100, :height => :infinity, :unit => "meters")
|
81
|
+
dimension.width.should == 100
|
82
|
+
dimension.height.should == 1000000000
|
83
|
+
dimension.unit.should == "meters"
|
84
|
+
end
|
85
|
+
|
68
86
|
end
|
data/spec/serialized_spec.rb
CHANGED
@@ -9,11 +9,19 @@ describe JsonRecord::Serialized do
|
|
9
9
|
JsonRecord::Test.drop_tables
|
10
10
|
end
|
11
11
|
|
12
|
-
it "should have accessors for json attributes" do
|
12
|
+
it "should have accessors for json attributes and not interfere with column attribute accessors" do
|
13
13
|
model = JsonRecord::Test::Model.new
|
14
14
|
model.name.should == nil
|
15
15
|
model.name = "test"
|
16
16
|
model.name.should == "test"
|
17
|
+
model[:name].should == "test"
|
18
|
+
model['name'].should == "test"
|
19
|
+
model[:name] = "new value"
|
20
|
+
model.name.should == "new value"
|
21
|
+
model.string_field = "a"
|
22
|
+
model.string_field.should == "a"
|
23
|
+
model[:string_field] = "b"
|
24
|
+
model[:string_field].should == "b"
|
17
25
|
end
|
18
26
|
|
19
27
|
it "should convert blank values to nil" do
|
@@ -145,6 +153,7 @@ describe JsonRecord::Serialized do
|
|
145
153
|
"field_3"=>nil,
|
146
154
|
"field_4"=>nil,
|
147
155
|
"field_5"=>nil,
|
156
|
+
"unit_price"=>nil,
|
148
157
|
"traits"=>[],
|
149
158
|
"value"=>0,
|
150
159
|
"strings"=>[],
|
@@ -393,9 +402,17 @@ describe JsonRecord::Serialized do
|
|
393
402
|
end
|
394
403
|
|
395
404
|
it "should get the json field definition for a field" do
|
396
|
-
JsonRecord::Test::Model.json_field_definition(:value)
|
397
|
-
|
398
|
-
|
405
|
+
json_field, field = JsonRecord::Test::Model.json_field_definition(:value)
|
406
|
+
json_field.should == "json"
|
407
|
+
field.name.should == "value"
|
408
|
+
|
409
|
+
json_field, field = JsonRecord::Test::Model.json_field_definition("field_1")
|
410
|
+
json_field.should == "compressed_json"
|
411
|
+
field.name.should == "field_1"
|
412
|
+
|
413
|
+
json_field, field = JsonRecord::Test::Model.json_field_definition("nothing")
|
414
|
+
json_field.should == nil
|
415
|
+
field.should == nil
|
399
416
|
end
|
400
417
|
|
401
418
|
it "should validate uniqueness of embedded documents" do
|
@@ -442,4 +459,11 @@ describe JsonRecord::Serialized do
|
|
442
459
|
trait.valid?.should == true
|
443
460
|
trait.callbacks.should == [:before_validation, :after_validation]
|
444
461
|
end
|
462
|
+
|
463
|
+
it "should allow overriding the attribute reader and writers" do
|
464
|
+
model = JsonRecord::Test::Model.new(:unit_price => :infinity)
|
465
|
+
model.unit_price.should == 1000000000
|
466
|
+
model.unit_price = 1.2253
|
467
|
+
model.unit_price.should == 1.23
|
468
|
+
end
|
445
469
|
end
|
data/spec/test_models.rb
CHANGED
@@ -43,6 +43,15 @@ module JsonRecord
|
|
43
43
|
include JsonRecord::EmbeddedDocument
|
44
44
|
schema.key :height, Integer, :required => true
|
45
45
|
schema.key :width, Integer, :required => true
|
46
|
+
attr_accessor :unit
|
47
|
+
|
48
|
+
def height= (value)
|
49
|
+
if value == :infinity
|
50
|
+
self[:height] = 1000000000
|
51
|
+
else
|
52
|
+
self[:height] = value
|
53
|
+
end
|
54
|
+
end
|
46
55
|
end
|
47
56
|
|
48
57
|
class Model < ActiveRecord::Base
|
@@ -67,6 +76,17 @@ module JsonRecord
|
|
67
76
|
schema.key :field_3, :in => ("A".."M")
|
68
77
|
schema.key :field_4, :length => (4..15)
|
69
78
|
schema.key :field_5, :length => {:minimum => 5}
|
79
|
+
schema.key :unit_price, Float
|
80
|
+
end
|
81
|
+
|
82
|
+
def unit_price
|
83
|
+
p = self[:price]
|
84
|
+
p.is_a?(Numeric) ? (p * 100).round / 100.0 : p
|
85
|
+
end
|
86
|
+
|
87
|
+
def unit_price= (value)
|
88
|
+
value = 1000000000 if value == :infinity
|
89
|
+
self[:price] = value
|
70
90
|
end
|
71
91
|
end
|
72
92
|
|
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.6
|
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-24 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|