spyke 1.8.5 → 1.8.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cad85c078caa849ff351e6f096085b23bc3aa447
4
- data.tar.gz: 8a28b48d15894f136d3b95f67c071fd2b9e2989e
3
+ metadata.gz: 21babce7ddc3d045a984d9efc4501ac4b811f497
4
+ data.tar.gz: bb6a369931ec88d716aed6fe2831928ac9f42e60
5
5
  SHA512:
6
- metadata.gz: e17e259553eb5310cac7194d4711047486b26a1021da2ea43c2fa5a8813b0b0a57fed1c7411232fb8e33670560572878f05e1b9b366f94b213b47ef25d580c22
7
- data.tar.gz: 6deb4d9939c5e818c4d29287024b794e8dba9c2be7abb61d53bedd7db740281a771c25a7e3c9e3cbe867d7d32539192419d83d93fb1a5f378c83ce19c52c2067
6
+ metadata.gz: c298b3f6569a9a0367129b54b76d3b1ccc042b3ddc0f829c1f0ef279666aaa84e576b3427fc63006cc5cf542ebda1e38a4ce890f6c177a031b4d0a63c3664d3e
7
+ data.tar.gz: bfd3b3b44721671a22be2c9732917b495d710a4504d4eccd892c88b81790f1370a4e6f73092f6e3afe9a89ae5a692af2dd50b1ee8dadab3a3d8af9b5e4caddf8
@@ -1,4 +1,5 @@
1
1
  require 'spyke/associations/association'
2
+ require 'spyke/associations/builder'
2
3
  require 'spyke/associations/has_many'
3
4
  require 'spyke/associations/has_one'
4
5
  require 'spyke/associations/belongs_to'
@@ -14,7 +15,7 @@ module Spyke
14
15
 
15
16
  module ClassMethods
16
17
  def has_many(name, options = {})
17
- self.associations = associations.merge(name => options.merge(type: HasMany))
18
+ create_association(name, HasMany, options)
18
19
 
19
20
  define_method "#{name.to_s.singularize}_ids=" do |ids|
20
21
  attributes[name] = []
@@ -27,7 +28,7 @@ module Spyke
27
28
  end
28
29
 
29
30
  def has_one(name, options = {})
30
- self.associations = associations.merge(name => options.merge(type: HasOne))
31
+ create_association(name, HasOne, options)
31
32
 
32
33
  define_method "build_#{name}" do |attributes = nil|
33
34
  association(name).build(attributes)
@@ -35,7 +36,7 @@ module Spyke
35
36
  end
36
37
 
37
38
  def belongs_to(name, options = {})
38
- self.associations = associations.merge(name => options.merge(type: BelongsTo))
39
+ create_association(name, BelongsTo, options)
39
40
 
40
41
  define_method "build_#{name}" do |attributes = nil|
41
42
  association(name).build(attributes)
@@ -54,9 +55,14 @@ module Spyke
54
55
 
55
56
  def reflect_on_association(name)
56
57
  # Just enough to support nested_form gem
57
- assoc = associations[name] || associations[name.to_s.pluralize.to_sym]
58
- Association.new(nil, name, assoc)
58
+ associations[name] || associations[name.to_s.pluralize.to_sym]
59
59
  end
60
+
61
+ private
62
+
63
+ def create_association(name, type, options)
64
+ self.associations = associations.merge(name => Builder.new(self, name, type, options))
65
+ end
60
66
  end
61
67
  end
62
68
  end
@@ -6,8 +6,8 @@ module Spyke
6
6
  class Association < Relation
7
7
  attr_reader :parent, :name
8
8
 
9
- def initialize(parent, name, options = {})
10
- super (options[:class_name] || name.to_s).classify.constantize, options
9
+ def initialize(klass, parent, name, options = {})
10
+ super(klass, options)
11
11
  @parent, @name = parent, name
12
12
  end
13
13
 
@@ -0,0 +1,35 @@
1
+ module Spyke
2
+ module Associations
3
+ class Builder
4
+ def initialize(parent_class, name, type, options = {})
5
+ @parent_class, @name, @type, @options = parent_class, name, type, options
6
+ end
7
+
8
+ def build(parent)
9
+ @type.new(klass, parent, @name, @options)
10
+ end
11
+
12
+ def klass
13
+ @klass ||= compute_class(@options[:class_name] || @name)
14
+ end
15
+
16
+ private
17
+
18
+ # https://github.com/rails/rails/blob/70ac072976c8cc6f013f0df3777e54ccae3f4f8c/activerecord/lib/active_record/inheritance.rb#L132-L150
19
+ def compute_class(type_name)
20
+ parent_name = @parent_class.to_s
21
+ type_name = type_name.to_s.classify
22
+
23
+ candidates = []
24
+ parent_name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }
25
+ candidates << type_name
26
+
27
+ candidates.each do |candidate|
28
+ constant = ActiveSupport::Dependencies.safe_constantize(candidate)
29
+ return constant if candidate == constant.to_s
30
+ end
31
+ raise NameError.new("uninitialized constant #{candidates.first}", candidates.first)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -73,8 +73,7 @@ module Spyke
73
73
  end
