cyrax 0.5.15 → 0.6.0.beta

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.
@@ -5,7 +5,6 @@ module Cyrax::Serializers
5
5
  @dynamic_attrs = {}
6
6
  @relation_attrs = {}
7
7
  @namespace_attrs = {}
8
- @assigned_attrs = {}
9
8
  @default_attributes = false
10
9
  instance_eval(&block) if block_given?
11
10
  end
@@ -14,10 +13,6 @@ module Cyrax::Serializers
14
13
  @namespace_attrs[name] = self.class.new(&block)
15
14
  end
16
15
 
17
- def assigned(name, &block)
18
- @assigned_attrs[name] = self.class.new(&block)
19
- end
20
-
21
16
  def relation(attribute, &block)
22
17
  if block_given?
23
18
  @relation_attrs[attribute] = self.class.new(&block)
@@ -25,8 +20,6 @@ module Cyrax::Serializers
25
20
  @attrs[attribute] = attribute
26
21
  end
27
22
  end
28
- alias_method :has_many, :relation
29
- alias_method :has_one, :relation
30
23
 
31
24
  def default_attributes
32
25
  @default_attributes = true
@@ -47,9 +40,7 @@ module Cyrax::Serializers
47
40
  end
48
41
 
49
42
  def serialize(resource, options = {})
50
- if resource.nil?
51
- nil
52
- elsif resource.respond_to?(:to_a)
43
+ if resource.respond_to?(:to_a)
53
44
  resource.to_a.map{ |r| serialize_one(r, options) }
54
45
  else
55
46
  serialize_one(resource, options)
@@ -62,19 +53,14 @@ module Cyrax::Serializers
62
53
  result = resource.attributes rescue {}
63
54
  end
64
55
  @dynamic_attrs.map do |attribute, block|
65
- result[attribute] = options[:serializer].instance_exec(resource, options, &block)
56
+ result[attribute] = options[:serializer].instance_exec(resource, &block)
66
57
  end
67
58
  @relation_attrs.map do |attribute, scope|
68
59
  value = resource.send(attribute)
69
- result[attribute] = scope.serialize(value, options)
70
- end
71
- @assigned_attrs.map do |attribute, scope|
72
- assignments = options[:assignments] || {}
73
- value = assignments[attribute]
74
- result[attribute] = scope.serialize(value, options)
60
+ result[attribute] = scope.serialize(value)
75
61
  end
76
62
  @namespace_attrs.map do |attribute, scope|
77
- result[attribute] = scope.serialize(resource, options)
63
+ result[attribute] = scope.serialize(resource)
78
64
  end
79
65
  @attrs.map do |attribute, options|
80
66
  result[attribute] = resource.send(attribute)
data/lib/cyrax/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cyrax
2
- VERSION = "0.5.15"
2
+ VERSION = "0.6.0.beta"
3
3
  end
data/lib/cyrax.rb CHANGED
@@ -6,6 +6,7 @@ require "cyrax/extensions/has_response.rb"
6
6
  require "cyrax/extensions/has_service.rb"
7
7
  require "cyrax/extensions/has_decorator.rb"
8
8
  require "cyrax/extensions/has_serializer.rb"
9
+ require "cyrax/extensions/has_repository.rb"
9
10
  require "cyrax/presenters/base_collection.rb"
10
11
  require "cyrax/presenters/decorated_collection.rb"
11
12
  require "cyrax/serializers/scope.rb"
@@ -17,10 +18,10 @@ require "cyrax/presenter.rb"
17
18
  require "cyrax/response.rb"
18
19
  require "cyrax/decorator.rb"
19
20
  require "cyrax/serializer.rb"
21
+ require "cyrax/repository.rb"
20
22
 
21
23
  module Cyrax
22
24
  @@strong_parameters = true
23
- @@automatically_set_invalid_status = true
24
25
 
25
26
  def self.strong_parameters
26
27
  @@strong_parameters
@@ -29,12 +30,4 @@ module Cyrax
29
30
  def self.strong_parameters=(value)
30
31
  @@strong_parameters = value
31
32
  end
