cyrax 0.2.3 → 0.2.4

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
- ZWUxYTQwNmI1YzlmOGRmZWE2OWIwNDMzY2QwYzc1ZWM0ODliYTI2OA==
4
+ NmE3M2RiMzA0YzZkOWJlNjI1MWZmM2I4ZDVkMjNjMzRiYTI1YjVmMw==
5
5
  data.tar.gz: !binary |-
6
- YTEzMGY5MjgxYjVmYzYwODYwZmRmNWJmYWI0NWQ3MjkzNzE2YjcyMg==
6
+ YzM3YTgzM2Q2OTczMjVjNjgxYmQ3NzFkNTE2NjE5ZDBhNzQ4MDk2YQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MzgxYTRmODEwYzNiZjY1YWRhYTlkZDY3ZTdjNTBhY2M5ODY4MmJlOTdlNDAz
10
- MDdkZTYyMDY4ZGQ2ZDg2NzI3MjBjMWE1MWFmNmUxMWZiZWI2ZTFlOWJhN2Yw
11
- ZjhiOTk1NjhjOWRlMzg0MGE1NDE4YzUwNzIxMjExNTJhNDAwOGU=
9
+ YTJhMTMyZTJiM2EzOWVjNGQ0NjEzZjUyNTBlYWIzOGU4NDYzNzRiZDM1NmEz
10
+ YzMwNTZmMGFlMzE1NTVkZmE4NTc2NDQwNDg3N2M5MGVjZWVhYzQwMTEwNWJl
11
+ ODJjODU2OWJhNmEyNjVkZjQzZjEzYmE4MDM1ZDc4YzI2MTNmNGI=
12
12
  data.tar.gz: !binary |-
13
- MWVhYjc5MzIyMjQ0OWExMTE5MTQ4ZTAxMDVmYWZhZjg2NjNkZWZhN2YzMTQ5
14
- MDdiNGJhN2ZmOGEwNzBhZjFmMjg0OWUxMDFlMmUxMWQzMzIwMWQwYzVlNDA1
15
- YzM0ZGNlYmYzMjViODE3MmM1NjI4ZmRiZjA0MjFjYWY3ZGMzZmE=
13
+ MjUyMmFmNzJkZDJkYWEwZWMzNGRiZWQwNDI3NmQwYWEyNzkwYjAzMjFlYjc1
14
+ MWQ4ZDAyOTkxMzEwNTk1NjZjNDdlZGM3MWQ3MzVmNWY5NWUwMDdlNjUzMjU0
15
+ NmRmNjUzOWM5OGZiYzlhM2Y4NzQyYWVmODdlZGEzZjU2ZDQzODE=
@@ -9,4 +9,20 @@ class Cyrax::Base
9
9
  @accessor = options[:as]
10
10
  @params = options[:params]
11
11
  end
12
+
13
+ def has_extension?(name)
14
+ self.class.has_extension?(name)
15
+ end
16
+
17
+ class << self
18
+ def register_extension(name)
19
+ @_extensions ||= []
20
+ @_extensions << name.to_sym
21
+ end
22
+
23
+ def has_extension?(name)
24
+ @_extensions ||= []
25
+ @_extensions.include?(name.to_sym)
26
+ end
27
+ end
12
28
  end
@@ -10,11 +10,9 @@ class Cyrax::BasePresenter
10
10
  end
11
11
 
12
12
  class << self
13
-
14
13
  def present(object, options = {})
15
14
  self.new.present(object, options)
16
15
  end
17
-
18
16
  end
19
17
 
20
18
  private
@@ -6,6 +6,7 @@ module Cyrax::Extensions
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
+ register_extension :has_callbacks
9
10
  class_attribute :resource_callbacks_handler_class
10
11
  end
11
12
 
@@ -3,12 +3,13 @@ module Cyrax::Extensions
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
+ register_extension :has_decorator
6
7
  class_attribute :decorator_class_name
7
8
  end
8
9
 
9
10
  module ClassMethods
10
11
  def decorator(name)
11
- self.decorator_class_name = name.to_s
12
+ self.decorator_class_name = name ? name.to_s : nil
12
13
  end
13
14
  end
14
15
 
@@ -4,6 +4,7 @@ module Cyrax::Extensions
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
+ register_extension :has_resource
7
8
  class_attribute :resource_name
8
9
  class_attribute :resource_class_name
9
10
  end
@@ -46,16 +47,8 @@ module Cyrax::Extensions
46
47
  resource_scope.find(id)
47
48
  end
48
49
 
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
50
+ def response_name
51
+ resource_name
59
52
  end
60
53
 
61
54
  module ClassMethods
@@ -30,11 +30,17 @@ module Cyrax::Extensions
30
30
  @_message = message
