in_json 0.0.5 → 0.0.6

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
data/in_json.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{in_json}
8
- s.version = "0.0.5"
8
+ s.version = "0.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 = ["Henry Hsu"]
data/lib/in_json.rb CHANGED
@@ -58,19 +58,47 @@ module InJson
58
58
  write_inheritable_attribute :in_json_definitions, definitions
59
59
  end
60
60
 
61
+ # TODO move precedence to doc
62
+ def in_json_definition(name, injected_definition)
63
+ definitions = read_inheritable_attribute(:in_json_definitions)
64
+
65
+ # Try nested first (if I am nested)
66
+ return injected_definition if injected_definition.kind_of?(Hash)
67
+
68
+ # Try named second
69
+ injected_definition.kind_of?(Symbol) && definitions && result = definitions[injected_definition]
70
+ return result if result
71
+
72
+ # Try thread third
73
+ thread_definition = Thread.current[:in_json_definition]
74
+ thread_definition && definitions && result = definitions[thread_definition]
75
+ return result if result # *yuck*
76
+
77
+ # Try given definitions fourth
78
+ definitions && result = definitions[name]
79
+ return result if result
80
+
81
+ # Try default last
82
+ return definitions && definitions[:default]
83
+ end
84
+
61
85
  # Calculates associations to be load alongside based on an {InJson} definition
62
86
  # @param [Symbol] name the definition to calculate from
63
87
  # @return [Hash] the associations
64
- def include_in_json(name = :default)
65
- definitions = read_inheritable_attribute(:in_json_definitions) || {}
66
- definition = definitions[name]
88
+ def include_in_json(name = :default, injected_definition = nil)
89
+ definition = in_json_definition(name, injected_definition)
67
90
  return unless definition
68
-
69
91
  reflexions = reflections
70
92
 
71
- definition.recursively_reject({}) { |key, value|
72
- !(reflexions && reflexions.respond_to?(:has_key?) && reflexions.has_key?(key)) && value.nil?
73
- }
93
+ definition.inject({}) do |result, attr_dfn|
94
+ attr, definition = attr_dfn
95
+
96
+ klass = reflexions[attr].klass if reflexions.has_key?(attr) && reflexions[attr]
97
+ def_at = (klass.include_in_json(name, definition) || {}) if klass
98
+
99
+ result[attr] = def_at if def_at
100
+ result
101
+ end
74
102
  end
75
103
  end
76
104
 
@@ -80,7 +108,7 @@ module InJson
80
108
  # @param [Symbol, Hash, nil] injected_definition a named {InJson} definition, a full Hash definition, or nil
81
109
  # @return [Hash] the JSON-ready Hash
82
110
  def in_json(name = :default, injected_definition = nil)
83
- definition = in_json_definition(name, injected_definition)
111
+ definition = self.class.in_json_definition(name, injected_definition)
84
112
  attrs = attributes.freeze.symbolize_keys
85
113
  return attrs unless definition
86
114
  definition.inject({}) do |result, attr_dfn|
@@ -94,32 +122,6 @@ module InJson
94
122
  end
95
123
  end
96
124
  alias_method :as_json, :in_json
97
-
98
- protected
99
-
100
- # TODO move precedence to doc
101
- def in_json_definition(name, injected_definition)
102
- definitions = self.class.read_inheritable_attribute(:in_json_definitions)
103
-
104
- # Try nested first (if I am nested)
105
- return injected_definition if injected_definition.kind_of?(Hash)
106
-
107
- # Try named second
108
- injected_definition.kind_of?(Symbol) && definitions && result = definitions[injected_definition]
109
- return result if result
110
-
111
- # Try thread third
112
- thread_definition = Thread.current[:in_json_definition]
113
- thread_definition && definitions && result = definitions[thread_definition]
114
- return result if result # *yuck*
115
-
116
- # Try given definitions fourth
117
- definitions && result = definitions[name]
118
- return result if result
119
-
120
- # Try default last
121
- return definitions && definitions[:default]
122
- end
123
125
  end
124
126
  end
125
127
 
@@ -6,11 +6,11 @@ module InJson
6
6
 
7
7
  module InstanceMethods
8
8
  def recursively_reject(default = nil, &blk)
9
- reject(&blk).inject({}) do |result, k_v|
9
+ inject({}) do |result, k_v|
10
10
  key, value = k_v
11
11
  result[key] = value.is_a?(Hash) ? value.recursively_reject(&blk) : (value || default)
12
12
  result
13
- end
13
+ end.reject(&blk)
14
14
  end
15
15
  end
16
16
  end
@@ -19,7 +19,7 @@ describe Hash do
19
19
  5 => {3 => 'e'}
20
20
  }
21
21
  end
22
-
22
+
23
23
  it "recursively rejects even keys" do
24
24
  @sample.recursively_reject { |key, value| key % 2 == 0 }.should == {
25
25
  1 => {1 => 'a'},
data/spec/in_json_spec.rb CHANGED
@@ -64,6 +64,11 @@ class User < ActiveRecord::Base
64
64
  end
65
65
  end
66
66
 
67
+ in_json(:with_posts_and_all_named) do
68
+ posts :with_all
69
+ end
70
+
71
+
67
72
  in_json(:with_posts_and_comments_missing) do
68
73
  posts do
69
74
  title
@@ -98,6 +103,11 @@ class Post < ActiveRecord::Base
98
103
  score
99
104
  end
100
105
  end
106
+
107
+ in_json(:with_all) do
108
+ reviews
109
+ comments :with_votes
110
+ end
101
111
  end
102
112
 
103
113
  class BlogPost < Post
@@ -105,6 +115,7 @@ end
105
115
 
106
116
  class Comment < ActiveRecord::Base
107
117
  belongs_to :post
118
+ has_many :votes
108
119
 
109
120
  in_json do
110
121
  content
@@ -121,6 +132,19 @@ class Comment < ActiveRecord::Base
121
132
  in_json(:with_posts_and_comments_named) do # Just to counter-act collission
122
133
  content
123
134
  end
135
+
136
+ in_json (:with_votes) do
137
+ votes
138
+ end
139
+ end
140
+
141
+ class Comment::Vote < ActiveRecord::Base
142
+ belongs_to :comment
143
+ belongs_to :user
144
+
145
+ in_json do
146
+ rating
147
+ end
124
148
  end
125
149
 
126
150
  class Review < ActiveRecord::Base
@@ -273,6 +297,15 @@ describe InJson do
273
297
  User.include_in_json(:with_posts).should == {
274
298
  :posts => {}
275
299
  }
300
+
301
+ User.include_in_json(:with_posts_and_all_named).should == {
302
+ :posts => {
303
+ :reviews => {},
304
+ :comments => {
305
+ :votes => {}
306
+ }
307
+ }
308
+ }
276
309
  end
277
310
 
278
311
  it "should use calculated eager-loading includes" do
data/spec/spec_helper.rb CHANGED
@@ -35,6 +35,12 @@ ActiveRecord::Base.connection.create_table :comments do |t|
35
35
  t.integer :post_id
36
36
  end
37
37
 
38
+ ActiveRecord::Base.connection.create_table :comment_votes do |t|
39
+ t.integer :rating
40
+ t.integer :comment_id
41
+ t.integer :user_id
42
+ end
43
+
38
44
  ActiveRecord::Base.connection.create_table :reviews do |t|
39
45
  t.integer :score
40
46
  t.integer :post_id
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: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Henry Hsu