kissifer-json-attributes 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 kissifer
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.
@@ -0,0 +1,7 @@
1
+ = json-attributes
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 kissifer. See LICENSE for details.
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "json-attributes"
8
+ gem.summary = %Q{Module to help with initialising/setting/getting object attributes from json strings}
9
+ gem.email = "tierneydrchris@gmail.com"
10
+ gem.homepage = "http://github.com/kissifer/json-attributes"
11
+ gem.authors = ["kissifer"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ spec.spec_opts = ['--options spec/spec.opts']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+
33
+ task :default => :spec
34
+
35
+ require 'rake/rdoctask'
36
+ Rake::RDocTask.new do |rdoc|
37
+ if File.exist?('VERSION.yml')
38
+ config = YAML.load(File.read('VERSION.yml'))
39
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
40
+ else
41
+ version = ""
42
+ end
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "json-attributes #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
49
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,48 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{json-attributes}
5
+ s.version = "0.0.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["kissifer"]
9
+ s.date = %q{2009-06-24}
10
+ s.email = %q{tierneydrchris@gmail.com}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "json-attributes.gemspec",
23
+ "lib/json-attributes.rb",
24
+ "lib/monkey_patches.rb",
25
+ "spec/json-attributes_spec.rb",
26
+ "spec/spec.opts",
27
+ "spec/spec_helper.rb"
28
+ ]
29
+ s.homepage = %q{http://github.com/kissifer/json-attributes}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.4}
33
+ s.summary = %q{Module to help with initialising/setting/getting object attributes from json strings}
34
+ s.test_files = [
35
+ "spec/json-attributes_spec.rb",
36
+ "spec/spec_helper.rb"
37
+ ]
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
+ else
45
+ end
46
+ else
47
+ end
48
+ end
@@ -0,0 +1,82 @@
1
+ require 'json/ext'
2
+ require File.expand_path(File.dirname(__FILE__) + '/monkey_patches')
3
+
4
+ module JsonAttributes
5
+ module Mixin
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def json_reader(*syms)
12
+ attr_reader *syms
13
+ json_readers.concat(syms)
14
+ end
15
+
16
+ def json_writer(*syms)
17
+ attr_writer *syms
18
+ json_writers.concat(syms)
19
+ end
20
+
21
+ def json_accessor(*syms)
22
+ attr_accessor *syms
23
+ json_readers.concat(syms)
24
+ json_writers.concat(syms)
25
+ end
26
+
27
+ def json_conditioner(*syms)
28
+ json_conditioners.concat(syms)
29
+ end
30
+
31
+ def json_validator(*syms)
32
+ json_validators.concat(syms)
33
+ end
34
+
35
+ def json_readers
36
+ @json_readers ||= []
37
+ end
38
+
39
+ def json_writers
40
+ @json_writers ||= []
41
+ end
42
+
43
+ def json_conditioners
44
+ @json_conditioners ||= []
45
+ end
46
+
47
+ def json_validators
48
+ @json_validators ||= []
49
+ end
50
+ end
51
+
52
+ def report_to_json
53
+ self.class.json_readers.inject({}) do |hash, reader|
54
+ hash[reader] = send reader
55
+ hash
56
+ end.to_json
57
+ end
58
+
59
+ def update_from_json(json)
60
+ parsed = JSON.parse(json).symbolize_keys!
61
+ self.class.json_conditioners.each do |conditioner|
62
+ parsed = send(conditioner, parsed)
63
+ end
64
+
65
+ self.class.json_validators.each do |validator|
66
+ send(validator, parsed)
67
+ end
68
+
69
+ parsed.each_pair do |key, value|
70
+ self.send("#{key}=", value) if self.class.json_writers.include?(key.to_sym)
71
+ end
72
+ end
73
+
74
+ def create_from_json(json)
75
+ parsed = JSON.parse(json)
76
+ raise "Attempt to create without all the required json elements" unless self.class.json_writers.all? do |required|
77
+ parsed.has_key?(required.to_s)
78
+ end
79
+ update_from_json(json)
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,50 @@
1
+ class Hash
2
+ def assert_valid_keys(*valid_keys)
3
+ # Lifted from activesupport
4
+ unknown_keys = keys - [valid_keys].flatten
5
+ raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
6
+ end
7
+
8
+ def symbolize_keys!
9
+ # Lifted from merb
10
+ each do |key, value|
11
+ sym = key.respond_to?(:to_sym) ? key.to_sym : key
12
+ self[sym] = value
13
+ delete(key) unless key == sym
14
+ end
15
+ self
16
+ end
17
+ end
18
+
19
+ class Array
20
+ def assert_valid_elements(*valid_elements)
21
+ # Lifted from activesupport
22
+ unknown_elements = self - [valid_elements].flatten
23
+ raise(ArgumentError, "Unknown elements(s): #{unknown_elements.join(", ")}") unless unknown_elements.empty?
24
+ end
25
+
26
+ def symbolize_elements!
27
+ collect! do |element|
28
+ element.respond_to?(:to_sym) ? element.to_sym : element
29
+ end
30
+ self
31
+ end
32
+ end
33
+
34
+ module Rack
35
+ class Request
36
+ def url_with_no_query
37
+ url = scheme + "://"
38
+ url << host
39
+
40
+ if scheme == "https" && port != 443 ||
41
+ scheme == "http" && port != 80
42
+ url << ":#{port}"
43
+ end
44
+
45
+ url << path
46
+
47
+ url
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,234 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+
4
+ describe "A class that includes JsonAttributes::Mixin" do
5
+ context "(readers)" do
6
+ it "should allow the ability to specify attr_readers that are included in the json report" do
7
+ reader_test_class = Class.new do
8
+ include JsonAttributes::Mixin
9
+
10
+ json_reader :foo, :bar
11
+
12
+ def initialize
13
+ @foo = "food"
14
+ @bar = "bard"
15
+ end
16
+ end
17
+
18
+ test = reader_test_class.new
19
+ test.foo.should == "food"
20
+ test.bar.should == "bard"
21
+ test.report_to_json.should == JSON.unparse({:foo => "food", :bar => "bard"})
22
+ end
23
+ end
24
+
25
+ context "(writers)" do
26
+ it "should allow the ability to specify attr_writers that can be updated from json" do
27
+ writer_test_class = Class.new do
28
+ include JsonAttributes::Mixin
29
+
30
+ json_writer :foo, :bar
31
+ attr_reader :foo, :bar
32
+
33
+ def initialize
34
+ @foo = "food"
35
+ @bar = "bard"
36
+ end
37
+ end
38
+
39
+ test = writer_test_class.new
40
+ test.foo.should == "food"
41
+ test.bar.should == "bard"
42
+
43
+ test.update_from_json(JSON.unparse({:foo => "foof", :bar => "barf"}))
44
+ test.foo.should == "foof"
45
+ test.bar.should == "barf"
46
+
47
+ test.update_from_json(JSON.unparse({:foo => "fool"}))
48
+ test.foo.should == "fool"
49
+ test.bar.should == "barf"
50
+
51
+ lambda{test.foo = "foo"}.should_not raise_error
52
+ lambda{test.bar = "bar"}.should_not raise_error
53
+ end
54
+
55
+ it "should ignore irrelevant items when updating" do
56
+ writer_test_class = Class.new do
57
+ include JsonAttributes::Mixin
58
+
59
+ json_writer :foo
60
+
61
+ def initialize
62
+ @foo = "food"
63
+ end
64
+ end
65
+
66
+ test = writer_test_class.new
67
+ lambda{test.update_from_json(JSON.unparse({:foo => "foof", :bar => "barf"}))}.should_not raise_error
68
+ end
69
+
70
+ it "should not allow readers to be updated" do
71
+ writer_test_class = Class.new do
72
+ include JsonAttributes::Mixin
73
+
74
+ json_reader :foo
75
+
76
+ def initialize
77
+ @foo = "food"
78
+ end
79
+ end
80
+
81
+ test = writer_test_class.new
82
+ test.update_from_json(JSON.unparse({:foo => "foof"}))
83
+ test.foo.should == "food"
84
+ end
85
+
86
+ it "should complain about a non hash-like top-level node when updating" do
87
+ test_class = Class.new do
88
+ include JsonAttributes::Mixin
89
+ end
90
+
91
+ test = test_class.new
92
+ lambda{test.update_from_json(JSON.unparse([:foo, "foof"]))}.should raise_error
93
+ end
94
+ end
95
+
96
+ context "(accessors)" do
97
+ it "should allow the ability to specify readable and writable accessors" do
98
+ accessor_test_class = Class.new do
99
+ include JsonAttributes::Mixin
100
+
101
+ json_accessor :foo, :bar
102
+
103
+ def initialize
104
+ @foo = "food"
105
+ @bar = "bard"
106
+ end
107
+ end
108
+
109
+ test = accessor_test_class.new
110
+ test.foo = "fool"
111
+ test.foo.should == "fool"
112
+ test.update_from_json(JSON.unparse({:foo => "foof"}))
113
+ test.report_to_json.should == JSON.unparse({:foo => "foof", :bar => "bard"})
114
+ end
115
+ end
116
+
117
+ context "(creator)" do
118
+ it "should provide a create_from_json method that expects ALL of the give json writers" do
119
+ creator_test_class = Class.new do
120
+ include JsonAttributes::Mixin
121
+
122
+ json_writer :foo, :bar
123
+ attr_reader :foo, :bar
124
+
125
+ def initialize
126
+ @foo = "food"
127
+ @bar = "bard"
128
+ end
129
+ end
130
+
131
+ test = creator_test_class.new
132
+ lambda{test.create_from_json(JSON.unparse({:foo => "foof"}))}.should raise_error
133
+ test.foo.should == "food"
134
+ test.create_from_json(JSON.unparse({:foo => "foof", :bar => "barf"}))
135
+ test.foo.should == "foof"
136
+ test.bar.should == "barf"
137
+ end
138
+ end
139
+
140
+ context "(conditioning parsed json)" do
141
+ before(:each) do
142
+ @conditioning_test_class = Class.new do
143
+ include JsonAttributes::Mixin
144
+
145
+ json_writer :foo
146
+ attr_reader :foo
147
+
148
+ json_conditioner :call_me_first, :call_me_after
149
+
150
+ def initialize
151
+ @foo = "food"
152
+ end
153
+ end
154
+ end
155
+
156
+ context "during update" do
157
+ it "should allow the parsed json to be conditioned in a controller way" do
158
+
159
+ test = @conditioning_test_class.new
160
+ test.should_receive(:call_me_first).with({:foo => "foof"}).and_return({:foo => "fool"})
161
+ test.should_receive(:call_me_after).with({:foo => "fool"}).and_return({:foo => "foot"})
162
+ test.update_from_json(JSON.unparse({:foo => "foof"}))
163
+ test.foo.should == "foot"
164
+ end
165
+ end
166
+
167
+ context "during create" do
168
+ it "should allow the parsed json to be conditioned in a controller way" do
169
+
170
+ test = @conditioning_test_class.new
171
+ test.should_receive(:call_me_first).with({:foo => "foof"}).and_return({:foo => "fool"})
172
+ test.should_receive(:call_me_after).with({:foo => "fool"}).and_return({:foo => "foot"})
173
+ test.create_from_json(JSON.unparse({:foo => "foof"}))
174
+ test.foo.should == "foot"
175
+ end
176
+ end
177
+ end
178
+
179
+ context "(validating parsed json)" do
180
+ before(:each) do
181
+ @validating_test_class = Class.new do
182
+ include JsonAttributes::Mixin
183
+
184
+ json_writer :foo
185
+ attr_reader :foo
186
+
187
+ json_conditioner :condition
188
+ json_validator :validator_1, :validator_2
189
+
190
+ def initialize
191
+ @foo = "food"
192
+ end
193
+ end
194
+ end
195
+
196
+ context "during update" do
197
+ it "should not update the object if a validator rasies an error" do
198
+ test = @validating_test_class.new
199
+ test.should_receive(:condition).and_return({:foo => "fool"})
200
+ test.should_receive(:validator_1).with({:foo => "fool"})
201
+ test.should_receive(:validator_2).with({:foo => "fool"}).and_raise("Something did not validate")
202
+
203
+ lambda{test.update_from_json(JSON.unparse({:foo => "foof"}))}.should raise_error("Something did not validate")
204
+ test.foo.should == "food"
205
+
206
+ test.should_receive(:condition).and_return({:foo => "fool"})
207
+ test.should_receive(:validator_1).with({:foo => "fool"})
208
+ test.should_receive(:validator_2).with({:foo => "fool"})
209
+
210
+ test.update_from_json(JSON.unparse({:foo => "foof"}))
211
+ test.foo.should == "fool"
212
+ end
213
+ end
214
+
215
+ context "during create" do
216
+ it "should not update the object if a validator rasies an error" do
217
+ test = @validating_test_class.new
218
+ test.should_receive(:condition).and_return({:foo => "fool"})
219
+ test.should_receive(:validator_1).with({:foo => "fool"})
220
+ test.should_receive(:validator_2).with({:foo => "fool"}).and_raise("Something did not validate")
221
+
222
+ lambda{test.create_from_json(JSON.unparse({:foo => "foof"}))}.should raise_error("Something did not validate")
223
+ test.foo.should == "food"
224
+
225
+ test.should_receive(:condition).and_return({:foo => "fool"})
226
+ test.should_receive(:validator_1).with({:foo => "fool"})
227
+ test.should_receive(:validator_2).with({:foo => "fool"})
228
+
229
+ test.create_from_json(JSON.unparse({:foo => "foof"}))
230
+ test.foo.should == "fool"
231
+ end
232
+ end
233
+ end
234
+ end
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format nested
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'json/ext'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'json-attributes'
8
+
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kissifer-json-attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - kissifer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-24 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: tierneydrchris@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - json-attributes.gemspec
33
+ - lib/json-attributes.rb
34
+ - lib/monkey_patches.rb
35
+ - spec/json-attributes_spec.rb
36
+ - spec/spec.opts
37
+ - spec/spec_helper.rb
38
+ has_rdoc: false
39
+ homepage: http://github.com/kissifer/json-attributes
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --charset=UTF-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Module to help with initialising/setting/getting object attributes from json strings
64
+ test_files:
65
+ - spec/json-attributes_spec.rb
66
+ - spec/spec_helper.rb