74
74
 
75
75
  def association(name)
76
- options = associations[name]
77
- options[:type].new(self, name, options)
76
+ associations[name].build(self)
78
77
  end
79
78
 
80
79
  def attribute?(name)
@@ -4,8 +4,8 @@ module Spyke
4
4
  class Relation
5
5
  include Enumerable
6
6
 
7
- attr_reader :klass, :params
8
- attr_writer :params
7
+ attr_reader :klass
8
+ attr_accessor :params
9
9
  delegate :to_ary, :[], :any?, :empty?, :last, :size, :metadata, to: :find_some
10
10
 
11
11
  def initialize(klass, options = {})
data/lib/spyke/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Spyke
2
- VERSION = '1.8.5'
2
+ VERSION = '1.8.6'
3
3
  end
@@ -370,5 +370,24 @@ module Spyke
370
370
  assert_equal %w{ Condiments Tools }, recipe.groups.map(&:name)
371
371
  assert_equal %w{ Salt Spoon }, recipe.ingredients.map(&:name)
372
372
  end
373
+
374
+ def test_namespaced_model
375
+ tip_endpoint = stub_request(:get, 'http://sushi.com/tips/1').to_return_json(result: { id: 1 })
376
+ likes_endpoint = stub_request(:get, 'http://sushi.com/tips/1/likes')
377
+ Cookbook::Tip.find(1).likes.first
378
+ assert_requested tip_endpoint
379
+ assert_requested likes_endpoint
380
+ end
381
+
382
+ def test_namespaced_association_class_auto_detect
383
+ favorite = Cookbook::Tip.new.favorites.build
384
+ assert_equal Cookbook::Favorite, favorite.class
385
+ end
386
+
387
+ def test_raising_exception_if_class_not_found
388
+ assert_raises NameError do
389
+ Cookbook::Tip.new.votes
390
+ end
391
+ end
373
392
  end
374
393
  end
@@ -124,18 +124,16 @@ module Spyke
124
124
  assert_requested endpoint_2, times: 1
125
125
  end
126
126
 
127
- def test_namespaced_model
128
- tip_endpoint = stub_request(:get, 'http://sushi.com/tips/1').to_return_json(result: { id: 1 })
129
- likes_endpoint = stub_request(:get, 'http://sushi.com/tips/1/likes')
130
- Cookbook::Tip.find(1).likes.first
131
- assert_requested tip_endpoint
132
- assert_requested likes_endpoint
133
- end
134
-
135
127
  def test_path_validation
136
128
  assert_raises Spyke::InvalidPathError do
137
129
  Recipe.new.groups.to_a
138
130
  end
139
131
  end
132
+
133
+ def test_raise_no_method_error
134
+ assert_raises NoMethodError do
135
+ Recipe.new.groups.unknown_method
136
+ end
137
+ end
140
138
  end
141
139
  end
@@ -94,8 +94,13 @@ module Cookbook
94
94
  class Tip < Spyke::Base
95
95
  uri '/tips/(:id)'
96
96
  has_many :likes, class_name: 'Cookbook::Like', uri: '/tips/:cookbook_tip_id/likes/(:id)'
97
+ has_many :favorites
98
+ has_many :votes
97
99
  end
98
100
 
99
101
  class Like < Spyke::Base
100
102
  end
103
+
104
+ class Favorite < Spyke::Base
105
+ end
101
106
  end
data/test/test_helper.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  # Coverage
2
2
  require 'coveralls'
3
3
  Coveralls.wear!
4
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
5
+ SimpleCov.start do
6
+ add_filter 'test'
7
+ end
4
8
 
5
9
  require 'spyke'
6
10
  require 'faraday_middleware'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spyke
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.5
4
+ version: 1.8.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Balvig
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-19 00:00:00.000000000 Z
11
+ date: 2015-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -268,6 +268,7 @@ files:
268
268
  - lib/spyke/associations.rb
269
269
  - lib/spyke/associations/association.rb
270
270
  - lib/spyke/associations/belongs_to.rb
271
+ - lib/spyke/associations/builder.rb
271
272
  - lib/spyke/associations/has_many.rb
272
273
  - lib/spyke/associations/has_one.rb
273
274
  - lib/spyke/attribute_assignment.rb