scoped_serializer 0.1.0 → 1.0.0.b

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c32261f4040ce1b81c7e955dc1e0122163cde4d
4
- data.tar.gz: b84e63cf91a309ea2b683af89cc260ea51028506
3
+ metadata.gz: 0e9612c4d1cdb699e0f40eb48488a7070c2c0061
4
+ data.tar.gz: 81cba16a43b750cc6cb0f0a5d430f6f7ac9dcb09
5
5
  SHA512:
6
- metadata.gz: db2ffb1aa94ce0bf5e6d57187079ecd7fc993e1429a718f4a13050e3449752bb45abda201d1ca1ac44e256c5ac3142f861a052cb33c2ad40db28a949d14ea8ab
7
- data.tar.gz: 44234ef5494e7eacd4ed8a77a30e0718d6f89254143d50c8f3a1add90be4f00540f314686753a6bc2b3c110afe22dca0cacca027d256d5ec0d8a6c112492f20c
6
+ metadata.gz: 7d1b6de7261469f277e6f68b18317b57d6e5f7a769d54f12043ee746131d46cbb5bb6d4ca08c64edf697ef44a830be0a570d0949729bad0c3692a7a16c8c35fa
7
+ data.tar.gz: 75f0c405ffcb113f1ad0a8ccfc05f24b4393023d40de02485b22f5ae3f7349a5e1706dfc001ba5415df7851013bfa553fc2f4661833f10a513ee7561235a746b
@@ -0,0 +1,50 @@
1
+ module ActionController
2
+ module Serialization
3
+
4
+ extend ActiveSupport::Concern
5
+
6
+ ##
7
+ # Returns scope based on action.
8
+ #
9
+ def serializer_scope
10
+ scope = case action_name
11
+ when 'new', 'show', 'edit', 'update', 'create', 'destroy'
12
+ :resource
13
+ when 'index'
14
+ :collection
15
+ else
16
+ :default
17
+ end
18
+ end
19
+
20
+ ##
21
+ # Default serializer options.
22
+ #
23
+ def default_serializer_options
24
+ {}
25
+ end
26
+
27
+ ##
28
+ # JSON serializer to use.
29
+ #
30
+ def build_json_serializer(object, options={})
31
+ ScopedSerializer.for(object, { :scope => serializer_scope, :super => true }.merge(options.merge(default_serializer_options)))
32
+ end
33
+
34
+ ##
35
+ # Renders specific JSON serializer see {ScopedSerializer}.
36
+ #
37
+ [:_render_option_json, :_render_with_renderer_json].each do |method|
38
+ define_method method do |resource, options|
39
+ serializer = build_json_serializer(resource, options)
40
+
41
+ if serializer
42
+ super(serializer, options)
43
+ else
44
+ super(resource, options)
45
+ end
46
+ end
47
+ end
48
+
49
+ end
50
+ end
@@ -1,18 +1,15 @@
1
1
  module ScopedSerializer
2
2
  class ArraySerializer < BaseSerializer
3
3
 
4
- attr_reader :array, :scope, :scope_name, :options
4
+ attr_reader :array, :options
5
5
 
6
- def initialize(array, scope_name=:default, options={})
6
+ def initialize(array, options={})
7
7
  @array = array
8
- @scope_name = scope_name
9
8
  @options = options || {}
10
9
  end
11
10
 
12
11
  def serializable_hash(options={})
13
- array.collect do |object|
14
- ScopedSerializer.for(object, @scope_name, @options.merge(:root => false)).as_json
15
- end
12
+ serializable_objects.collect(&:as_json)
16
13
  end
17
14
 
18
15
  def meta
@@ -25,5 +22,28 @@ module ScopedSerializer
25
22
  data
26
23
  end
27
24
 
25
+ ##
26
+ # Returns attributes as a CSV string.
27
+ #
28
+ def to_csv(options={})
29
+ columns = options.delete(:columns)
30
+
31
+ CSV.generate(options) do |csv|
32
+ csv << columns if columns.present?
33
+
34
+ serializable_objects.each do |object|
35
+ csv << object.attributes_hash.values
36
+ end
37
+ end
38
+ end
39
+
40
+ protected
41
+
42
+ def serializable_objects
43
+ @serializable_objects ||= array.collect do |object|
44
+ ScopedSerializer.for(object, @options.merge(:root => false))
45
+ end
46
+ end
47
+
28
48
  end
29
49
  end
@@ -1,3 +1,5 @@
1
+ require 'csv'
2
+
1
3
  module ScopedSerializer
2
4
  class BaseSerializer
3
5
 
@@ -54,5 +56,24 @@ module ScopedSerializer
54
56
  @options[:meta] || {}
55
57
  end
56
58
 
59
+ ##
60
+ # Returns attributes as a CSV string.
61
+ #
62
+ def to_csv(options={})
63
+ CSV.generate(options) do |csv|
64
+ csv << scope.attributes
65
+ csv << attributes_hash.values
66
+ end
67
+ end
68
+
69
+ ##
70
+ # Returns attributes as an XLS string.
71
+ #
72
+ def to_xls(options={})
73
+ options.merge!(:col_sep => "\t")
74
+
75
+ to_csv(options)
76
+ end
77
+
57
78
  end
58
79
  end
@@ -4,8 +4,21 @@ module ScopedSerializer
4
4
  def initialize(*args)
