active_remote 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,6 +3,7 @@
3
3
  .DS_Store
4
4
  .bundle
5
5
  .config
6
+ .rvmrc
6
7
  .yardoc
7
8
  Gemfile.lock
8
9
  coverage
data/Gemfile CHANGED
@@ -1,7 +1,5 @@
1
1
  source 'http://gems.moneydesktop.com'
2
2
  source 'https://rubygems.org'
3
3
 
4
- gem 'builder', '~> 3.0.0' # Geminabox will to go to 3.1.3, which causes problems with ActiveModel.
5
-
6
4
  # Specify your gem's dependencies in active_remote.gemspec
7
5
  gemspec
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 MDev
1
+ Copyright (c) 2012 Adam Hutchison
2
2
 
3
3
  MIT License
4
4
 
@@ -19,7 +19,6 @@ Gem::Specification.new do |s|
19
19
  ##
20
20
  # Dependencies
21
21
  #
22
- s.add_dependency "activemodel"
23
22
  s.add_dependency "active_attr"
24
23
  s.add_dependency "protobuf", ">= 2.0"
25
24
 
@@ -37,20 +37,9 @@ module ActiveRemote
37
37
  # end
38
38
  # end
39
39
  #
40
- def belongs_to(*klass_names)
41
- klass_names.flatten.compact.uniq.each do |klass_name|
42
-
43
- define_method(klass_name) do
44
- value = instance_variable_get(:"@#{klass_name}")
45
-
46
- unless value
47
- klass = klass_name.to_s.classify.constantize
48
- value = klass.search(:guid => read_attribute(:"#{klass_name}_guid")).first
49
- instance_variable_set(:"@#{klass_name}", value)
50
- end
51
-
52
- return value
53
- end
40
+ def belongs_to(belongs_to_klass, options={})
41
+ perform_association( belongs_to_klass, options ) do |klass, obj|
42
+ klass.search(:guid => obj.read_attribute(:"#{belongs_to_klass}_guid")).first
54
43
  end
55
44
  end
56
45
 
@@ -84,21 +73,9 @@ module ActiveRemote
84
73
  # end
85
74
  # end
86
75
  #
87
- def has_many(*klass_names)
88
- klass_names.flatten.compact.uniq.each do |plural_klass_name|
89
- singular_name = plural_klass_name.to_s.singularize
90
-
91
- define_method(plural_klass_name) do
92
- values = instance_variable_get(:"@#{plural_klass_name}")
93
-
94
- unless values
95
- klass = plural_klass_name.to_s.classify.constantize
96
- values = klass.search(:"#{self.class.name.demodulize.underscore}_guid" => self.guid)
97
- instance_variable_set(:"@#{plural_klass_name}", values)
98
- end
99
-
100
- return values
101
- end
76
+ def has_many(has_many_class, options={})
77
+ perform_association( has_many_class, options ) do |klass, obj|
78
+ klass.search(:"#{obj.class.name.demodulize.underscore}_guid" => obj.guid)
102
79
  end
103
80
  end
104
81
 
@@ -131,20 +108,26 @@ module ActiveRemote
131
108
  # end
132
109
  # end
133
110
  #
134
- def has_one(*klass_names)
135
- klass_names.flatten.compact.uniq.each do |klass_name|
111
+ def has_one(has_one_klass, options={})
112
+ perform_association( has_one_klass, options ) do |klass, obj|
113
+ klass.search(:"#{obj.class.name.demodulize.underscore}_guid" => obj.guid).first
114
+ end
115
+ end
136
116
 
137
- define_method(klass_name) do
138
- value = instance_variable_get(:"@#{klass_name}")
117
+ private
139
118
 
140
- unless value
141
- klass = klass_name.to_s.classify.constantize
142
- value = klass.search(:"#{self.class.name.demodulize.underscore}_guid" => self.guid).first
143
- instance_variable_set(:"@#{klass_name}", value)
144
- end
119
+ def perform_association(associated_klass, optionz={})
120
+ define_method(associated_klass) do
121
+ value = instance_variable_get(:"@#{associated_klass}")
145
122
 
146
- return value
123
+ unless value
124
+ klass_name = optionz.fetch(:class_name){ associated_klass }
125
+ klass = klass_name.to_s.classify.constantize
126
+ value = yield( klass, self )
127
+ instance_variable_set(:"@#{associated_klass}", value)
147
128
  end
129
+
130
+ return value
148
131
  end
149
132
  end
150
133
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRemote
2
- VERSION = "1.2.1"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -5,27 +5,43 @@ describe ActiveRemote::Association do
5
5
  let(:records) { [ record ] }
6
6
 
7
7
  describe '.belongs_to' do
8
- let(:author_guid) { 'AUT-123' }
9
8
 
