cyrax 0.1.2 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MTI5NjkzYjE1MjUwZTQzZDVjYzBjZDJmMTc4MWQ4N2Y3NTk0YzNkNA==
4
+ ZmYzYjFhZDU3NzM5ZTA5MjI4YThiN2Q5ZWRjZGEyYmQ4OGY2MTdlZQ==
5
5
  data.tar.gz: !binary |-
6
- YmQ4YWUwYjIwMTFjZDdkNTQyOTljYWJiMjQ0MmExYTVkNjg1OTE3Nw==
6
+ OWIxZTMxOTI2MjI0ZWE4ZTFjNzI1ZGQ0NjE5M2VhMGU4MGE4NGI1ZA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- M2E0NmE3MmExMjc5M2Y3MTg3NDZmNzI3MDljYzhkZDY2NmE2MDVmMmZhODc0
10
- Nzc3YmNhYmMzNGVlNjk4NDQ5ZTYxNmJiYmY1Yjg2NTgzNmVkZDU1Zjc4ZDI5
11
- MDdhMDVjZWIyMDkwMjQ2M2VmZGEyN2EyNWZmY2FjMzE4NmZiNGU=
9
+ YzQzY2IxNWJhYThmNDEzMWE5NGRkZTMzYmNlYzY1N2I4MDYzNGYzMzRjZjI1
10
+ Mzg2NGMzOWI5NTYyYjc1Zjk5MDhiM2NlNzc2OTRkMDYwYTNhM2M3M2Q0OWNm
11
+ ZmYwZWEzM2Q5ZDk5MjFmMGViOTUzNDdkZjNlNTgyOGJkZWM4NTE=
12
12
  data.tar.gz: !binary |-
13
- N2YyNDI2NDgzYzRkNmM1NWQzM2VkOTNhOWM3NjIyODAzZDQ1YTgyNjYyYTk5
14
- MGIyODZhOGViNTliMmQyOWIxZDc5ZjJjNzQxNmMxYzI3ZWEyY2E0MjRjMTNh
15
- NmEwMDc1NjA2ZTc1ZGUwYzA4MTQ3NmVhYjc2M2U0NTRhMmE0OWQ=
13
+ MGM4NGI3YmFjZjM4MWZhMTYyNjdmMTNmODc1NGJmNTcwODg1NjFjODRjZWQ4
14
+ N2ZmMDVmOGNlZDgzZjI4ZDc0ZDg2ZTkxODA4Zjc4ZDZkNzVhMThkZDc5MmVh
15
+ NTE1ZGJlYjVmNTU0YzkwYTJlNGQ3MmQ0MzdiNjE2ZWU3YWI0NWY=
@@ -22,4 +22,21 @@ class Cyrax::Decorator
22
22
 
23
23
  resource.send(method, *args, &block)
24
24
  end
25
+
26
+ class << self
27
+ def fetch(resource)
28
+ resource.respond_to?(:all) ? resource.all : resource
29
+ end
30
+
31
+ def decorate(resource)
32
+ resource = fetch(resource)
33
+ if resource.is_a?(Array)
34
+ resource.map do |item|
35
+ self.new(item)
36
+ end
37
+ else
38
+ self.new(resource)
39
+ end
40
+ end
41
+ end
25
42
  end
@@ -12,10 +12,6 @@ module Cyrax::Extensions
12
12
  end
13
13
  end
14
14
 
15
- def decorated_collection
16
- decorable? ? build_decorated_collection : wrapped_collection
17
- end
18
-
19
15
  private
20
16
 
21
17
  def decorable?
@@ -25,21 +21,5 @@ module Cyrax::Extensions
25
21
  def decorator_class
26
22
  self.class.decorator_class_name.to_s.classify.constantize
27
23
  end
28
-
29
- def wrapped_collection
30
- collection = build_collection
31
-
32
- if collection.kind_of?(Array)
33
- return collection
34
- elsif collection.respond_to?(:all)
35
- return collection.all
36
- else
37
- return Array.wrap(collection)
38
- end
39
- end
40
-
41
- def build_decorated_collection
42
- wrapped_collection.map{|i| decorator_class.new(i)}
43
- end
44
24
  end
45
25
  end
@@ -46,6 +46,18 @@ module Cyrax::Extensions
46
46
  resource_scope.find(id)
47
47
  end
48
48
 
49
+ def wrapped_collection
50
+ collection = build_collection
51
+
52
+ if collection.kind_of?(Array)
53
+ return collection
54
+ elsif collection.respond_to?(:all)
55
+ return collection.all
56
+ else
57
+ return Array.wrap(collection)
58
+ end
59
+ end
60
+
49
61
  module ClassMethods
