cyrax 0.2.12 → 0.3.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/cyrax.rb +21 -4
- data/lib/cyrax/decorator.rb +1 -1
- data/lib/cyrax/extensions/has_response.rb +6 -4
- data/lib/cyrax/extensions/has_serializer.rb +23 -0
- data/lib/cyrax/extensions/has_service.rb +35 -19
- data/lib/cyrax/helpers/controller.rb +28 -0
- data/lib/cyrax/{base_presenter.rb → presenter.rb} +4 -5
- data/lib/cyrax/presenters/base_collection.rb +36 -0
- data/lib/cyrax/presenters/decorated_collection.rb +24 -0
- data/lib/cyrax/{base_resource.rb → resource.rb} +2 -1
- data/lib/cyrax/serializer.rb +28 -0
- data/lib/cyrax/serializers/scope.rb +35 -0
- data/lib/cyrax/version.rb +1 -1
- data/spec/cyrax/{base_resource_spec.rb → resource_spec.rb} +3 -3
- data/spec/cyrax/serializer_spec.rb +20 -0
- metadata +16 -10
- data/lib/cyrax/base_collection_presenter.rb +0 -17
- data/lib/cyrax/decorated_collection_presenter.rb +0 -31
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
N2ExMDYyODcwNjBlZDU0M2VhZmJjMmMzODhmZTY4OGY0NTBlNTc0NQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MGEzNzgzYjhmOWFiMDc2M2JiN2ZhOTJlYTNjOWRlMGRkOWE3N2FlZg==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MWJjMGJlYTM4YTQ4ODZhZjlkNWRjY2Y0ZjAzN2FlNjBmNmMxNTcyNzJkZWQ4
|
10
|
+
ODY4NGIzZTk4NzhjNWY5ZjI0MDM1MGM3NTc4MzU4MWY0MTc0MWU4NmQxMzQx
|
11
|
+
ZmRiNDUzYWMyM2U2OGRlYzA5ZjNmMDg3NTY4Y2E1NjEwZWJhYTU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NjhmY2I2ZjczZjM3NzhhNTYyMTJhYTU1ZWRiZmZlN2M3ZmU0MDU1YjNhMjk3
|
14
|
+
YjUxZjRhOTU1ZTIxM2YxZjY0YTIyYzY3ZmFkYjE3NGY4M2NiODg2OTZhYTIx
|
15
|
+
NDEwNjk1YWU5NjYzZjI4NGNmMWVlNTM1ZDEyMGQwYjUxMmFjMDI=
|
data/lib/cyrax.rb
CHANGED
@@ -4,14 +4,18 @@ require "cyrax/extensions/has_response.rb"
|
|
4
4
|
require "cyrax/extensions/has_callbacks.rb"
|
5
5
|
require "cyrax/extensions/has_service.rb"
|
6
6
|
require "cyrax/extensions/has_decorator.rb"
|
7
|
+
require "cyrax/extensions/has_serializer.rb"
|
8
|
+
require "cyrax/presenters/base_collection.rb"
|
9
|
+
require "cyrax/presenters/decorated_collection.rb"
|
10
|
+
require "cyrax/serializers/scope.rb"
|
11
|
+
require "cyrax/helpers/controller.rb"
|
7
12
|
require "cyrax/base.rb"
|
8
|
-
require "cyrax/
|
9
|
-
require "cyrax/
|
13
|
+
require "cyrax/resource.rb"
|
14
|
+
require "cyrax/presenter.rb"
|
10
15
|
require "cyrax/response.rb"
|
11
16
|
require "cyrax/callbacks.rb"
|
12
|
-
require "cyrax/base_collection_presenter.rb"
|
13
|
-
require "cyrax/decorated_collection_presenter.rb"
|
14
17
|
require "cyrax/decorator.rb"
|
18
|
+
require "cyrax/serializer.rb"
|
15
19
|
|
16
20
|
module Cyrax
|
17
21
|
@@strong_parameters = false
|
@@ -23,4 +27,17 @@ module Cyrax
|
|
23
27
|
def self.strong_parameters=(value)
|
24
28
|
@@strong_parameters = value
|
25
29
|
end
|
30
|
+
|
31
|
+
def self.const_missing(const_name)
|
32
|
+
case const_name
|
33
|
+
when :BaseResource
|
34
|
+
warn "`Cyrax::BaseResource` has been deprecated. Use `Cyrax::Resource` instead."
|
35
|
+
Cyrax::Resource
|
36
|
+
when :DecoratedCollectionPresenter
|
37
|
+
warn "`Cyrax:DecoratedCollectionPresenter` has been deprecated. Use `Cyrax::Presenters::DecoratedCollection` instead."
|
38
|
+
Cyrax::Presenters::DecoratedCollection
|
39
|
+
else
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
26
43
|
end
|
data/lib/cyrax/decorator.rb
CHANGED
@@ -31,7 +31,7 @@ class Cyrax::Decorator
|
|
31
31
|
alias_method :decorate, :new
|
32
32
|
|
33
33
|
def decorate_collection(resource)
|
34
|
-
Cyrax::
|
34
|
+
Cyrax::Presenters::DecoratedCollection.decorate(resource, decorator: self)
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
@@ -9,7 +9,6 @@ module Cyrax::Extensions
|
|
9
9
|
|
10
10
|
def assign_resource(resource_name, resource, options = {})
|
11
11
|
if options[:decorator]
|
12
|
-
options.merge!(decorable: true)
|
13
12
|
resource = Cyrax::BasePresenter.present(resource, options)
|
14
13
|
end
|
15
14
|
@_assignments ||= {}
|
@@ -40,10 +39,13 @@ module Cyrax::Extensions
|
|
40
39
|
def respond_with(result, options = {})
|
41
40
|
name = options[:name] || response_name
|
42
41
|
result = result.result.to_model if result.is_a?(Cyrax::Response)
|
43
|
-
if respond_to?(:decorable?)
|
44
|
-
options
|
45
|
-
result = Cyrax::BasePresenter.present(result, options)
|
42
|
+
if respond_to?(:decorable?) && decorable?
|
43
|
+
options = {decorator: decorator_class}.merge(options)
|
46
44
|
end
|
45
|
+
if respond_to?(:seializable?) && seializable?
|
46
|
+
options = {serializer: serializer_class}.merge(options)
|
47
|
+
end
|
48
|
+
result = Cyrax::Presenter.present(result, options)
|
47
49
|
response = Cyrax::Response.new(name, result)
|
48
50
|
response.message = @_message
|
49
51
|
response.errors = @_errors
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Cyrax::Extensions
|
2
|
+
module HasSerializer
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class_attribute :serializer_class_name
|
7
|
+
end
|
8
|
+
|
9
|
+
def seializable?
|
10
|
+
!self.class.serializer_class_name.nil?
|
11
|
+
end
|
12
|
+
|
13
|
+
def serializer_class
|
14
|
+
self.class.serializer_class_name.to_s.classify.constantize
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def serializer(name)
|
19
|
+
self.serializer_class_name = name ? name.to_s : nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -13,14 +13,16 @@ module Cyrax::Extensions
|
|
13
13
|
|
14
14
|
def create(custom_attributes=nil)
|
15
15
|
resource = build_resource(nil, custom_attributes||resource_attributes)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
16
|
+
transaction do
|
17
|
+
invoke_callback(:before_create, resource)
|
18
|
+
invoke_callback(:before_save, resource)
|
19
|
+
if save_resource(resource)
|
20
|
+
invoke_callback(:after_create, resource)
|
21
|
+
invoke_callback(:after_save, resource)
|
22
|
+
set_message("#{resource_name.titleize} successfully created")
|
23
|
+
else
|
24
|
+
add_errors_from(resource)
|
25
|
+
end
|
24
26
|
end
|
25
27
|
respond_with(resource)
|
26
28
|
end
|
@@ -33,23 +35,27 @@ module Cyrax::Extensions
|
|
33
35
|
|
34
36
|
def update(custom_attributes=nil)
|
35
37
|
resource = build_resource(params[:id], custom_attributes||resource_attributes)
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
38
|
+
transaction do
|
39
|
+
invoke_callback(:before_update, resource)
|
40
|
+
invoke_callback(:before_save, resource)
|
41
|
+
if save_resource(resource)
|
42
|
+
invoke_callback(:after_update, resource)
|
43
|
+
invoke_callback(:after_save, resource)
|
44
|
+
set_message("#{resource_name.titleize} successfully updated")
|
45
|
+
else
|
46
|
+
add_errors_from(resource)
|
47
|
+
end
|
44
48
|
end
|
45
49
|
respond_with(resource)
|
46
50
|
end
|
47
51
|
|
48
52
|
def destroy
|
49
53
|
resource = find_resource(params[:id])
|
50
|
-
|
51
|
-
|
52
|
-
|
54
|
+
transaction do
|
55
|
+
invoke_callback(:before_destroy, resource)
|
56
|
+
delete_resource(resource)
|
57
|
+
invoke_callback(:after_destroy, resource)
|
58
|
+
end
|
53
59
|
respond_with(resource)
|
54
60
|
end
|
55
61
|
|
@@ -78,5 +84,15 @@ module Cyrax::Extensions
|
|
78
84
|
def build_collection
|
79
85
|
resource_scope
|
80
86
|
end
|
87
|
+
|
88
|
+
def transaction(&block)
|
89
|
+
if defined?(ActiveRecord::Base)
|
90
|
+
ActiveRecord::Base.transaction do
|
91
|
+
block.call
|
92
|
+
end
|
93
|
+
else
|
94
|
+
block.call
|
95
|
+
end
|
96
|
+
end
|
81
97
|
end
|
82
98
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Cyrax::ControllerHelper
|
2
|
+
def respond_with(*args)
|
3
|
+
if args.present? && args.first.is_a?(Cyrax::Response)
|
4
|
+
response, options = *args
|
5
|
+
default_options = {
|
6
|
+
notice: response.notice,
|
7
|
+
error: response.error
|
8
|
+
}
|
9
|
+
set_resource_from(response)
|
10
|
+
flash[:notice] = response.notice if response.notice.present?
|
11
|
+
|
12
|
+
# convert result to model if possible
|
13
|
+
result = response.result
|
14
|
+
result = result.to_model if result.respond_to?(:to_model)
|
15
|
+
|
16
|
+
super(result, default_options.merge(options || {}))
|
17
|
+
else
|
18
|
+
super(*args)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def set_resource_from(response)
|
23
|
+
instance_variable_set("@#{response.resource_name}", response.result)
|
24
|
+
response.assignments.each do |key, value|
|
25
|
+
instance_variable_set("@#{key}", value)
|
26
|
+
end if response.assignments.present? && response.assignments.is_a?(Hash)
|
27
|
+
end
|
28
|
+
end
|
@@ -1,8 +1,7 @@
|
|
1
|
-
class Cyrax::
|
2
|
-
|
1
|
+
class Cyrax::Presenter
|
3
2
|
def present(object, options = {})
|
4
3
|
should_decorate = options[:decorate].nil? || options[:decorate]
|
5
|
-
if options[:
|
4
|
+
if options[:decorator] && should_decorate
|
6
5
|
present_with_decoration(object, options)
|
7
6
|
else
|
8
7
|
present_without_decoration(object, options)
|
@@ -19,7 +18,7 @@ class Cyrax::BasePresenter
|
|
19
18
|
|
20
19
|
def present_with_decoration(object, options)
|
21
20
|
if options[:present] == :collection
|
22
|
-
|
21
|
+
Cyrax::Presenters::DecoratedCollection.new(object, options)
|
23
22
|
else
|
24
23
|
options[:decorator].decorate(object)
|
25
24
|
end
|
@@ -27,7 +26,7 @@ class Cyrax::BasePresenter
|
|
27
26
|
|
28
27
|
def present_without_decoration(object, options)
|
29
28
|
if options[:present] == :collection
|
30
|
-
Cyrax::
|
29
|
+
Cyrax::Presenters::BaseCollection.new(object, options)
|
31
30
|
else
|
32
31
|
object
|
33
32
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Cyrax::Presenters
|
2
|
+
class BaseCollection
|
3
|
+
attr_reader :collection, :options
|
4
|
+
|
5
|
+
def initialize(collection, options = {})
|
6
|
+
@collection = collection
|
7
|
+
@options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
array_methods = Array.instance_methods - Object.instance_methods
|
11
|
+
delegate *array_methods, to: :presented_collection
|
12
|
+
|
13
|
+
def presented_collection
|
14
|
+
if collection.is_a?(ActiveRecord::Relation)
|
15
|
+
collection.to_a
|
16
|
+
elsif collection.respond_to?(:all)
|
17
|
+
collection.all
|
18
|
+
else
|
19
|
+
Array.wrap collection
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def as_json
|
24
|
+
if options[:serializer]
|
25
|
+
options[:serializer].new(presented_collection).serialize
|
26
|
+
else
|
27
|
+
presented_collection.as_json
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def method_missing(method, *args, &block)
|
32
|
+
return super unless collection.respond_to?(method)
|
33
|
+
collection.send(method, *args, &block)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Cyrax::Presenters
|
2
|
+
class DecoratedCollection < Cyrax::Presenters::BaseCollection
|
3
|
+
def initialize(collection, options = {})
|
4
|
+
super
|
5
|
+
unless options[:decorator]
|
6
|
+
raise "Decorator is not defined! Please define it with option :decorator"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def presented_collection
|
11
|
+
@decorated_collection ||= super.map {|item| decorate_item(item)}
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.decorate(*args)
|
15
|
+
new(*args).presented_collection
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def decorate_item(item)
|
21
|
+
options[:decorator].new(item)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
|
-
class Cyrax::
|
1
|
+
class Cyrax::Resource < Cyrax::Base
|
2
2
|
include Cyrax::Extensions::HasResource
|
3
3
|
include Cyrax::Extensions::HasCallbacks
|
4
4
|
include Cyrax::Extensions::HasService
|
5
5
|
include Cyrax::Extensions::HasDecorator
|
6
|
+
include Cyrax::Extensions::HasSerializer
|
6
7
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class Cyrax::Serializer
|
2
|
+
attr_accessor :object
|
3
|
+
def initialize(object)
|
4
|
+
@object = object
|
5
|
+
end
|
6
|
+
|
7
|
+
def serialize
|
8
|
+
self.class.scope.serialize(object)
|
9
|
+
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def scope
|
13
|
+
@scope ||= Cyrax::Serializers::Scope.new()
|
14
|
+
end
|
15
|
+
|
16
|
+
def namespace(name, &block)
|
17
|
+
scope.namespace(name, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def attributes(*attrs)
|
21
|
+
scope.attributes(*attrs)
|
22
|
+
end
|
23
|
+
|
24
|
+
def attribute(attribute, options = {})
|
25
|
+
scope.attribute(attribute, options)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Cyrax::Serializers
|
2
|
+
class Scope
|
3
|
+
def initialize(&block)
|
4
|
+
@attrs = {}
|
5
|
+
instance_eval(&block) if block_given?
|
6
|
+
end
|
7
|
+
|
8
|
+
def namespace(name, &block)
|
9
|
+
@attrs[name] = self.class.new(&block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def attributes(*attrs)
|
13
|
+
attrs.map do |attribute|
|
14
|
+
attribute(attribute)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def attribute(attribute, options = {})
|
19
|
+
@attrs ||= {}
|
20
|
+
@attrs[attribute] ||= attribute
|
21
|
+
end
|
22
|
+
|
23
|
+
def serialize(object)
|
24
|
+
result = {}
|
25
|
+
@attrs.map do |attribute, options|
|
26
|
+
value = object.send(attribute)
|
27
|
+
if options.is_a?(Cyrax::Serializers::Scope)
|
28
|
+
value = options.serialize(value)
|
29
|
+
end
|
30
|
+
result[attribute] = value
|
31
|
+
end
|
32
|
+
result
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/cyrax/version.rb
CHANGED
@@ -2,10 +2,10 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
class Foo; end
|
4
4
|
module Cyrax
|
5
|
-
describe
|
5
|
+
describe Resource do
|
6
6
|
describe 'it should behave like resource' do
|
7
7
|
context 'instance methods' do
|
8
|
-
subject { Cyrax::
|
8
|
+
subject { Cyrax::Resource.new }
|
9
9
|
it{ should respond_to(:accessor) }
|
10
10
|
it{ should respond_to(:params) }
|
11
11
|
it{ should respond_to(:resource_name) }
|
@@ -25,7 +25,7 @@ module Cyrax
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
subject { Cyrax::
|
28
|
+
subject { Cyrax::Resource.new }
|
29
29
|
let(:resource) { double.as_null_object }
|
30
30
|
let(:collection) { [double] }
|
31
31
|
before do
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class FooSerializer < Cyrax::Serializer
|
4
|
+
attribute :name
|
5
|
+
namespace :some do
|
6
|
+
attributes :foo, :bar
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Cyrax
|
11
|
+
describe FooSerializer do
|
12
|
+
let(:serializable) { double(name: 'me', some: double(foo: '1234', bar: '1234')) }
|
13
|
+
subject { FooSerializer.new(serializable).serialize }
|
14
|
+
|
15
|
+
it 'should serialize object' do
|
16
|
+
subject[:name].should eq('me')
|
17
|
+
subject[:some][:foo].should eq('1234')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
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.
|
4
|
+
version: 0.3.0.pre2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Droidlabs
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -73,24 +73,27 @@ extensions: []
|
|
73
73
|
extra_rdoc_files: []
|
74
74
|
files:
|
75
75
|
- lib/cyrax/base.rb
|
76
|
-
- lib/cyrax/base_collection_presenter.rb
|
77
|
-
- lib/cyrax/base_presenter.rb
|
78
|
-
- lib/cyrax/base_resource.rb
|
79
76
|
- lib/cyrax/callbacks.rb
|
80
|
-
- lib/cyrax/decorated_collection_presenter.rb
|
81
77
|
- lib/cyrax/decorator.rb
|
82
78
|
- lib/cyrax/extensions/has_callbacks.rb
|
83
79
|
- lib/cyrax/extensions/has_decorator.rb
|
84
80
|
- lib/cyrax/extensions/has_resource.rb
|
85
81
|
- lib/cyrax/extensions/has_response.rb
|
82
|
+
- lib/cyrax/extensions/has_serializer.rb
|
86
83
|
- lib/cyrax/extensions/has_service.rb
|
84
|
+
- lib/cyrax/helpers/controller.rb
|
85
|
+
- lib/cyrax/presenter.rb
|
86
|
+
- lib/cyrax/presenters/base_collection.rb
|
87
|
+
- lib/cyrax/presenters/decorated_collection.rb
|
88
|
+
- lib/cyrax/resource.rb
|
87
89
|
- lib/cyrax/response.rb
|
90
|
+
- lib/cyrax/serializer.rb
|
91
|
+
- lib/cyrax/serializers/scope.rb
|
88
92
|
- lib/cyrax/version.rb
|
89
93
|
- lib/cyrax.rb
|
90
94
|
- LICENSE.txt
|
91
95
|
- Rakefile
|
92
96
|
- README.md
|
93
|
-
- spec/cyrax/base_resource_spec.rb
|
94
97
|
- spec/cyrax/base_spec.rb
|
95
98
|
- spec/cyrax/callbacks_spec.rb
|
96
99
|
- spec/cyrax/decorator_spec.rb
|
@@ -99,7 +102,9 @@ files:
|
|
99
102
|
- spec/cyrax/extensions/has_resource_spec.rb
|
100
103
|
- spec/cyrax/extensions/has_response_spec.rb
|
101
104
|
- spec/cyrax/extensions/has_service_spec.rb
|
105
|
+
- spec/cyrax/resource_spec.rb
|
102
106
|
- spec/cyrax/response_spec.rb
|
107
|
+
- spec/cyrax/serializer_spec.rb
|
103
108
|
- spec/spec_helper.rb
|
104
109
|
homepage:
|
105
110
|
licenses:
|
@@ -116,9 +121,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
116
121
|
version: '0'
|
117
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
123
|
requirements:
|
119
|
-
- - ! '
|
124
|
+
- - ! '>'
|
120
125
|
- !ruby/object:Gem::Version
|
121
|
-
version:
|
126
|
+
version: 1.3.1
|
122
127
|
requirements: []
|
123
128
|
rubyforge_project:
|
124
129
|
rubygems_version: 2.0.5
|
@@ -126,7 +131,6 @@ signing_key:
|
|
126
131
|
specification_version: 4
|
127
132
|
summary: Small library for adding service layer to Rails projects
|
128
133
|
test_files:
|
129
|
-
- spec/cyrax/base_resource_spec.rb
|
130
134
|
- spec/cyrax/base_spec.rb
|
131
135
|
- spec/cyrax/callbacks_spec.rb
|
132
136
|
- spec/cyrax/decorator_spec.rb
|
@@ -135,5 +139,7 @@ test_files:
|
|
135
139
|
- spec/cyrax/extensions/has_resource_spec.rb
|
136
140
|
- spec/cyrax/extensions/has_response_spec.rb
|
137
141
|
- spec/cyrax/extensions/has_service_spec.rb
|
142
|
+
- spec/cyrax/resource_spec.rb
|
138
143
|
- spec/cyrax/response_spec.rb
|
144
|
+
- spec/cyrax/serializer_spec.rb
|
139
145
|
- spec/spec_helper.rb
|
@@ -1,17 +0,0 @@
|
|
1
|
-
class Cyrax::BaseCollectionPresenter
|
2
|
-
attr_reader :collection
|
3
|
-
|
4
|
-
def initialize(collection, options = {})
|
5
|
-
@collection = collection
|
6
|
-
end
|
7
|
-
|
8
|
-
def fetched_collection
|
9
|
-
if collection.is_a?(ActiveRecord::Relation)
|
10
|
-
collection.to_a
|
11
|
-
elsif collection.respond_to?(:all)
|
12
|
-
collection.all
|
13
|
-
else
|
14
|
-
Array.wrap collection
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
class Cyrax::DecoratedCollectionPresenter < Cyrax::BaseCollectionPresenter
|
2
|
-
attr_reader :decorator
|
3
|
-
|
4
|
-
def initialize(collection, options = {})
|
5
|
-
super
|
6
|
-
@decorator = options[:decorator]
|
7
|
-
raise "Decorator class is not defined! Please define it with option :decorator" unless decorator
|
8
|
-
end
|
9
|
-
|
10
|
-
class << self
|
11
|
-
alias_method :decorate, :new
|
12
|
-
end
|
13
|
-
|
14
|
-
array_methods = Array.instance_methods - Object.instance_methods
|
15
|
-
delegate *array_methods, to: :decorated_collection
|
16
|
-
|
17
|
-
def decorated_collection
|
18
|
-
@decorated_collection ||= fetched_collection.map {|item| decorate_item(item)}
|
19
|
-
end
|
20
|
-
|
21
|
-
def method_missing(method, *args, &block)
|
22
|
-
return super unless collection.respond_to?(method)
|
23
|
-
collection.send(method, *args, &block)
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def decorate_item(item)
|
29
|
-
decorator.new(item)
|
30
|
-
end
|
31
|
-
end
|