json_record 1.0.7 → 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +4 -1
- data/lib/json_record/attribute_methods.rb +1 -1
- data/lib/json_record/embedded_document.rb +8 -0
- data/lib/json_record/field_definition.rb +9 -1
- data/spec/serialized_spec.rb +16 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/test_models.rb +10 -0
- metadata +45 -22
- data/CHANGE_LOG +0 -28
- data/MIT_LICENSE +0 -20
- data/VERSION +0 -1
- data/init.rb +0 -1
- data/json_record.gemspec +0 -73
data/Rakefile
CHANGED
@@ -12,7 +12,7 @@ begin
|
|
12
12
|
t.spec_files = FileList.new('spec/**/*_spec.rb')
|
13
13
|
end
|
14
14
|
rescue LoadError
|
15
|
-
|
15
|
+
task :test do
|
16
16
|
STDERR.puts "You must have rspec >= 1.2.9 to run the tests"
|
17
17
|
end
|
18
18
|
end
|
@@ -33,6 +33,9 @@ begin
|
|
33
33
|
gem.email = "brian@embellishedvisions.com"
|
34
34
|
gem.homepage = "http://github.com/bdurand/json_record"
|
35
35
|
gem.authors = ["Brian Durand"]
|
36
|
+
gem.files = FileList["lib/**/*", "spec/**/*", "README.rdoc", "Rakefile"].to_a
|
37
|
+
gem.has_rdoc = true
|
38
|
+
gem.extra_rdoc_files = ["README.rdoc"]
|
36
39
|
|
37
40
|
gem.add_dependency('activerecord', '>= 2.2.2')
|
38
41
|
gem.add_development_dependency('rspec', '>= 1.2.9')
|
@@ -37,7 +37,7 @@ module JsonRecord
|
|
37
37
|
if changes.include?(field.name)
|
38
38
|
changes.delete(field.name) if converted_value == changes[field.name]
|
39
39
|
else
|
40
|
-
old_value = (old_value.clone rescue old_value) unless old_value.nil?
|
40
|
+
old_value = (old_value.clone rescue old_value) unless old_value.nil? || old_value.is_a?(Numeric) || old_value.is_a?(Symbol) || old_value.is_a?(TrueClass) || old_value.is_a?(FalseClass)
|
41
41
|
changes[field.name] = old_value
|
42
42
|
end
|
43
43
|
end
|
@@ -139,6 +139,10 @@ module JsonRecord
|
|
139
139
|
@json_attributes.to_json(*args)
|
140
140
|
end
|
141
141
|
|
142
|
+
def to_hash
|
143
|
+
@json_attributes
|
144
|
+
end
|
145
|
+
|
142
146
|
def eql? (val)
|
143
147
|
val.class == self.class && val.attributes == attributes && val.parent == parent
|
144
148
|
end
|
@@ -147,6 +151,10 @@ module JsonRecord
|
|
147
151
|
eql?(val)
|
148
152
|
end
|
149
153
|
|
154
|
+
def equal? (val)
|
155
|
+
eql?(val)
|
156
|
+
end
|
157
|
+
|
150
158
|
def hash
|
151
159
|
attributes.hash + parent.hash
|
152
160
|
end
|
@@ -22,7 +22,13 @@ module JsonRecord
|
|
22
22
|
|
23
23
|
# Get the default value.
|
24
24
|
def default
|
25
|
-
|
25
|
+
if @default.nil?
|
26
|
+
nil
|
27
|
+
elsif @default.is_a?(Numeric) || @default.is_a?(Symbol) || @default.is_a?(TrueClass) || @default.is_a?(FalseClass)
|
28
|
+
@default
|
29
|
+
else
|
30
|
+
@default.dup rescue @default
|
31
|
+
end
|
26
32
|
end
|
27
33
|
|
28
34
|
# Indicates the field is multivalued.
|
@@ -72,6 +78,8 @@ module JsonRecord
|
|
72
78
|
elsif @type == Hash
|
73
79
|
raise ArgumentError.new("#{name} must be a Hash") unless val.is_a?(Hash)
|
74
80
|
return val
|
81
|
+
elsif @type == BigDecimal
|
82
|
+
return BigDecimal.new(val.to_s)
|
75
83
|
else
|
76
84
|
if val.is_a?(@type)
|
77
85
|
val
|
data/spec/serialized_spec.rb
CHANGED
@@ -94,6 +94,12 @@ describe JsonRecord::Serialized do
|
|
94
94
|
model.strings.should == ["a", "b"]
|
95
95
|
end
|
96
96
|
|
97
|
+
it "should convert values to BigDecimal" do
|
98
|
+
model = JsonRecord::Test::Model.new
|
99
|
+
model.price = '5.55'
|
100
|
+
model.price.should == BigDecimal.new('5.55')
|
101
|
+
end
|
102
|
+
|
97
103
|
it "should convert a hash to an embedded document" do
|
98
104
|
model = JsonRecord::Test::Model.new
|
99
105
|
model.primary_trait = {:name => "thing", :value => "stuff"}
|
@@ -145,6 +151,7 @@ describe JsonRecord::Serialized do
|
|
145
151
|
JsonRecord::Test::Model.new.attributes.should == {
|
146
152
|
"name"=>nil,
|
147
153
|
"price"=>nil,
|
154
|
+
"ratio"=>nil,
|
148
155
|
"string_field"=>nil,
|
149
156
|
"verified_at"=>nil,
|
150
157
|
"viewed_at"=>nil,
|
@@ -154,6 +161,7 @@ describe JsonRecord::Serialized do
|
|
154
161
|
"field_4"=>nil,
|
155
162
|
"field_5"=>nil,
|
156
163
|
"unit_price"=>nil,
|
164
|
+
"unit_ratio"=>nil,
|
157
165
|
"traits"=>[],
|
158
166
|
"value"=>0,
|
159
167
|
"strings"=>[],
|
@@ -466,4 +474,12 @@ describe JsonRecord::Serialized do
|
|
466
474
|
model.unit_price = 1.2253
|
467
475
|
model.unit_price.should == 1.23
|
468
476
|
end
|
477
|
+
|
478
|
+
it "should blow up if the json column doesn't exist" do
|
479
|
+
lambda{JsonRecord::Test::Broken.new(:name => "Test", :value => "Moo")}.should raise_error
|
480
|
+
end
|
481
|
+
|
482
|
+
it "should blow up if trying to set a json attribute hasn't been defined" do
|
483
|
+
lambda{model = JsonRecord::Test::Model.new(:undefined_attribute => "what?")}.should raise_error
|
484
|
+
end
|
469
485
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
|
3
|
-
active_record_version = ENV["ACTIVE_RECORD_VERSION"] || [">=
|
3
|
+
active_record_version = ENV["ACTIVE_RECORD_VERSION"] || [">=2.2.2", "<=2.9.9"]
|
4
4
|
active_record_version = [active_record_version] unless active_record_version.is_a?(Array)
|
5
5
|
gem 'activerecord', *active_record_version
|
6
6
|
|
data/spec/test_models.rb
CHANGED
@@ -59,6 +59,7 @@ module JsonRecord
|
|
59
59
|
schema.key :name, String, :required => true, :length => 15
|
60
60
|
schema.key :value, Integer, :default => 0
|
61
61
|
schema.key :price, Float
|
62
|
+
schema.key :ratio, BigDecimal
|
62
63
|
schema.key :verified, Boolean
|
63
64
|
schema.key :when, Date
|
64
65
|
schema.key :verified_at, Time
|
@@ -77,6 +78,7 @@ module JsonRecord
|
|
77
78
|
schema.key :field_4, :length => (4..15)
|
78
79
|
schema.key :field_5, :length => {:minimum => 5}
|
79
80
|
schema.key :unit_price, Float
|
81
|
+
schema.key :unit_ratio, BigDecimal
|
80
82
|
end
|
81
83
|
|
82
84
|
def unit_price
|
@@ -97,5 +99,13 @@ module JsonRecord
|
|
97
99
|
schema.key :another_field
|
98
100
|
end
|
99
101
|
end
|
102
|
+
|
103
|
+
class Broken < ActiveRecord::Base
|
104
|
+
set_table_name :models
|
105
|
+
serialize_to_json(:no_such_column) do |schema|
|
106
|
+
schema.key :name, String
|
107
|
+
schema.key :value, String
|
108
|
+
end
|
109
|
+
end
|
100
110
|
end
|
101
111
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 7
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 8
|
10
|
+
version: 1.0.8
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Brian Durand
|
@@ -9,39 +15,55 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date: 2010-
|
18
|
+
date: 2010-08-31 00:00:00 -05:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
22
|
name: activerecord
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
20
26
|
requirements:
|
21
27
|
- - ">="
|
22
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 2
|
33
|
+
- 2
|
23
34
|
version: 2.2.2
|
24
|
-
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
25
37
|
- !ruby/object:Gem::Dependency
|
26
38
|
name: rspec
|
27
|
-
|
28
|
-
|
29
|
-
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
30
42
|
requirements:
|
31
43
|
- - ">="
|
32
44
|
- !ruby/object:Gem::Version
|
45
|
+
hash: 13
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 2
|
49
|
+
- 9
|
33
50
|
version: 1.2.9
|
34
|
-
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
35
53
|
- !ruby/object:Gem::Dependency
|
36
54
|
name: jeweler
|
37
|
-
|
38
|
-
|
39
|
-
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
40
58
|
requirements:
|
41
59
|
- - ">="
|
42
60
|
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
43
64
|
version: "0"
|
44
|
-
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
45
67
|
description:
|
46
68
|
email: brian@embellishedvisions.com
|
47
69
|
executables: []
|
@@ -51,13 +73,8 @@ extensions: []
|
|
51
73
|
extra_rdoc_files:
|
52
74
|
- README.rdoc
|
53
75
|
files:
|
54
|
-
- CHANGE_LOG
|
55
|
-
- MIT_LICENSE
|
56
76
|
- README.rdoc
|
57
77
|
- Rakefile
|
58
|
-
- VERSION
|
59
|
-
- init.rb
|
60
|
-
- json_record.gemspec
|
61
78
|
- lib/json_record.rb
|
62
79
|
- lib/json_record/attribute_methods.rb
|
63
80
|
- lib/json_record/embedded_document.rb
|
@@ -82,21 +99,27 @@ rdoc_options:
|
|
82
99
|
require_paths:
|
83
100
|
- lib
|
84
101
|
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
85
103
|
requirements:
|
86
104
|
- - ">="
|
87
105
|
- !ruby/object:Gem::Version
|
106
|
+
hash: 3
|
107
|
+
segments:
|
108
|
+
- 0
|
88
109
|
version: "0"
|
89
|
-
version:
|
90
110
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
91
112
|
requirements:
|
92
113
|
- - ">="
|
93
114
|
- !ruby/object:Gem::Version
|
115
|
+
hash: 3
|
116
|
+
segments:
|
117
|
+
- 0
|
94
118
|
version: "0"
|
95
|
-
version:
|
96
119
|
requirements: []
|
97
120
|
|
98
121
|
rubyforge_project:
|
99
|
-
rubygems_version: 1.3.
|
122
|
+
rubygems_version: 1.3.7
|
100
123
|
signing_key:
|
101
124
|
specification_version: 3
|
102
125
|
summary: ActiveRecord support for mapping complex documents in a single RDBMS row via JSON serialization.
|
data/CHANGE_LOG
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
1.0.0
|
2
|
-
- Initial release
|
3
|
-
|
4
|
-
1.0.1
|
5
|
-
- Fixed bug where embedded documents couldn't reference the schema until a field was defined
|
6
|
-
|
7
|
-
1.0.2
|
8
|
-
- Changed EmbeddedDocument to be a module instead of a class to fix inheritance problem with validations
|
9
|
-
- Remove key and many methods from EmbeddedDocument in favor of always calling the schema
|
10
|
-
|
11
|
-
1.0.3
|
12
|
-
- Added before and after validation callbacks on EmbeddedDocument
|
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
|
18
|
-
|
19
|
-
1.0.5
|
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 []=
|
25
|
-
|
26
|
-
1.0.7
|
27
|
-
- Added attributes= to EmbeddedDocument for mass attribute assignment
|
28
|
-
- Changed deserialized Json values to go through setter methods instead of being directly set
|
data/MIT_LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright (c) 2010 Brian Durand
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.0.7
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'json_record'
|
data/json_record.gemspec
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{json_record}
|
8
|
-
s.version = "1.0.7"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Brian Durand"]
|
12
|
-
s.date = %q{2010-02-26}
|
13
|
-
s.email = %q{brian@embellishedvisions.com}
|
14
|
-
s.extra_rdoc_files = [
|
15
|
-
"README.rdoc"
|
16
|
-
]
|
17
|
-
s.files = [
|
18
|
-
"CHANGE_LOG",
|
19
|
-
"MIT_LICENSE",
|
20
|
-
"README.rdoc",
|
21
|
-
"Rakefile",
|
22
|
-
"VERSION",
|
23
|
-
"init.rb",
|
24
|
-
"json_record.gemspec",
|
25
|
-
"lib/json_record.rb",
|
26
|
-
"lib/json_record/attribute_methods.rb",
|
27
|
-
"lib/json_record/embedded_document.rb",
|
28
|
-
"lib/json_record/embedded_document_array.rb",
|
29
|
-
"lib/json_record/field_definition.rb",
|
30
|
-
"lib/json_record/json_field.rb",
|
31
|
-
"lib/json_record/schema.rb",
|
32
|
-
"lib/json_record/serialized.rb",
|
33
|
-
"spec/embedded_document_array_spec.rb",
|
34
|
-
"spec/embedded_document_spec.rb",
|
35
|
-
"spec/field_definition_spec.rb",
|
36
|
-
"spec/serialized_spec.rb",
|
37
|
-
"spec/spec_helper.rb",
|
38
|
-
"spec/test_models.rb"
|
39
|
-
]
|
40
|
-
s.homepage = %q{http://github.com/bdurand/json_record}
|
41
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
42
|
-
s.require_paths = ["lib"]
|
43
|
-
s.rubygems_version = %q{1.3.5}
|
44
|
-
s.summary = %q{ActiveRecord support for mapping complex documents in a single RDBMS row via JSON serialization.}
|
45
|
-
s.test_files = [
|
46
|
-
"spec/embedded_document_array_spec.rb",
|
47
|
-
"spec/embedded_document_spec.rb",
|
48
|
-
"spec/field_definition_spec.rb",
|
49
|
-
"spec/serialized_spec.rb",
|
50
|
-
"spec/spec_helper.rb",
|
51
|
-
"spec/test_models.rb"
|
52
|
-
]
|
53
|
-
|
54
|
-
if s.respond_to? :specification_version then
|
55
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
56
|
-
s.specification_version = 3
|
57
|
-
|
58
|
-
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
59
|
-
s.add_runtime_dependency(%q<activerecord>, [">= 2.2.2"])
|
60
|
-
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
61
|
-
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
62
|
-
else
|
63
|
-
s.add_dependency(%q<activerecord>, [">= 2.2.2"])
|
64
|
-
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
65
|
-
s.add_dependency(%q<jeweler>, [">= 0"])
|
66
|
-
end
|
67
|
-
else
|
68
|
-
s.add_dependency(%q<activerecord>, [">= 2.2.2"])
|
69
|
-
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
70
|
-
s.add_dependency(%q<jeweler>, [">= 0"])
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|