cyrax 0.3.7 → 0.3.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/cyrax/base.rb +8 -0
- data/lib/cyrax/extensions/has_resource.rb +24 -0
- data/lib/cyrax/extensions/has_response.rb +4 -1
- data/lib/cyrax/extensions/has_serializer.rb +1 -1
- data/lib/cyrax/extensions/has_service.rb +37 -1
- data/lib/cyrax/serializer.rb +10 -0
- data/lib/cyrax/serializers/scope.rb +30 -9
- data/lib/cyrax/version.rb +1 -1
- data/spec/cyrax/serializer_spec.rb +12 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NGUwYzA1NjdmZDE3NTA2M2EzNDMzN2RmNmE3NWU3YmZjYTdjYWQ0NA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZTM5YWUzZWQ4YjViY2ZmZmY4MWMzZTI2NDgxNzIxNjNjZTAxNDBhZQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Yzg2M2I5MzRlYjhjYWNlMTZmZTczYTZhNTA4YjEzN2YwZjk1OWE5OWY2M2Y1
|
10
|
+
M2E1ZmVmNDI2NjM0MzI3Y2FmZDE4Y2YyNWMwNWIwOTAyY2Q2YjA1NWZiNDcz
|
11
|
+
YTAwMmFhMmUzZDA3ODYyYTEyMDQ1MWUyMjg5N2U2NmVhNzBmODg=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YjYzNWRjZDgxMmMxYThhNDI4MWFhMGNkZGI1ZGIzZmZiYTE0MDdiZTQ2MmQw
|
14
|
+
MzIyZjQ2YTc4YzU4MzgyNjNiNmNkNzJkM2JjZTFlNTliZjE3NzBmNmUwN2Rj
|
15
|
+
OGRjZDI3OGFhMzNmNTBhNGYzNWI1NmMxMTAzYmQzN2NkOGIyOGU=
|
data/lib/cyrax/base.rb
CHANGED
@@ -5,6 +5,14 @@ class Cyrax::Base
|
|
5
5
|
|
6
6
|
attr_accessor :params, :accessor
|
7
7
|
|
8
|
+
# Creates a new Cyrax resource
|
9
|
+
#
|
10
|
+
# @param options [Hash] Options to pass in. You need `:as` and `:params`
|
11
|
+
# :as defines the accessor
|
12
|
+
# :params are parameters passed by the controller usually
|
13
|
+
#
|
14
|
+
# @example Instantiating a new resource
|
15
|
+
# Products::UserResource.new(as: current_user, params: params)
|
8
16
|
def initialize(options = {})
|
9
17
|
@accessor = options[:as]
|
10
18
|
@params = options[:params]
|
@@ -9,6 +9,7 @@ module Cyrax::Extensions
|
|
9
9
|
class_attribute :collection_name
|
10
10
|
end
|
11
11
|
|
12
|
+
# Returns the resource class as a constant - e.g. Product
|
12
13
|
def resource_class
|
13
14
|
if self.resource_class_name
|
14
15
|
self.resource_class_name.constantize
|
@@ -17,6 +18,15 @@ module Cyrax::Extensions
|
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
21
|
+
# Returns the resource class - e.g. Product
|
22
|
+
# If you want your resource to return something interesting, you should override this in your resource by defining the method and returning your own scope
|
23
|
+
#
|
24
|
+
# @example Overriding resource_scope
|
25
|
+
# class Products::UserResource < Products::BaseResource
|
26
|
+
# def resource_scope
|
27
|
+
# accessor.products
|
28
|
+
# end
|
29
|
+
# end
|
20
30
|
def resource_scope
|
21
31
|
resource_class
|
22
32
|
end
|
@@ -30,6 +40,13 @@ module Cyrax::Extensions
|
|
30
40
|
end
|
31
41
|
|
32
42
|
module ClassMethods
|
43
|
+
|
44
|
+
# Class method for setting all the attributes that you want to access in the resource
|
45
|
+
#
|
46
|
+
# @example
|
47
|
+
# accessible_attributes :description, :model, :price_in_cents, :vendor
|
48
|
+
#
|
49
|
+
# @param attrs [Array(Symbol)] Symbols of the attributes
|
33
50
|
def accessible_attributes(*attrs)
|
34
51
|
if attrs.blank?
|
35
52
|
@accessible_attributes || []
|
@@ -39,6 +56,13 @@ module Cyrax::Extensions
|
|
39
56
|
end
|
40
57
|
end
|
41
58
|
|
59
|
+
# Class method for setting the resource that you want to access
|
60
|
+
#
|
61
|
+
# @example
|
62
|
+
# resource :product
|
63
|
+
#
|
64
|
+
# @param name [Symbol] The name of the resource
|
65
|
+
# @param options Hash [Hash] Options
|
42
66
|
def resource(name, options = {})
|
43
67
|
self.resource_name = name.to_s
|
44
68
|
self.resource_class_name = options[:class_name]
|
@@ -36,6 +36,9 @@ module Cyrax::Extensions
|
|
36
36
|
self.class.name.demodulize.underscore
|
37
37
|
end
|
38
38
|
|
39
|
+
# Generates the response for to pass to the Rails controller
|
40
|
+
# @param result The data you want to respond with - can be an Active Record Relation, the class of the Model itself (e.g. Product)
|
41
|
+
# @param options [Hash] Options
|
39
42
|
def respond_with(result, options = {})
|
40
43
|
options[:as] ||= accessor
|
41
44
|
name = options[:name] || response_name
|
@@ -43,7 +46,7 @@ module Cyrax::Extensions
|
|
43
46
|
if respond_to?(:decorable?) && decorable?
|
44
47
|
options = {decorator: decorator_class}.merge(options)
|
45
48
|
end
|
46
|
-
if respond_to?(:
|
49
|
+
if respond_to?(:serializable?) && serializable?
|
47
50
|
options = {serializer: serializer_class}.merge(options)
|
48
51
|
end
|
49
52
|
result = Cyrax::Presenter.present(result, options)
|
@@ -2,17 +2,27 @@ module Cyrax::Extensions
|
|
2
2
|
module HasService
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
+
# Builds and returns a collection response for Rails
|
6
|
+
# @return [Cyrax::Response] response
|
5
7
|
def collection
|
6
8
|
respond_with build_collection, name: collection_name, present: :collection
|
7
9
|
end
|
10
|
+
# Overrides collection with read_all
|
8
11
|
alias_method :read_all, :collection
|
9
12
|
alias_method :read_all!, :collection
|
10
13
|
|
14
|
+
# Builds a new resource without saving to DB
|
15
|
+
# Runs Model.new (before saving)
|
16
|
+
# Used for :new action in controller
|
17
|
+
# @return [Cyrax::Response] response
|
11
18
|
def build
|
12
19
|
respond_with build_resource(nil)
|
13
20
|
end
|
14
21
|
alias_method :build!, :build
|
15
22
|
|
23
|
+
# Creates a new resource and persists to DB
|
24
|
+
# Used for :create action in controller
|
25
|
+
# @return [Cyrax::Response] response
|
16
26
|
def create(custom_attributes = nil, &block)
|
17
27
|
resource = build_resource(nil, custom_attributes||resource_attributes)
|
18
28
|
transaction do
|
@@ -31,6 +41,9 @@ module Cyrax::Extensions
|
|
31
41
|
end
|
32
42
|
alias_method :create!, :create
|
33
43
|
|
44
|
+
# Reads a single item from the DB
|
45
|
+
# Used for :show action in controller
|
46
|
+
# @return [Cyrax::Response] response
|
34
47
|
def read
|
35
48
|
respond_with find_resource(params[:id])
|
36
49
|
end
|
@@ -38,7 +51,9 @@ module Cyrax::Extensions
|
|
38
51
|
alias_method :edit, :read
|
39
52
|
alias_method :edit!, :read
|
40
53
|
|
41
|
-
|
54
|
+
# Updates a single item and persists to DB
|
55
|
+
# Used for :update action in controller
|
56
|
+
# @return [Cyrax::Response] response
|
42
57
|
def update(custom_attributes = nil, &block)
|
43
58
|
resource = build_resource(params[:id], custom_attributes||resource_attributes)
|
44
59
|
transaction do
|
@@ -57,6 +72,9 @@ module Cyrax::Extensions
|
|
57
72
|
end
|
58
73
|
alias_method :update!, :update
|
59
74
|
|
75
|
+
# Destroys a resource from the DB
|
76
|
+
# Used for :destroy action in controller
|
77
|
+
# @return [Cyrax::Response] response
|
60
78
|
def destroy(&block)
|
61
79
|
resource = find_resource(params[:id])
|
62
80
|
transaction do
|
@@ -69,10 +87,17 @@ module Cyrax::Extensions
|
|
69
87
|
end
|
70
88
|
alias_method :destroy!, :destroy
|
71
89
|
|
90
|
+
# Finds and returns a single item from the DB
|
91
|
+
# @param id [int] ID of item
|
92
|
+
# @return [object] The object
|
72
93
|
def find_resource(id)
|
73
94
|
resource_scope.find(id)
|
74
95
|
end
|
75
96
|
|
97
|
+
# Instantiates the resource
|
98
|
+
# @param id [int] ID or nil if you want a new object
|
99
|
+
# @param attributes [hash] Attributes you want to add to the resource
|
100
|
+
# @return [object] The object
|
76
101
|
def build_resource(id, attributes = {})
|
77
102
|
if id.present?
|
78
103
|
resource = find_resource(id)
|
@@ -83,14 +108,25 @@ module Cyrax::Extensions
|
|
83
108
|
end
|
84
109
|
end
|
85
110
|
|
111
|
+
# Saves a resource
|
112
|
+
# @param resource [object] The resource to save
|
86
113
|
def save_resource(resource)
|
87
114
|
resource.save
|
88
115
|
end
|
89
116
|
|
117
|
+
# Remove a resource
|
118
|
+
# Calls destroy method on resource
|
119
|
+
# @param resource [object] The resource to destroy
|
90
120
|
def delete_resource(resource)
|
91
121
|
resource.destroy
|
92
122
|
end
|
93
123
|
|
124
|
+
# Returns a collection of the resource we are calling.
|
125
|
+
#
|
126
|
+
# If you want your resource to return something interesting, you should override the resource_scope method.
|
127
|
+
# Otherwise by default it will return the constantized model name and it will call .all on it during presentation.
|
128
|
+
#
|
129
|
+
# @return [type] The collection
|
94
130
|
def build_collection
|
95
131
|
resource_scope
|
96
132
|
end
|
data/lib/cyrax/serializer.rb
CHANGED
@@ -13,6 +13,16 @@ class Cyrax::Serializer < Cyrax::Wrapper
|
|
13
13
|
scope.namespace(name, &block)
|
14
14
|
end
|
15
15
|
|
16
|
+
def relation(name, &block)
|
17
|
+
scope.relation(name, &block)
|
18
|
+
end
|
19
|
+
alias_method :has_many, :relation
|
20
|
+
alias_method :has_one, :relation
|
21
|
+
|
22
|
+
def default_attributes
|
23
|
+
scope.default_attributes
|
24
|
+
end
|
25
|
+
|
16
26
|
def attributes(*attrs)
|
17
27
|
scope.attributes(*attrs)
|
18
28
|
end
|
@@ -3,11 +3,26 @@ module Cyrax::Serializers
|
|
3
3
|
def initialize(&block)
|
4
4
|
@attrs = {}
|
5
5
|
@dynamic_attrs = {}
|
6
|
+
@relation_attrs = {}
|
7
|
+
@namespace_attrs = {}
|
8
|
+
@default_attributes = false
|
6
9
|
instance_eval(&block) if block_given?
|
7
10
|
end
|
8
11
|
|
9
12
|
def namespace(name, &block)
|
10
|
-
@
|
13
|
+
@namespace_attrs[name] = self.class.new(&block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def relation(attribute, &block)
|
17
|
+
if block_given?
|
18
|
+
@relation_attrs[attribute] = self.class.new(&block)
|
19
|
+
else
|
20
|
+
@attrs[attribute] = attribute
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def default_attributes
|
25
|
+
@default_attributes = true
|
11
26
|
end
|
12
27
|
|
13
28
|
def attributes(*attrs)
|
@@ -25,8 +40,8 @@ module Cyrax::Serializers
|
|
25
40
|
end
|
26
41
|
|
27
42
|
def serialize(resource, options = {})
|
28
|
-
if resource.
|
29
|
-
resource.map{ |r| serialize_one(r, options) }
|
43
|
+
if resource.respond_to?(:to_a)
|
44
|
+
resource.to_a.map{ |r| serialize_one(r, options) }
|
30
45
|
else
|
31
46
|
serialize_one(resource, options)
|
32
47
|
end
|
@@ -34,16 +49,22 @@ module Cyrax::Serializers
|
|
34
49
|
|
35
50
|
def serialize_one(resource, options = {})
|
36
51
|
result = {}
|
37
|
-
@
|
38
|
-
|
39
|
-
if options.is_a?(Cyrax::Serializers::Scope)
|
40
|
-
value = options.serialize(value)
|
41
|
-
end
|
42
|
-
result[attribute] = value
|
52
|
+
if @default_attributes
|
53
|
+
result = resource.attributes rescue {}
|
43
54
|
end
|
44
55
|
@dynamic_attrs.map do |attribute, block|
|
45
56
|
result[attribute] = options[:serializer].instance_exec(resource, &block)
|
46
57
|
end
|
58
|
+
@relation_attrs.map do |attribute, scope|
|
59
|
+
value = resource.send(attribute)
|
60
|
+
result[attribute] = scope.serialize(value)
|
61
|
+
end
|
62
|
+
@namespace_attrs.map do |attribute, scope|
|
63
|
+
result[attribute] = scope.serialize(resource)
|
64
|
+
end
|
65
|
+
@attrs.map do |attribute, options|
|
66
|
+
result[attribute] = resource.send(attribute)
|
67
|
+
end
|
47
68
|
result
|
48
69
|
end
|
49
70
|
end
|
data/lib/cyrax/version.rb
CHANGED
@@ -5,16 +5,26 @@ class FooSerializer < Cyrax::Serializer
|
|
5
5
|
namespace :some do
|
6
6
|
attributes :foo, :bar
|
7
7
|
end
|
8
|
+
relation :another do
|
9
|
+
attributes :another_foo, :another_bar
|
10
|
+
end
|
8
11
|
end
|
9
12
|
|
10
13
|
module Cyrax
|
11
14
|
describe FooSerializer do
|
12
|
-
let(:serializable) {
|
15
|
+
let(:serializable) {
|
16
|
+
double(
|
17
|
+
name: 'me',
|
18
|
+
foo: '2342',
|
19
|
+
bar: '4223',
|
20
|
+
another: double(another_foo: '1234', another_bar: '1234'))
|
21
|
+
}
|
13
22
|
subject { FooSerializer.new(serializable).serialize }
|
14
23
|
|
15
24
|
it 'should serialize object' do
|
16
25
|
subject[:name].should eq('me')
|
17
|
-
subject[:some][:foo].should eq('
|
26
|
+
subject[:some][:foo].should eq('2342')
|
27
|
+
subject[:another][:another_foo].should eq('1234')
|
18
28
|
end
|
19
29
|
end
|
20
30
|
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.3.
|
4
|
+
version: 0.3.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Droidlabs
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|