31
31
  end
32
32
 
33
- def respond_with(resource, options = {})
34
- name = options[:name] || resource_name
35
- presenter_options = options.merge(decorable: decorable?, decorator_class: decorator_class)
36
- result = Cyrax::BasePresenter.present(resource, presenter_options)
37
- response = Cyrax::Response.new(name, result)
33
+ def response_name
34
+ self.class.name.demodulize.underscore
35
+ end
36
+
37
+ def respond_with(result, options = {})
38
+ name = options[:name] || response_name
39
+ if has_extension?(:has_decorator)
40
+ options.merge!(decorable: decorable?, decorator_class: decorator_class)
41
+ result = Cyrax::BasePresenter.present(result, options)
42
+ end
43
+ response = result.is_a?(Cyrax::Response) ? result : Cyrax::Response.new(name, result)
38
44
  response.message = @_message
39
45
  response.errors = @_errors
40
46
  response.assignments = @_assignments
@@ -2,6 +2,10 @@ module Cyrax::Extensions
2
2
  module HasService
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ included do
6
+ register_extension :has_service
7
+ end
8
+
5
9
  def collection
6
10
  respond_with build_collection, name: collection_name, present: :collection
7
11
  end
@@ -1,3 +1,3 @@
1
1
  module Cyrax
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
@@ -1,41 +1,45 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Cyrax
4
- describe Cyrax::Extensions::HasCallbacks do
4
+ class BaseWithCallbacks < Cyrax::Base
5
5
  include Cyrax::Extensions::HasCallbacks
6
+ end
7
+
8
+ describe Cyrax::Extensions::HasCallbacks do
9
+ subject { BaseWithCallbacks.new }
6
10
 
7
11
  describe '#invoke_callback' do
8
12
  context 'when callback handler class is not defined' do
9
13
  let(:name) { mock }
10
14
  let(:resource) { mock }
11
- before { self.class.stub!(:resource_callbacks_handler_class) }
12
- it { invoke_callback(name, resource).should be_nil }
15
+ before { subject.class.stub!(:resource_callbacks_handler_class) }
16
+ it { subject.invoke_callback(name, resource).should be_nil }
13
17
  end
14
18
 
15
19
  context 'when callback handler class is defined' do
16
20
  let(:name) { "foo" }
17
21
  let(:resource) { mock }
18
22
  let(:handle_class) { double.as_null_object }
19
- before { self.class.stub!(:resource_callbacks_handler_class).and_return(handle_class) }
23
+ before { subject.class.stub!(:resource_callbacks_handler_class).and_return(handle_class) }
20
24
 
21
25
  it 'initializes handle class instance' do
22
26
  handle_class.should_receive(:new).with(resource)
23
- invoke_callback(name, resource)
27
+ subject.invoke_callback(name, resource)
24
28
  end
25
29
 
26
30
  it 'calls corresponding method on instance' do
27
31
  instance = double.as_null_object
28
32
  handle_class.stub!(:new).and_return(instance)
29
33
  instance.should_receive(:foo)
30
- invoke_callback(name, resource)
34
+ subject.invoke_callback(name, resource)
31
35
  end
32
36
  end
33
37
  end
34
38
 
35
39
  describe '#callbacks_handler' do
36
40
  it 'should define callbacks handler class' do
37
- self.class.callbacks_handler('Foo')
38
- self.class.resource_callbacks_handler_class.should eq('Foo')
41
+ subject.class.callbacks_handler('Foo')
42
+ subject.class.resource_callbacks_handler_class.should eq('Foo')
39
43
  end
40
44
  end
41
45
  end
@@ -12,38 +12,39 @@ class Foo;
12
12
  end
13
13
 
14
14
  module Cyrax
15
- describe Cyrax::Extensions::HasDecorator do
15
+ class BaseWithDecorator < Cyrax::Base
16
16
  include Cyrax::Extensions::HasDecorator
17
+ end
18
+
19
+ describe Cyrax::Extensions::HasDecorator do
20
+ subject { BaseWithDecorator.new }
17
21
 
18
22
  describe 'class attributes' do
19
- subject { self.class }
20
- it { should respond_to(:decorator_class_name) }
23
+ its(:class) { should respond_to(:decorator_class_name) }
21
24
  end
22
25
 
23
26
  describe 'class methods' do
24
27
  describe '#decorator' do
25
- before { self.class.decorator(:foo) }
26
- subject { self }
28
+ before { subject.class.decorator(:foo) }
27
29
 
28
30
  its(:decorator_class_name) { should eq('foo') }
29
31
  end
30
32
  end
31
33
 
32
34
  describe 'instance methods' do
33
- subject { self }
34
-
35
35
  describe '#decorator_class' do