10
- subject { Post.new(:author_guid => author_guid) }
11
- it { should respond_to(:author) }
9
+ context 'simple association' do
10
+ let(:author_guid) { 'AUT-123' }
12
11
 
13
- it 'searches the associated model for a single record' do
14
- Author.should_receive(:search).with(:guid => subject.author_guid).and_return(records)
15
- subject.author.should eq record
16
- end
12
+ subject { Post.new(:author_guid => author_guid) }
13
+ it { should respond_to(:author) }
17
14
 
18
- it 'memoizes the result record' do
19
- Author.should_receive(:search).once.with(:guid => subject.author_guid).and_return(records)
20
- 3.times { subject.author.should eq record }
15
+ it 'searches the associated model for a single record' do
16
+ Author.should_receive(:search).with(:guid => subject.author_guid).and_return(records)
17
+ subject.author.should eq record
18
+ end
19
+
20
+ it 'memorizes the result record' do
21
+ Author.should_receive(:search).once.with(:guid => subject.author_guid).and_return(records)
22
+ 3.times { subject.author.should eq record }
23
+ end
24
+
25
+ context 'when the search is empty' do
26
+ it 'returns a nil' do
27
+ Author.should_receive(:search).with(:guid => subject.author_guid).and_return([])
28
+ subject.author.should be_nil
29
+ end
30
+ end
21
31
  end
22
32
 
23
- context 'when the search is empty' do
24
- it 'returns a nil' do
25
- Author.should_receive(:search).with(:guid => subject.author_guid).and_return([])
26
- subject.author.should be_nil
33
+ context 'specific association with class name' do
34
+ let(:author_guid) { 'AUT-456' }
35
+
36
+ subject { Post.new(:author_guid => author_guid) }
37
+ it { should respond_to(:coauthor) }
38
+
39
+ it 'searches the associated model for a single record' do
40
+ Author.should_receive(:search).with(:guid => subject.author_guid).and_return(records)
41
+ subject.author.should eq record
27
42
  end
28
43
  end
44
+
29
45
  end
30
46
 
31
47
  describe '.has_many' do
@@ -51,6 +67,15 @@ describe ActiveRemote::Association do
51
67
  subject.posts.should be_empty
52
68
  end
53
69
  end
70
+
71
+ context 'specific association with class name' do
72
+ it { should respond_to(:flagged_posts) }
73
+
74
+ it 'searches the associated model for a single record' do
75
+ Post.should_receive(:search).with(:author_guid => subject.guid).and_return([])
76
+ subject.flagged_posts.should be_empty
77
+ end
78
+ end
54
79
  end
55
80
 
56
81
  describe '.has_one' do
@@ -75,6 +100,15 @@ describe ActiveRemote::Association do
75
100
  subject.category.should be_nil
76
101
  end
77
102
  end
103
+
104
+ context 'specific association with class name' do
105
+ it { should respond_to(:main_category) }
106
+
107
+ it 'searches the associated model for a single record' do
108
+ Category.should_receive(:search).with(:post_guid => subject.guid).and_return(records)
109
+ subject.main_category.should eq record
110
+ end
111
+ end
78
112
  end
79
113
 
80
114
  end
@@ -10,5 +10,6 @@ class Author < ::ActiveRemote::Base
10
10
  attribute :name
11
11
 
12
12
  has_many :posts
13
+ has_many :flagged_posts, :class_name => "::Post"
13
14
 
14
15
  end
@@ -11,6 +11,8 @@ class Post < ::ActiveRemote::Base
11
11
  attribute :author_guid
12
12
 
13
13
  belongs_to :author
14
+ belongs_to :coauthor, :class_name => ::Author
14
15
  has_one :category
16
+ has_one :main_category, :class_name => ::Category
15
17
 
16
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_remote
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-16 00:00:00.000000000 Z
12
+ date: 2013-01-09 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: activemodel
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
14
  - !ruby/object:Gem::Dependency
31
15
  name: active_attr
32
16
  requirement: !ruby/object:Gem::Requirement
@@ -166,7 +150,6 @@ extra_rdoc_files: []
166
150
  files:
167
151
  - .gitignore
168
152
  - .rspec
169
- - .rvmrc
170
153
  - Gemfile
171
154
  - LICENSE
172
155
  - README.md
@@ -235,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
235
218
  version: '0'
236
219
  segments:
237
220
  - 0
238
- hash: -4325357550838090592
221
+ hash: 1289251465950014823
239
222
  required_rubygems_version: !ruby/object:Gem::Requirement
240
223
  none: false
241
224
  requirements:
@@ -244,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
244
227
  version: '0'
245
228
  segments:
246
229
  - 0
247
- hash: -4325357550838090592
230
+ hash: 1289251465950014823
248
231
  requirements: []
249
232
  rubyforge_project:
250
233
  rubygems_version: 1.8.24
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm use 1.9.3@active_remote --create