5
5
  super
6
6
 
7
+ options = args.extract_options!
8
+
9
+ # Allow to define own model class
10
+ @model_class = options.delete(:model_class) || @array.klass
11
+
7
12
  # Configure root element
8
- @options[:root] = default_root_key(@array.klass).pluralize if @options[:root].nil?
13
+ @options[:root] = default_root_key(@model_class).pluralize if @options[:root].nil?
14
+ end
15
+
16
+ def to_csv(options={})
17
+ attributes = ScopedSerializer.find_serializer_by_class(@model_class)
18
+ .find_scope(options[:scope] || :default)
19
+ .attributes
20
+
21
+ super(options.merge(:columns => attributes))
9
22
  end
10
23
 
11
24
  end
@@ -61,13 +61,10 @@ module ScopedSerializer
61
61
 
62
62
  attr_reader :resource, :scope, :options
63
63
 
64
- def initialize(resource, scope=:default, options={})
64
+ def initialize(resource, options={})
65
65
  @resource = resource
66
66
  @options = options || {}
67
-
68
- if options[:scope].present?
69
- scope = options[:scope]
70
- end
67
+ scope = options[:scope] || :default
71
68
 
72
69
  if scope.is_a?(Symbol)
73
70
  @scope_name = scope
@@ -146,7 +143,7 @@ module ScopedSerializer
146
143
  end
147
144
 
148
145
  object = fetch_association(association_data, includes)
149
- data = ScopedSerializer.for(object, :default, options.merge(:associations => options[:include])).as_json
146
+ data = ScopedSerializer.for(object, options.merge(:associations => options[:include])).as_json
150
147
 
151
148
  hash.merge!(data) if data
152
149
  end
@@ -1,3 +1,3 @@
1
1
  module ScopedSerializer
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0.b"
3
3
  end
@@ -5,6 +5,17 @@ require 'scoped_serializer/default_serializer'
5
5
  require 'scoped_serializer/array_serializer'
6
6
  require 'scoped_serializer/collection_serializer'
7
7
 
8
+ begin
9
+ require 'action_controller'
10
+ require 'action_controller/serialization'
11
+
12
+ ActiveSupport.on_load(:action_controller) do
13
+ include ::ActionController::Serialization
14
+ end
15
+ rescue LoadError
16
+ # No Rails?
17
+ end
18
+
8
19
  ##
9
20
  # ScopedSerializer takes care of complex and abstract serialization classes.
10
21
  # It does this by allowing serialization scopes. For example, you can define a collection and a resource scope.
@@ -28,8 +39,8 @@ require 'scoped_serializer/collection_serializer'
28
39
  # end
29
40
  # end
30
41
  #
31
- # ScopedSerializer.render(@order, :resource)
32
- # ScopedSerializer.render(Order.order('id ASC'), :collection)
42
+ # ScopedSerializer.render(@order, :scope => :resource)
43
+ # ScopedSerializer.render(Order.order('id ASC'), :scope => :collection)
33
44
  #
34
45
 
35
46
  module ScopedSerializer
@@ -42,25 +53,25 @@ module ScopedSerializer
42
53
  #
43
54
  # @return [Hash]
44
55
  #
45
- def render(object, scope=:default, options={})
56
+ def render(object, options={})
46
57
  options.merge!({
47
58
  :super => true
48
59
  })
49
60
 
50
- self.for(object, scope, options).as_json
61
+ self.for(object, options).as_json
51
62
  end
52
63
 
53
64
  ##
54
65
  # Returns an instantized serializer for the given object.
55
66
  #
56
- def for(object, scope=:default, options={})
67
+ def for(object, options={})
57
68
  if object.respond_to?(:each)
58
69
  serializer = find_serializer(object)
59
70
  else
60
71
  serializer = options[:serializer] || find_serializer(object) || DefaultSerializer
61
72
  end
62
73
 
63
- serializer.new(object, scope, options) if serializer
74
+ serializer.new(object, options) if serializer
64
75
  end
65
76
 
66
77
  ##
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scoped_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0.b
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arjen Oosterkamp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-10 00:00:00.000000000 Z
11
+ date: 2015-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -108,6 +108,48 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: actionpack
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: activesupport
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
111
153
  description: Scoped serializers for Rails
112
154
  email:
113
155
  - mail@arjen.me
@@ -117,6 +159,7 @@ extra_rdoc_files: []
117
159
  files:
118
160
  - MIT-LICENSE
119
161
  - Rakefile
162
+ - lib/action_controller/serialization.rb
120
163
  - lib/scoped_serializer.rb
121
164
  - lib/scoped_serializer/array_serializer.rb
122
165
  - lib/scoped_serializer/base_serializer.rb
@@ -141,12 +184,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
141
184
  version: '0'
142
185
  required_rubygems_version: !ruby/object:Gem::Requirement
143
186
  requirements:
144
- - - ">="
187
+ - - ">"
145
188
  - !ruby/object:Gem::Version
146
- version: '0'
189
+ version: 1.3.1
147
190
  requirements: []
148
191
  rubyforge_project:
149
- rubygems_version: 2.2.2
192
+ rubygems_version: 2.4.5
150
193
  signing_key:
151
194
  specification_version: 4
152
195
  summary: Scoped serializers for Rails