36
- before { self.class.decorator(:foo) }
36
+ before { subject.class.decorator(:foo) }
37
37
  its(:decorator_class) { should eq(Foo) }
38
38
  end
39
39
 
40
40
  describe '#decorable?' do
41
41
  context 'when `decorator_class_name` present' do
42
- before { self.class.decorator(:foo) }
42
+ before { subject.class.decorator(:foo) }
43
43
  its(:decorable?) { should be_true }
44
44
  end
45
45
 
46
46
  context 'when `decorator_class_name` empty' do
47
+ before { subject.class.decorator(nil) }
47
48
  its(:decorable?) { should be_false }
48
49
  end
49
50
  end
@@ -10,19 +10,21 @@ class Bar; end
10
10
  class Foo; end
11
11
 
12
12
  module Cyrax
13
- describe Cyrax::Extensions::HasResource do
13
+ class BaseWithResource < Cyrax::Base
14
14
  include Cyrax::Extensions::HasResource
15
+ end
16
+
17
+ describe Cyrax::Extensions::HasResource do
18
+ subject { BaseWithResource.new }
15
19
 
16
20
  describe 'class attributes' do
17
- subject { self.class }
18
- it { should respond_to(:resource_name) }
19
- it { should respond_to(:resource_class_name) }
21
+ its(:class) { should respond_to(:resource_name) }
22
+ its(:class) { should respond_to(:resource_class_name) }
20
23
  end
21
24
 
22
25
  describe 'class methods' do
23
26
  describe '#resource' do
24
- before { self.class.resource(:foo, class_name:'bar', name:'bazz') }
25
- subject { self }
27
+ before { subject.class.resource(:foo, class_name:'bar', name:'bazz') }
26
28
 
27
29
  its(:resource_name) { should eq('foo') }
28
30
  its(:resource_class_name) { should eq('bar') }
@@ -30,26 +32,25 @@ module Cyrax
30
32
  end
31
33
 
32
34
  describe 'instance methods' do
33
- subject { self }
34
35
 
35
36
  describe '#resource_name' do
36
- before { self.class.resource(:foo) }
37
+ before { subject.class.resource(:foo) }
37
38
  its(:resource_name) { should eq('foo') }
38
39
  end
39
40
 
40
41
  describe '#collection_name' do
41
- before { self.class.resource(:foo) }
42
+ before { subject.class.resource(:foo) }
42
43
  its(:collection_name) { should eq('foos') }
43
44
  end
44
45
 
45
46
  describe '#resource_class' do
46
47
  context 'when `class_name` option is supplied' do
47
- before { self.class.resource(:foo, class_name:'Bar') }
48
+ before { subject.class.resource(:foo, class_name:'Bar') }
48
49
  its(:resource_class) { should eq(Bar) }
49
50
  end
50
51
 
51
52
  context 'when `class_name` option is omited' do
52
- before { self.class.resource(:foo) }
53
+ before { subject.class.resource(:foo) }
53
54
  its(:resource_class) { should eq(Foo) }
54
55
  end
55
56
  end
@@ -74,27 +75,6 @@ module Cyrax
74
75
  end
75
76
  end
76
77
 
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
-
98
78
  describe '#build_resource' do
99
79
  context 'when id is nil' do
100
80
  let(:resource_scope) { mock }
@@ -131,7 +111,6 @@ module Cyrax
131
111
  end
132
112
 
133
113
  describe 'private methods' do
134
- subject { self }
135
114
  describe '#dirty_resource_attributes' do
136
115
  context 'when params are present' do
137
116
  it 'should return from params by resource_name' do
@@ -1,10 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Cyrax
4
- describe Cyrax::Extensions::HasResponse do
4
+ class BaseWithResponse < Cyrax::Base
5
5
  include Cyrax::Extensions::HasResponse
6
+ end
6
7
 
7
- subject { self }
8
+ describe Cyrax::Extensions::HasResponse do
9
+ subject { BaseWithResponse.new }
8
10
  before do
9
11
  subject.stub!(:decorable?).and_return(false)
10
12
  subject.stub!(:decorator_class).and_return(nil)
@@ -55,7 +57,7 @@ module Cyrax
55
57
  end
56
58
 
57
59
  describe '#respond_with' do
58
- before { subject.stub!(:resource_name).and_return(:foo) }
60
+ before { subject.stub!(:response_name).and_return(:foo) }
59
61
  it 'calls Cyrax::Response' do
60
62
  Cyrax::Response.should_receive(:new).with(:foo, 'bar').and_return(mock.as_null_object)
61
63
  subject.respond_with('bar')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cyrax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Droidlabs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-09 00:00:00.000000000 Z
11
+ date: 2013-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport