api_resource 0.6.2 → 0.6.3

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/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- api_resource (0.6.1)
4
+ api_resource (0.6.2)
5
5
  colorize
6
6
  differ
7
7
  json
@@ -39,12 +39,14 @@ GEM
39
39
  activesupport (3.2.10)
40
40
  i18n (~> 0.6)
41
41
  multi_json (~> 1.0)
42
+ archive-tar-minitar (0.5.2)
42
43
  arel (3.0.2)
43
44
  builder (3.0.4)
44
45
  childprocess (0.3.6)
45
46
  ffi (~> 1.0, >= 1.0.6)
46
47
  coderay (1.0.8)
47
48
  colorize (0.5.8)
49
+ columnize (0.3.6)
48
50
  diff-lcs (1.1.3)
49
51
  differ (0.1.2)
50
52
  erubis (2.7.0)
@@ -77,10 +79,12 @@ GEM
77
79
  activesupport
78
80
  colorize
79
81
  i18n
80
- hike (1.2.1)
82
+ hike (1.2.2)
81
83
  i18n (0.6.1)
82
84
  journey (1.0.4)
83
- json (1.7.6)
85
+ json (1.7.7)
86
+ linecache19 (0.5.12)
87
+ ruby_core_source (>= 0.1.4)
84
88
  listen (0.7.1)
85
89
  log4r (1.1.10)
86
90
  lumberjack (1.0.2)
@@ -90,7 +94,7 @@ GEM
90
94
  treetop (~> 1.4.8)
91
95
  metaclass (0.0.1)
92
96
  method_source (0.8.1)
93
- mime-types (1.19)
97
+ mime-types (1.22)
94
98
  mocha (0.12.7)
95
99
  metaclass (~> 0.0.1)
96
100
  multi_json (1.5.0)
@@ -99,10 +103,10 @@ GEM
99
103
  coderay (~> 1.0.5)
100
104
  method_source (~> 0.8)
101
105
  slop (~> 3.3.1)
102
- rack (1.4.3)
106
+ rack (1.4.5)
103
107
  rack-cache (1.2)
104
108
  rack (>= 0.4)
105
- rack-ssl (1.3.2)
109
+ rack-ssl (1.3.3)
106
110
  rack
107
111
  rack-test (0.6.2)
108
112
  rack (>= 1.0)
@@ -123,7 +127,7 @@ GEM
123
127
  thor (>= 0.14.6, < 2.0)
124
128
  rake (10.0.3)
125
129
  rb-fsevent (0.9.3)
126
- rdoc (3.12)
130
+ rdoc (3.12.2)
127
131
  json (~> 1.4)
128
132
  rest-client (1.6.7)
129
133
  mime-types (>= 1.16)
@@ -135,6 +139,16 @@ GEM
135
139
  rspec-expectations (2.12.1)
136
140
  diff-lcs (~> 1.1.3)
137
141
  rspec-mocks (2.12.1)
142
+ ruby-debug-base19 (0.11.25)
143
+ columnize (>= 0.3.1)
144
+ linecache19 (>= 0.5.11)
145
+ ruby_core_source (>= 0.1.4)
146
+ ruby-debug19 (0.11.6)
147
+ columnize (>= 0.3.1)
148
+ linecache19 (>= 0.5.11)
149
+ ruby-debug-base19 (>= 0.11.19)
150
+ ruby_core_source (0.1.5)
151
+ archive-tar-minitar (>= 0.5.2)
138
152
  ruby_parser (3.1.1)
139
153
  sexp_processor (~> 4.1)
140
154
  sexp_processor (4.1.3)
@@ -151,11 +165,11 @@ GEM
151
165
  tilt (~> 1.1, != 1.3.0)
152
166
  sqlite3 (1.3.6)
153
167
  thor (0.16.0)
154
- tilt (1.3.3)
168
+ tilt (1.3.7)
155
169
  treetop (1.4.12)
156
170
  polyglot
157
171
  polyglot (>= 0.3.1)
158
- tzinfo (0.3.35)
172
+ tzinfo (0.3.37)
159
173
 
160
174
  PLATFORMS
161
175
  ruby
@@ -174,6 +188,7 @@ DEPENDENCIES
174
188
  rake
175
189
  rb-fsevent
176
190
  rspec
191
+ ruby-debug19
177
192
  simplecov
178
193
  spork
179
194
  sqlite3
data/api_resource.gemspec CHANGED
@@ -31,6 +31,7 @@ Gem::Specification.new do |gem|
31
31
  gem.add_development_dependency "flog"
32
32
  gem.add_development_dependency "hash_dealer"
33
33
  gem.add_development_dependency "rb-fsevent"
34
+ gem.add_development_dependency "ruby-debug19"
34
35
  gem.add_development_dependency "simplecov"
35
36
  # stuff that seems like crap
36
37
  gem.add_development_dependency "sqlite3"
@@ -144,7 +144,7 @@ module ApiResource
144
144
 
145
145
  # TODO: add a special foreign_key option to associations