32
-
33
- def self.automatically_set_invalid_status
34
- @@automatically_set_invalid_status
35
- end
36
-
37
- def self.automatically_set_invalid_status=(value)
38
- @@automatically_set_invalid_status = value
39
- end
40
33
  end
@@ -20,26 +20,26 @@ module Cyrax
20
20
  subject { BaseWithDecorator.new }
21
21
 
22
22
  describe 'class attributes' do
23
- its(:class) { should respond_to(:decorator_class_name) }
23
+ its(:class) { should respond_to(:_decorator_class) }
24
24
  end
25
25
 
26
26
  describe 'class methods' do
27
27
  describe '#decorator' do
28
- before { subject.class.decorator(:foo) }
28
+ before { subject.class.decorator(Foo) }
29
29
 
30
- its(:decorator_class_name) { should eq('foo') }
30
+ its(:decorator_class) { should eq(Foo) }
31
31
  end
32
32
  end
33
33
 
34
34
  describe 'instance methods' do
35
35
  describe '#decorator_class' do
36
- before { subject.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 { subject.class.decorator(:foo) }
42
+ before { subject.class.decorator(Foo) }
43
43
  its(:decorable?) { should be_true }
44
44
  end
45
45
 
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ class Bar; end
4
+ class Foo; end
5
+
6
+ module Cyrax
7
+ class BaseWithRepository < Cyrax::Base
8
+ include Cyrax::Extensions::HasRepository
9
+ end
10
+
11
+ describe Cyrax::Extensions::HasRepository do
12
+ subject { BaseWithRepository.new }
13
+
14
+ describe 'class attributes' do
15
+ its(:class) { should respond_to(:_repository_class) }
16
+ end
17
+
18
+ describe 'class methods' do
19
+ describe '#repository' do
20
+ context "define repository class" do
21
+ before { subject.class.repository(Foo) }
22
+
23
+ its(:repository_class) { should eq(Foo) }
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -18,16 +18,17 @@ module Cyrax
18
18
  subject { BaseWithResource.new }
19
19
 
20
20
  describe 'class attributes' do
21
- its(:class) { should respond_to(:resource_name) }
22
- its(:class) { should respond_to(:resource_class_name) }
21
+ its(:class) { should respond_to(:_resource_name) }
22
+ its(:class) { should respond_to(:_collection_name) }
23
+ its(:class) { should respond_to(:_resource_class) }
23
24
  end
24
25
 
25
26
  describe 'class methods' do
26
27
  describe '#resource' do
27
- before { subject.class.resource(:foo, class_name:'bar', name:'bazz') }
28
+ before { subject.class.resource(:foo, class: Bar, name:'bazz') }
28
29
 
29
30
  its(:resource_name) { should eq('foo') }
30
- its(:resource_class_name) { should eq('bar') }
31
+ its(:resource_class) { should eq(Bar) }
31
32
  end
32
33
  end
33
34
 
@@ -45,7 +46,7 @@ module Cyrax
45
46
 
46
47
  describe '#resource_class' do
47
48
  context 'when `class_name` option is supplied' do
48
- before { subject.class.resource(:foo, class_name:'Bar') }
49
+ before { subject.class.resource(:foo, class: Bar) }
49
50
  its(:resource_class) { should eq(Bar) }
50
51
  end
51
52
 
@@ -55,11 +56,6 @@ module Cyrax
55
56
  end
56
57
  end
57
58
 
58
- describe '#resource_scope' do
59
- before { subject.stub(:resource_class).and_return(Foo) }
60
- its(:resource_scope) { should eq(Foo) }
61
- end
62
-
63
59
  describe '#resource_attributes' do
64
60
  let(:dirty_resource_attributes) { double }
65
61
  before { subject.stub(:dirty_resource_attributes).and_return(dirty_resource_attributes)}
@@ -88,11 +84,6 @@ module Cyrax
88
84
  end
89
85
  end
90
86
  end
91
- describe '#default_resource_attributes' do
92
- it 'should return empty hash by default' do
93
- subject.send(:default_resource_attributes).should eq({})
94
- end
95
- end
96
87
 
97
88
  describe '#filter_attributes' do
98
89
  it 'should return supplied attributes by default' do
@@ -59,7 +59,7 @@ module Cyrax
59
59
  describe '#respond_with' do
60
60
  before { subject.stub(:response_name).and_return(:foo) }
61
61
  it 'calls Cyrax::Response' do
62
- Cyrax::Response.should_receive(:new).with(:foo, 'bar', {as: nil, assignments: nil}).and_return(double.as_null_object)
62
+ Cyrax::Response.should_receive(:new).with(:foo, 'bar', {as: nil}).and_return(double.as_null_object)
63
63
  subject.respond_with('bar')
64
64
  end
65
65
 
@@ -16,48 +16,5 @@ module Cyrax
16
16
 
17
17
  describe Cyrax::Extensions::HasResource do
18
18
  subject { BaseWithService.new }
19
-
20
- describe 'instance methods' do
21
- describe '#build_collection' do
22
- before { subject.stub(:resource_scope).and_return(Foo) }
23
- its(:build_collection) { should eq(Foo) }
24
- end
25
-
26
- describe '#find_resource' do
27
- let(:resource_scope) { double }
28
- before { subject.stub(:resource_scope).and_return(resource_scope) }
29
- it 'finds resource by id' do
30
- allow_message_expectations_on_nil
31
- resource_scope.should_receive(:find).with(123)
32
- subject.find_resource(123)
33
- end
34
- end
35
-
36
- describe '#build_resource' do
37
- context 'when id is nil' do
38
- let(:resource_scope) { double }
39
- before { subject.stub(:resource_scope).and_return(resource_scope) }
40
- before { subject.stub(:default_resource_attributes).and_return({}) }
41
- it 'initializes new object' do
42
- allow_message_expectations_on_nil
43
- resource_scope.should_receive(:new).with({foo: 'bar'})
44
- subject.build_resource(nil, {foo: 'bar'})
45
- end
46
- end
47
- context 'when id is present' do
48
- let(:resource) { double.as_null_object }
49
- it 'finds resource' do
50
- subject.should_receive(:find_resource).with(123).and_return(resource)
51
- subject.build_resource(123, {foo: 'bar'})
52
- end
53
-
54
- it 'assigns provided attributes' do
55
- subject.stub(:find_resource).and_return(resource)
56
- resource.should_receive(:attributes=).with({foo: 'bar'})
57
- subject.build_resource(123, {foo: 'bar'})
58
- end
59
- end
60
- end
61
- end
62
19
  end
63
20
  end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ class Foo; end
4
+ module Cyrax
5
+ describe Repository do
6
+ describe 'it should behave like repository' do
7
+ context 'instance methods' do
8
+ subject { Cyrax::Repository.new }
9
+ it{ should respond_to(:resource_class) }
10
+ it{ should respond_to(:scope) }
11
+ it{ should respond_to(:build) }
12
+ it{ should respond_to(:find_all) }
13
+ it{ should respond_to(:find) }
14
+ it{ should respond_to(:save) }
15
+ it{ should respond_to(:delete) }
16
+ end
17
+ end
18
+
19
+ describe 'instance methods' do
20
+ describe '#find_all' do
21
+ let(:scope) { double }
22
+ before { subject.stub(:scope).and_return(scope) }
23
+ it 'finds all objects by default' do
24
+ scope.should_receive(:all)
25
+ subject.find_all
26
+ end
27
+ end
28
+
29
+ describe '#find_resource' do
30
+ let(:scope) { double }
31
+ before { subject.stub(:scope).and_return(scope) }
32
+ it 'finds resource by id' do
33
+ allow_message_expectations_on_nil
34
+ scope.should_receive(:find).with(123)
35
+ subject.find(123)
36
+ end
37
+ end
38
+
39
+ describe '#build_resource' do
40
+ context 'when id is nil' do
41
+ let(:scope) { double }
42
+ before { subject.stub(:scope).and_return(scope) }
43
+ before { subject.stub(:default_attributes).and_return({}) }
44
+ it 'initializes new object' do
45
+ allow_message_expectations_on_nil
46
+ scope.should_receive(:new).with({foo: 'bar'})
47
+ subject.build(nil, {foo: 'bar'})
48
+ end
49
+ end
50
+ context 'when id is present' do
51
+ let(:resource) { double.as_null_object }
52
+ it 'finds resource' do
53
+ subject.should_receive(:find).with(123).and_return(resource)
54
+ subject.build(123, {foo: 'bar'})
55
+ end
56
+
57
+ it 'assigns provided attributes' do
58
+ subject.stub(:find).and_return(resource)
59
+ resource.should_receive(:attributes=).with({foo: 'bar'})
60
+ subject.build(123, {foo: 'bar'})
61
+ end
62
+ end
63
+ end
64
+
65
+ describe '#default_attributes' do
66
+ it 'should return empty hash by default' do
67
+ subject.send(:default_attributes).should eq({})
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -10,11 +10,7 @@ module Cyrax
10
10
  it{ should respond_to(:params) }
11
11
  it{ should respond_to(:resource_name) }
12
12
  it{ should respond_to(:resource_class) }
13
- it{ should respond_to(:resource_scope) }
14
13
  it{ should respond_to(:resource_attributes) }
15
- it{ should respond_to(:build_resource) }
16
- it{ should respond_to(:build_collection) }
17
- it{ should respond_to(:find_resource) }
18
14
  it{ should respond_to(:assign_resource) }
19
15
  it{ should respond_to(:respond_with) }
20
16
  it{ should respond_to(:set_message) }
@@ -25,17 +21,19 @@ module Cyrax
25
21
  end
26
22
 
27
23
  subject { Cyrax::Resource.new }
24
+ let(:repository) { double }
28
25
  let(:resource) { double.as_null_object }
29
26
  let(:collection) { [double] }
30
27
  before do
31
28
  subject.stub(:params).and_return({id:123})
32
- subject.stub(:find_resource).and_return(resource)
29
+ subject.stub(:repository).and_return(repository)
30
+ repository.stub(:find).and_return(resource)
33
31
  subject.class.send :resource, :foo
34
32
  end
35
33
 
36
34
  describe '#read_all' do
37
35
  it 'responds with decorated collection' do
38
- subject.should_receive(:build_collection).and_return(collection)
36
+ repository.should_receive(:find_all).and_return(collection)
39
37
  subject.should_receive(:respond_with).with(collection, name: 'foos', present: :collection)
40
38
  subject.read_all
41
39
  end
@@ -43,7 +41,7 @@ module Cyrax
43
41
 
44
42
  describe '#build' do
45
43
  it 'responds with resource' do
46
- subject.should_receive(:build_resource).with(nil).and_return(resource)
44
+ repository.should_receive(:build).with(nil).and_return(resource)
47
45
  subject.should_receive(:respond_with).with(resource)
48
46
  subject.build
49
47
  end
@@ -51,7 +49,7 @@ module Cyrax
51
49
 
52
50
  describe '#edit' do
53
51
  it 'responds with resource' do
54
- subject.should_receive(:find_resource)
52
+ repository.should_receive(:find)
55
53
  subject.should_receive(:respond_with).with(resource)
56
54
  subject.edit
57
55
  end
@@ -59,36 +57,33 @@ module Cyrax
59
57
 
60
58
  describe '#read' do
61
59
  it 'responds with resource' do
62
- subject.should_receive(:find_resource)
60
+ repository.should_receive(:find)
63
61
  subject.should_receive(:respond_with).with(resource)
64
62
  subject.read
65
63
  end
66
64
  end
67
65
 
68
66
  describe '#destroy' do
69
- it 'responds with resource' do
70
- subject.should_receive(:find_resource)
67
+ it 'destroys resource and responds with resource' do
68
+ repository.should_receive(:find)
69
+ repository.should_receive(:delete)
71
70
  subject.should_receive(:respond_with).with(resource)
72
71
  subject.destroy
73
72
  end
74
-
75
- it 'destroys resource' do
76
- resource.should_receive(:destroy)
77
- subject.destroy
78
- end
79
73
  end
80
74
 
81
75
  describe '#create' do
82
76
  let(:params) { {foo: 'bar'} }
83
- before { subject.stub(:build_resource).and_return(resource) }
77
+ before { repository.stub(:build).and_return(resource) }
84
78
  it 'responds with resource' do
85
- subject.should_receive(:build_resource).with(nil, params)
79
+ repository.should_receive(:save).with(resource)
80
+ repository.should_receive(:build).with(nil, params)
86
81
  subject.should_receive(:respond_with).with(resource)
87
82
  subject.create(params)
88
83
  end
89
84
 
90
85
  context 'when resource successfully saved' do
91
- before { resource.stub(:save).and_return(true) }
86
+ before { repository.stub(:save).and_return(true) }
92
87
 
93
88
  it 'sets message' do
94
89
  subject.should_receive(:set_message).with(:created)
@@ -97,31 +92,27 @@ module Cyrax
97
92
  end
98
93
 
99
94
  context 'when resource could not be saved' do
100
- before { resource.stub(:save).and_return(false) }
95
+ before { repository.stub(:save).and_return(false) }
101
96
 
102
97
  it 'does not set message' do
103
98
  subject.should_not_receive(:set_message).with(:created)
104
99
  subject.create(params)
105
100
  end
106
-
107
- it "sets 422 status" do
108
- subject.should_receive(:set_status).with(422)
109
- subject.create(params)
110
- end
111
101
  end
112
102
  end
113
103
 
114
104
  describe '#update' do
115
105
  let(:params) { {foo: 'bar'} }
116
- before { subject.stub(:build_resource).and_return(resource) }
106
+ before { repository.stub(:build).and_return(resource) }
117
107
  it 'responds with resource' do
118
- subject.should_receive(:build_resource).with(123, params)
108
+ repository.should_receive(:build).with(123, params)
109
+ repository.should_receive(:save).with(resource)
119
110
  subject.should_receive(:respond_with).with(resource)
120
111
  subject.update(params)
121
112
  end
122
113
 
123
114
  context 'when resource successfully saved' do
124
- before { resource.stub(:save).and_return(true) }
115
+ before { repository.stub(:save).and_return(true) }
125
116
 
126
117
  it 'sets message' do
127
118
  subject.should_receive(:set_message).with(:updated)
@@ -130,16 +121,11 @@ module Cyrax
130
121
  end
131
122
 
132
123
  context 'when resource could not be saved' do
133
- before { resource.stub(:save).and_return(false) }
124
+ before { repository.stub(:save).and_return(false) }
134
125
 
135
126
  it 'does not set message' do
136
127
  subject.should_not_receive(:set_message).with(:created)
137
- subject.update(params)
138
- end
139
-
140
- it "sets 422 status" do
141
- subject.should_receive(:set_status).with(422)
142
- subject.update(params)
128
+ subject.create(params)
143
129
  end
144
130
  end
145
131
  end
@@ -8,25 +8,16 @@ class FooSerializer < Cyrax::Serializer
8
8
  relation :another do
9
9
  attributes :another_foo, :another_bar
10
10
  end
11
- relation :empty_relation_many do
12
- attributes :another_foo, :another_bar
13
- end
14
- relation :empty_relation_one do
15
- attributes :another_foo, :another_bar
16
- end
17
11
  end
18
12
 
19
13
  module Cyrax
20
14
  describe FooSerializer do
21
- let(:serializable) {
15
+ let(:serializable) {
22
16
  double(
23
17
  name: 'me',
24
18
  foo: '2342',
25
19
  bar: '4223',
26
- another: double(another_foo: '1234', another_bar: '1234'),
27
- empty_relation_one: nil,
28
- empty_relation_many: []
29
- )
20
+ another: double(another_foo: '1234', another_bar: '1234'))
30
21
  }
31
22
  subject { FooSerializer.new(serializable).serialize }
32
23
 
@@ -34,8 +25,6 @@ module Cyrax
34
25
  subject[:name].should eq('me')
35
26
  subject[:some][:foo].should eq('2342')
36
27
  subject[:another][:another_foo].should eq('1234')
37
- subject[:empty_relation_one].should eq(nil)
38
- subject[:empty_relation_many].should eq([])
39
28
  end
40
29
  end
41
30
  end