cerializable 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cerializable.rb +1 -1
- data/lib/cerializable/cerializer.rb +1 -1
- data/lib/cerializable/model.rb +96 -0
- data/lib/cerializable/version.rb +1 -1
- data/test/cerializable_test.rb +2 -2
- data/test/dummy/app/models/some_poro_class.rb +9 -0
- data/test/dummy/app/serializers/some_poro_class_serializer.rb +6 -0
- data/test/dummy/log/test.log +8638 -0
- data/test/{acts_as_cerializable_test.rb → model_test.rb} +6 -2
- metadata +26 -8
- data/lib/cerializable/acts_as_cerializable.rb +0 -84
@@ -1,13 +1,17 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class ModelTest < ActiveSupport::TestCase
|
4
4
|
|
5
5
|
test "ActiveRecord::Base#acts_as_cerializable" do
|
6
6
|
assert true, ActiveRecord::Base.respond_to?(:acts_as_cerializable)
|
7
7
|
end
|
8
8
|
|
9
|
+
test "ActiveRecord::Base#cerializable" do
|
10
|
+
assert true, ActiveRecord::Base.respond_to?(:cerializable)
|
11
|
+
end
|
12
|
+
|
9
13
|
# Ensure both models are serializing their instances as expected
|
10
|
-
[Model, AnotherModel].each { |model_class|
|
14
|
+
[Model, AnotherModel, SomePoroClass].each { |model_class|
|
11
15
|
|
12
16
|
test "#{ model_class.name }#cerializer is defined" do
|
13
17
|
assert_equal model_class.send(:cerializer).class, Cerializable::Cerializer
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cerializable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Arnold
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -17,7 +17,7 @@ dependencies:
|
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 4.1.0
|
20
|
-
type: :
|
20
|
+
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
@@ -38,7 +38,21 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: appraisal
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Flexible custom serialization for Ruby objects.
|
42
56
|
email:
|
43
57
|
- eric.ed.arnold@gmail.com
|
44
58
|
executables: []
|
@@ -48,11 +62,10 @@ files:
|
|
48
62
|
- MIT-LICENSE
|
49
63
|
- Rakefile
|
50
64
|
- lib/cerializable.rb
|
51
|
-
- lib/cerializable/acts_as_cerializable.rb
|
52
65
|
- lib/cerializable/cerializer.rb
|
66
|
+
- lib/cerializable/model.rb
|
53
67
|
- lib/cerializable/version.rb
|
54
68
|
- lib/tasks/cerializable_tasks.rake
|
55
|
-
- test/acts_as_cerializable_test.rb
|
56
69
|
- test/cerializable_test.rb
|
57
70
|
- test/dummy/README.rdoc
|
58
71
|
- test/dummy/Rakefile
|
@@ -62,8 +75,10 @@ files:
|
|
62
75
|
- test/dummy/app/helpers/application_helper.rb
|
63
76
|
- test/dummy/app/models/another_model.rb
|
64
77
|
- test/dummy/app/models/model.rb
|
78
|
+
- test/dummy/app/models/some_poro_class.rb
|
65
79
|
- test/dummy/app/serializers/another_serializer.rb
|
66
80
|
- test/dummy/app/serializers/model_serializer.rb
|
81
|
+
- test/dummy/app/serializers/some_poro_class_serializer.rb
|
67
82
|
- test/dummy/app/views/layouts/application.html.erb
|
68
83
|
- test/dummy/bin/bundle
|
69
84
|
- test/dummy/bin/rails
|
@@ -103,6 +118,7 @@ files:
|
|
103
118
|
- test/dummy/test/fixtures/models.yml
|
104
119
|
- test/dummy/test/models/another_model_test.rb
|
105
120
|
- test/dummy/test/models/model_test.rb
|
121
|
+
- test/model_test.rb
|
106
122
|
- test/test_helper.rb
|
107
123
|
homepage: https://github.com/nativestranger/cerializable
|
108
124
|
licenses:
|
@@ -127,9 +143,8 @@ rubyforge_project:
|
|
127
143
|
rubygems_version: 2.6.6
|
128
144
|
signing_key:
|
129
145
|
specification_version: 4
|
130
|
-
summary: Flexible custom serialization for
|
146
|
+
summary: Flexible custom serialization for Ruby objects
|
131
147
|
test_files:
|
132
|
-
- test/acts_as_cerializable_test.rb
|
133
148
|
- test/cerializable_test.rb
|
134
149
|
- test/dummy/app/assets/javascripts/application.js
|
135
150
|
- test/dummy/app/assets/stylesheets/application.css
|
@@ -137,8 +152,10 @@ test_files:
|
|
137
152
|
- test/dummy/app/helpers/application_helper.rb
|
138
153
|
- test/dummy/app/models/another_model.rb
|
139
154
|
- test/dummy/app/models/model.rb
|
155
|
+
- test/dummy/app/models/some_poro_class.rb
|
140
156
|
- test/dummy/app/serializers/another_serializer.rb
|
141
157
|
- test/dummy/app/serializers/model_serializer.rb
|
158
|
+
- test/dummy/app/serializers/some_poro_class_serializer.rb
|
142
159
|
- test/dummy/app/views/layouts/application.html.erb
|
143
160
|
- test/dummy/bin/bundle
|
144
161
|
- test/dummy/bin/rails
|
@@ -180,4 +197,5 @@ test_files:
|
|
180
197
|
- test/dummy/test/fixtures/models.yml
|
181
198
|
- test/dummy/test/models/another_model_test.rb
|
182
199
|
- test/dummy/test/models/model_test.rb
|
200
|
+
- test/model_test.rb
|
183
201
|
- test/test_helper.rb
|
@@ -1,84 +0,0 @@
|
|
1
|
-
module Cerializable
|
2
|
-
module ActsAsCerializable
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
included do
|
6
|
-
# #cerializable_hash delegates to the #run method of the model's 'cerializer' object.
|
7
|
-
#
|
8
|
-
# It accepts `:only`, `:except`, and `:methods` options which can be passed as a
|
9
|
-
# symbol or as an array of symbols.
|
10
|
-
#
|
11
|
-
# Using the `:only` option will return a hash that only has the specified keys.
|
12
|
-
#
|
13
|
-
# > comment.cerializable_hash(only: :id)
|
14
|
-
# => { id: 1 }
|
15
|
-
#
|
16
|
-
# Using the `:except` option will return a hash that has all default keys except those specified.
|
17
|
-
#
|
18
|
-
# > comment.cerializable_hash(except: [:id, :user_id])
|
19
|
-
# => { body: '...sushi? ;)', deleted_at: nil }
|
20
|
-
#
|
21
|
-
# Using the `:methods` option add will add a key and value for each method specified.
|
22
|
-
#
|
23
|
-
# The key is the method name as a symbol.
|
24
|
-
# The value is the return value given when calling the method on the model instance.
|
25
|
-
#
|
26
|
-
# The :methods option is processed after the :only and :except options.
|
27
|
-
#
|
28
|
-
# > comment.cerializable_hash(only: id, methods: :hash])
|
29
|
-
# => { id: 1, hash: -2535926706119161824 }
|
30
|
-
#
|
31
|
-
def cerializable_hash(options = {})
|
32
|
-
[:only, :except, :methods].each do |option_name|
|
33
|
-
next if options[option_name].nil?
|
34
|
-
|
35
|
-
unless options[option_name].is_a?(Symbol) || options[option_name].is_a?(Array)
|
36
|
-
raise Exception, "'#{ option_name }' option must be of an Array or Symbol class."
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
# serialize the instance using the class's cerializer object.
|
41
|
-
hash = self.class.cerializer.run(self, options)
|
42
|
-
|
43
|
-
# alter the hash according to the :only, :except, and :methods serialization options.
|
44
|
-
ensure_is_array = proc { |arg| arg.class.ancestors.include?(Array) ? arg : Array.new(1, arg) }
|
45
|
-
|
46
|
-
if except_options = options[:except] && ensure_is_array.call(options[:except])
|
47
|
-
except_options.each { |key| hash.delete(key) }
|
48
|
-
end
|
49
|
-
|
50
|
-
if only_options = options[:only] && ensure_is_array.call(options[:only])
|
51
|
-
hash.keys.each { |key| hash.delete(key) unless only_options.include?(key) }
|
52
|
-
end
|
53
|
-
|
54
|
-
if methods_options = options[:methods] && ensure_is_array.call(options[:methods])
|
55
|
-
methods_options.each { |method_name| hash[method_name] = self.send(method_name) }
|
56
|
-
end
|
57
|
-
|
58
|
-
hash
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
module ClassMethods
|
64
|
-
# `acts_as_cerializable` is used to declare that a
|
65
|
-
# model uses Cerializable for serialization.
|
66
|
-
#
|
67
|
-
# Unless a module is specified via the
|
68
|
-
# +serialize_with+ option, the base model's cerializer will attempt
|
69
|
-
# to include a module based on the model's name.
|
70
|
-
#
|
71
|
-
# For example, calling +Comment.acts_as_cerializable+ without a +serialize_with+
|
72
|
-
# option will cause Cerializable to look for a +CommentSerializer+.
|
73
|
-
#
|
74
|
-
# Calling +Comment.acts_as_cerializable+ +serialize_with:+ +MySerializer+
|
75
|
-
# will cause Cerializable to look for a +MySerializer+ instead.
|
76
|
-
def acts_as_cerializable(options = {})
|
77
|
-
Cerializable.setup(options.merge(base: self))
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
ActiveRecord::Base.send :include, Cerializable::ActsAsCerializable
|