146
146
  def association_foreign_key_field(assoc, type = nil)
147
-
147
+
148
148
  if type.nil? && has_many?(assoc)
149
149
  type = :has_many
150
150
  else
@@ -154,7 +154,7 @@ module ApiResource
154
154
  # for now just use the association name
155
155
  str = assoc.to_s.singularize.foreign_key
156
156
 
157
- if type == :has_many
157
+ if type.to_s =~ /^has_many/
158
158
  str = str.pluralize
159
159
  end
160
160
 
@@ -228,10 +228,12 @@ module ApiResource
228
228
 
229
229
  def #{id_method_name}
230
230
  @attributes_cache[:#{id_method_name}] ||= begin
231
- if @attributes.key?(:#{id_method_name})
232
- @attributes[:#{id_method_name}]
233
- else
231
+ if @attributes.has_key?("#{id_method_name}")
232
+ @attributes["#{id_method_name}"]
233
+ elsif self.#{assoc_name}.collection?
234
234
  self.#{assoc_name}.collect(&:id)
235
+ else
236
+ self.#{assoc_name}? ? self.#{assoc_name}.id : nil
235
237
  end
236
238
  end
237
239
  end
@@ -241,6 +243,7 @@ module ApiResource
241
243
  #{id_method_name}_will_change!
242
244
  end
243
245
  @attributes_cache[:#{id_method_name}] = val
246
+ write_attribute(:#{id_method_name}, val)
244
247
  end
245
248
 
246
249
  EOE
@@ -16,6 +16,10 @@ module ApiResource
16
16
  self.internal_object
17
17
  end
18
18
 
19
+ def collection?
20
+ true
21
+ end
22
+
19
23
  def each(*args, &block)
20
24
  self.internal_object.each(*args, &block)
21
25
  end
@@ -9,6 +9,10 @@ module ApiResource
9
9
  self.internal_object.serializable_hash(options)
10
10
  end
11
11
 
12
+ def collection?
13
+ false
14
+ end
15
+
12
16
  def internal_object
13
17
  unless instance_variable_defined?(:@internal_object)
14
18
  if self.remote_path.present?
@@ -152,6 +152,9 @@ module ApiResource
152
152
 
153
153
  def load_resource_definition
154
154
  unless instance_variable_defined?(:@resource_definition)
155
+ # the last time we checked
156
+ @resource_load_time = Time.now
157
+
155
158
  # set to not nil so we don't get an infinite loop
156
159
  @resource_definition = true
157
160
  self.set_class_attributes_upon_load
@@ -354,12 +357,28 @@ module ApiResource
354
357
  end
355
358
 
356
359
  protected
360
+
361
+ # do we have an invalid resource
362
+ def resource_definition_is_invalid?
363
+ # if we have a Hash, it's valid
364
+ return false if @resource_definition.is_a?(Hash)
365
+ # if we have no recheck time, it's invalid
366
+ return true if @resource_load_time.nil?
367
+ # have we checked in the last minute?
368
+ return @resource_load_time < Time.now - 1.minute
369
+ end
370
+
357
371
  def method_missing(meth, *args, &block)
358
372
  # make one attempt to load remote attrs
359
- if self.load_resource_definition
373
+ if self.resource_definition_is_invalid?
374
+ self.reload_resource_definition
375
+ end
376
+ # see if we respond to the method now
377
+ if self.respond_to?(meth)
360
378
  return self.send(meth, *args, &block)
379
+ else
380
+ super
361
381
  end
362
- super
363
382
  end
364
383
 
365
384
  private
@@ -663,7 +682,7 @@ module ApiResource
663
682
  include AssociationActivation
664
683
  self.activate_associations
665
684
 
666
- include Scopes, Callbacks, Observing, Attributes, ModelErrors, Conditions, Finders
685
+ include Scopes, Callbacks, Observing, Attributes, ModelErrors, Conditions, Finders, Typecast
667
686
 
668
687
  end
669
688
 
@@ -1,3 +1,3 @@
1
1
  module ApiResource
2
- VERSION = "0.6.2"
2
+ VERSION = "0.6.3"
3
3
  end
@@ -6,6 +6,16 @@ describe ApiResource::Associations::AssociationProxy do
6
6
  TestResource.reload_resource_definition
7
7
  end
8
8
 
9
+ context "Methods that collide with enumerables" do
10
+
11
+ it "should not include enumerables" do
12
+ test_resource = TestResource.find(1)
13
+ # the #zip method is included in enumerable
14
+ test_resource.belongs_to_object.zip.should_not be_blank
15
+ end
16
+
17
+ end
18
+
9
19
  context "#remote_path" do
10
20
 
11
21
  it "should provide access to its remote path when
@@ -804,6 +804,8 @@ describe "Associations" do
804
804
  before(:all) do
805
805
  TestAR.class_eval do
806
806
  has_one_remote :test_resource
807
+ has_one_remote :other_test_resource,
808
+ :class_name => "TestResource"
807
809
  end
808
810
  end
809
811
  it "should attempt to load a single remote object for a has_one relationship" do
@@ -815,6 +817,27 @@ describe "Associations" do
815
817
  # load the test resource
816
818
  tar.test_resource.name.should eql "testing"
817
819
  end
820
+
821
+ it "should use its object's primary_key as the _id method" do
822
+ tar = TestAR.new
823
+ tar.stubs(:id).returns(1)
824
+ TestResource.connection.expects(:get)
825
+ .with("/test_resources.json?test_ar_id=1")
826
+ .returns([{"name" => "testing", "id" => 14}])
827
+ # load the test resource
828
+ tar.other_test_resource_id.should eql(14)
829
+ end
830
+
831
+ it "should handle mising objects in its _id method" do
832
+ tar = TestAR.new
833
+ tar.stubs(:id).returns(1)
834
+ TestResource.connection.expects(:get)
835
+ .with("/test_resources.json?test_ar_id=1")
836
+ .returns([])
837
+ # load the test resource
838
+ tar.other_test_resource_id.should be_nil
839
+ end
840
+
818
841
  end
819
842
  context "Has Many" do
820
843
  before(:all) do
@@ -826,9 +849,12 @@ describe "Associations" do
826
849
  tar = TestAR.new
827
850
  tar.stubs(:id).returns(1)
828
851
  HasManyObject.load_resource_definition
829
- HasManyObject.connection.expects(:get).with("/has_many_objects.json?test_ar_id=1").once.returns([{"name" => "testing"}])
852
+ HasManyObject.connection.expects(:get).with("/has_many_objects.json?test_ar_id=1").once.returns(
853
+ [{"name" => "testing", "id" => 22}]
854
+ )
830
855
  # load the test resource
831
856
  tar.has_many_objects.first.name.should eql "testing"
857
+ tar.has_many_object_ids.should eql([22])
832
858
  end
833
859
  end
834
860
  context "Has Many Through" do
data/spec/spec_helper.rb CHANGED
@@ -14,6 +14,9 @@ Spork.prefork do
14
14
  require 'api_resource'
15
15
  require 'simplecov'
16
16
 
17
+ require 'ruby-debug'
18
+ Debugger.start
19
+
17
20
 
18
21
  SimpleCov.start do
19
22
  add_filter "/spec/"
@@ -49,8 +49,17 @@ Mocks.define do
49
49
  end
50
50
 
51
51
  endpoint("/belongs_to_objects/new") do
52
- get({})
52
+ get({
53
+ "attributes" => {
54
+ "public" => ["zip"]
55
+ }
56
+ })
57
+ end
58
+
59
+ endpoint("/belongs_to_objects/:id") do
60
+ get(HashDealer.roll(:belongs_to_object_response))
53
61
  end
62
+
54
63
 
55
64
  endpoint("/test_associations/new") do
56
65
  get({})
@@ -29,3 +29,7 @@ end
29
29
  HashDealer.define(:has_many_object) do
30
30
  name("name")
31
31
  end
32
+
33
+ HashDealer.define(:belongs_to_object_response) do
34
+ zip "11201"
35
+ end
@@ -40,6 +40,7 @@ HashDealer.define(:test_resource_with_proxies, :parent => :test_resource) do
40
40
  has_one_object{HashDealer.roll(:has_one_object)}
41
41
  has_many_objects{(0..4).to_a.collect{HashDealer.roll(:has_many_object)}}
42
42
  has_many_service_uri{[{:service_uri => "/test_resource/1/has_many"}]}
43
+ belongs_to_object({:service_uri => "/belongs_to_objects/4"})
43
44
  end
44
45
 
45
46
  HashDealer.define(:test_resource_errors) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-01-07 00:00:00.000000000 Z
14
+ date: 2013-04-12 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rake
@@ -221,6 +221,22 @@ dependencies:
221
221
  - - ! '>='
222
222
  - !ruby/object:Gem::Version
223
223
  version: '0'
224
+ - !ruby/object:Gem::Dependency
225
+ name: ruby-debug19
226
+ requirement: !ruby/object:Gem::Requirement
227
+ none: false
228
+ requirements:
229
+ - - ! '>='
230
+ - !ruby/object:Gem::Version
231
+ version: '0'
232
+ type: :development
233
+ prerelease: false
234
+ version_requirements: !ruby/object:Gem::Requirement
235
+ none: false
236
+ requirements:
237
+ - - ! '>='
238
+ - !ruby/object:Gem::Version
239
+ version: '0'
224
240
  - !ruby/object:Gem::Dependency
225
241
  name: simplecov
226
242
  requirement: !ruby/object:Gem::Requirement
@@ -477,7 +493,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
477
493
  version: '0'
478
494
  requirements: []
479
495
  rubyforge_project:
480
- rubygems_version: 1.8.23
496
+ rubygems_version: 1.8.24
481
497
  signing_key:
482
498
  specification_version: 3
483
499
  summary: ActiveRecord for restful APIs