in_json 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/in_json.gemspec +5 -2
- data/lib/in_json/ext/hash.rb +21 -0
- data/lib/in_json.rb +13 -1
- data/spec/in_json/ext/hash_spec.rb +30 -0
- data/spec/in_json_spec.rb +16 -1
- metadata +7 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/in_json.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{in_json}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.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 = ["Henry Hsu"]
|
12
|
-
s.date = %q{2011-05-
|
12
|
+
s.date = %q{2011-05-31}
|
13
13
|
s.description = %q{JSON serialization for Rails. Straight-forward attributes. Only serialize what you want. Optimally obeys eager-loaded associations}
|
14
14
|
s.email = %q{henry.hsu@photomugs.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -28,6 +28,8 @@ Gem::Specification.new do |s|
|
|
28
28
|
"in_json.gemspec",
|
29
29
|
"lib/in_json.rb",
|
30
30
|
"lib/in_json/ext/array.rb",
|
31
|
+
"lib/in_json/ext/hash.rb",
|
32
|
+
"spec/in_json/ext/hash_spec.rb",
|
31
33
|
"spec/in_json_spec.rb",
|
32
34
|
"spec/spec_helper.rb"
|
33
35
|
]
|
@@ -37,6 +39,7 @@ Gem::Specification.new do |s|
|
|
37
39
|
s.rubygems_version = %q{1.5.0}
|
38
40
|
s.summary = %q{Optimally straight-forward JSON serialization for Rails}
|
39
41
|
s.test_files = [
|
42
|
+
"spec/in_json/ext/hash_spec.rb",
|
40
43
|
"spec/in_json_spec.rb",
|
41
44
|
"spec/spec_helper.rb"
|
42
45
|
]
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module InJson
|
2
|
+
module HashExt
|
3
|
+
def self.included(base)
|
4
|
+
base.send :include, InstanceMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module InstanceMethods
|
8
|
+
def recursively_reject(&blk)
|
9
|
+
reject(&blk).inject({}) do |result, k_v|
|
10
|
+
key, value = k_v
|
11
|
+
result[key] = value.is_a?(Hash) ? value.recursively_reject(&blk) : value
|
12
|
+
result
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Hash.class_eval do
|
20
|
+
include InJson::HashExt
|
21
|
+
end
|
data/lib/in_json.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'in_json/ext/array'
|
2
|
+
require 'in_json/ext/hash'
|
2
3
|
|
3
4
|
module InJson
|
4
5
|
def self.included(base)
|
@@ -38,7 +39,8 @@ module InJson
|
|
38
39
|
# Stores any method calls as {Hash} keys
|
39
40
|
# @return [Hash] the evaluated definition
|
40
41
|
def method_missing(method, *args, &block)
|
41
|
-
|
42
|
+
method_s = method.to_s
|
43
|
+
method = method_s.sub(/^_/, '').to_sym if method_s =~ /^_.*/
|
42
44
|
@hash[method] = block_given? ? Definition.new.instance_eval(&block) : args.first
|
43
45
|
@hash
|
44
46
|
end
|
@@ -55,6 +57,16 @@ module InJson
|
|
55
57
|
|
56
58
|
write_inheritable_attribute :in_json_definitions, definitions
|
57
59
|
end
|
60
|
+
|
61
|
+
# Calculates associations to be load alongside based on an {InJson} definition
|
62
|
+
# @param [Symbol] name the definition to calculate from
|
63
|
+
# @return [Hash] the associations
|
64
|
+
def include_in_json(name = :default)
|
65
|
+
definitions = read_inheritable_attribute(:in_json_definitions) || {}
|
66
|
+
definition = definitions[name]
|
67
|
+
return unless definition
|
68
|
+
definition.recursively_reject { |key, value| value.nil? }
|
69
|
+
end
|
58
70
|
end
|
59
71
|
|
60
72
|
module InstanceMethods
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
before do
|
5
|
+
@sample = {
|
6
|
+
1 => {1 => 'a'},
|
7
|
+
2 => {1 => 'b'},
|
8
|
+
3 => {2 => 'c'},
|
9
|
+
4 => {2 => 'd'},
|
10
|
+
5 => {3 => 'e'},
|
11
|
+
6 => {3 => 'f'}
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
it "rejects even keys" do
|
16
|
+
@sample.reject { |key, value| key % 2 == 0 }.should == {
|
17
|
+
1 => {1 => 'a'},
|
18
|
+
3 => {2 => 'c'},
|
19
|
+
5 => {3 => 'e'}
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
it "recursively rejects even keys" do
|
24
|
+
@sample.recursively_reject { |key, value| key % 2 == 0 }.should == {
|
25
|
+
1 => {1 => 'a'},
|
26
|
+
3 => {},
|
27
|
+
5 => {3 => 'e'}
|
28
|
+
}
|
29
|
+
end
|
30
|
+
end
|
data/spec/in_json_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
class User < ActiveRecord::Base
|
4
4
|
has_many :posts
|
@@ -263,6 +263,21 @@ describe InJson do
|
|
263
263
|
}.should have_queries(0)
|
264
264
|
end
|
265
265
|
|
266
|
+
it "should intelligently calculate eager-loading includes" do
|
267
|
+
User.include_in_json(:with_posts_and_comments).should == {
|
268
|
+
:posts => {
|
269
|
+
:comments => {}
|
270
|
+
}
|
271
|
+
}
|
272
|
+
end
|
273
|
+
|
274
|
+
it "should use calculated eager-loading includes" do
|
275
|
+
lambda { @users = User.find(:all, :include => User.include_in_json(:with_posts_and_comments)) }.should have_queries(3)
|
276
|
+
lambda {
|
277
|
+
@users.in_json(:with_posts_and_comments)
|
278
|
+
}.should have_queries(0)
|
279
|
+
end
|
280
|
+
|
266
281
|
it "should return model as json" do
|
267
282
|
@user_hash = JSON.parse(@user.to_json)
|
268
283
|
@user_hash['name'].should == 'John User'
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: in_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Henry Hsu
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-05-
|
18
|
+
date: 2011-05-31 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -251,6 +251,8 @@ files:
|
|
251
251
|
- in_json.gemspec
|
252
252
|
- lib/in_json.rb
|
253
253
|
- lib/in_json/ext/array.rb
|
254
|
+
- lib/in_json/ext/hash.rb
|
255
|
+
- spec/in_json/ext/hash_spec.rb
|
254
256
|
- spec/in_json_spec.rb
|
255
257
|
- spec/spec_helper.rb
|
256
258
|
has_rdoc: true
|
@@ -288,5 +290,6 @@ signing_key:
|
|
288
290
|
specification_version: 3
|
289
291
|
summary: Optimally straight-forward JSON serialization for Rails
|
290
292
|
test_files:
|
293
|
+
- spec/in_json/ext/hash_spec.rb
|
291
294
|
- spec/in_json_spec.rb
|
292
295
|
- spec/spec_helper.rb
|