50
62
  def accessible_attributes(*attrs)
51
63
  if attrs.blank?
@@ -8,8 +8,8 @@ module Cyrax::Extensions
8
8
  end
9
9
 
10
10
  def assign_resource(resource_name, resource, options = {})
11
+ resource = options[:decorator].decorate(resource) if options[:decorator]
11
12
  @_assignments ||= {}
12
- resource = options[:decorator].new(resource) if options[:decorator]
13
13
  @_assignments[resource_name.to_sym] = resource
14
14
  end
15
15
 
@@ -33,7 +33,7 @@ module Cyrax::Extensions
33
33
  def respond_with(resource, options = {})
34
34
  name = options[:name] || resource_name
35
35
  should_decorate = options[:decorate].nil? || options[:decorate]
36
- result = decorable? && should_decorate ? decorator_class.new(resource) : resource
36
+ result = decorable? && should_decorate ? decorator_class.decorate(resource) : resource
37
37
  response = Cyrax::Response.new(name, result)
38
38
  response.message = @_message
39
39
  response.errors = @_errors
@@ -3,7 +3,7 @@ module Cyrax::Extensions
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  def collection
6
- respond_with decorated_collection, name: collection_name
6
+ respond_with wrapped_collection, name: collection_name
7
7
  end
8
8
 
9
9
  def build
@@ -1,3 +1,3 @@
1
1
  module Cyrax
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -47,56 +47,6 @@ module Cyrax
47
47
  its(:decorable?) { should be_false }
48
48
  end
49
49
  end
50
-
51
- describe '#decorated_collection' do
52
- context 'when `decorable?`' do
53
- before { subject.stub!(:decorable?).and_return(true) }
54
- it 'should return decorated result' do
55
- subject.should_receive(:build_decorated_collection)
56
- subject.decorated_collection
57
- end
58
- end
59
- context 'when not `decorable?`' do
60
- before { subject.stub!(:decorable?).and_return(false) }
61
- it 'should return raw result' do
62
- subject.should_not_receive(:build_decorated_collection)
63
- subject.should_receive(:build_collection)
64
- subject.decorated_collection
65
- end
66
- end
67
- end
68
-
69
- describe '#build_decorated_collection' do
70
- before do
71
- self.class.decorator(:foo)
72
- subject.stub!(:wrapped_collection).and_return([:bar])
73
- end
74
-
75
- it 'returns array of decorator instances' do
76
- subject.send(:build_decorated_collection).should eq([Foo.new(:bar)])
77
- end
78
- end
79
-
80
- describe '#prepare_collection_for_decorate' do
81
- subject { self.send(:wrapped_collection) }
82
-
83
- context 'when #build_collection returns array' do
84
- before { self.stub!(:build_collection).and_return([:bar]) }
85
- it { should eq([:bar]) }
86
- end
87
-
88
- context 'when #build_collection returns any object except array' do
89
- before { self.stub!(:build_collection).and_return(:bar) }
90
- it { should eq([:bar]) }
91
- end
92
-
93
- context 'when #build_collection returns any object which responds to #all method' do
94
- let(:obj) { mock(:all => [:bar] ) }
95
- before { self.stub!(:build_collection).and_return(obj) }
96
-
97
- it { should eq([:bar]) }
98
- end
99
- end
100
50
  end
101
51
  end
102
52
  end
@@ -74,6 +74,27 @@ module Cyrax
74
74
  end
75
75
  end
76
76
 
77
+ describe '#wrapped_collection' do
78
+ subject { self.send(:wrapped_collection) }
79
+
80
+ context 'when #build_collection returns array' do
81
+ before { self.stub!(:build_collection).and_return([:bar]) }
82
+ it { should eq([:bar]) }
83
+ end
84
+
85
+ context 'when #build_collection returns any object except array' do
86
+ before { self.stub!(:build_collection).and_return(:bar) }
87
+ it { should eq([:bar]) }
88
+ end
89
+
90
+ context 'when #build_collection returns any object which responds to #all method' do
91
+ let(:obj) { mock(:all => [:bar] ) }
92
+ before { self.stub!(:build_collection).and_return(obj) }
93
+
94
+ it { should eq([:bar]) }
95
+ end
96
+ end
97
+
77
98
  describe '#build_resource' do
78
99
  context 'when id is nil' do
79
100
  let(:resource_scope) { mock }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cyrax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Droidlabs