smooth_operator 1.21.1 → 1.21.3
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.
- checksums.yaml +8 -8
- data/lib/smooth_operator/associations/association_reflection.rb +4 -0
- data/lib/smooth_operator/associations.rb +18 -6
- data/lib/smooth_operator/attribute_assignment.rb +23 -10
- data/lib/smooth_operator/helpers.rb +4 -0
- data/lib/smooth_operator/serialization.rb +70 -31
- data/lib/smooth_operator/version.rb +1 -1
- data/spec/support/models/post.rb +4 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MGI1ZGJkM2IzYmEyNDhmNjg0OGU2MDExNjExYWFmZmFiMDQ3NTVmMg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZjUzODkzZjVjNDc4YTZjZjI4ODY0YmUyNmMwYjhhMjUwYTQ0NDNlMA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZDU3MGZjZWU0YzI0MzVlYTVhMWRlM2VjMDhjYzRlN2ZjOTYzMjQ1ZGNjMTgz
|
10
|
+
M2Q2NTg3Mzc4MTRiMWVhNWM5ZDcxOGM0MmM1ZTQ4OWQxYmEyNzMwZGE5NmQy
|
11
|
+
ZjIxOGVmMDA0M2E5NmM2YmQxYjgzNTliZmUwYmFlOTE2NDBmZDE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MTk5NTg5N2QzNGExZWEwYzZkOTg4MDA0ODJmMDU5ZmVkYzExYTFlMDVlNmU2
|
14
|
+
ODA5ZWQxNTExMzE1OWQ0NmM1ODhiMzUxMmRiZWZkYzBiNzJhODk5YmIzMzVi
|
15
|
+
MTkwMTdjNDhhMWJjZDk4YzI5MzMxZmJiOGM0OTliNWRhNTBkOGQ=
|
@@ -4,16 +4,16 @@ require "smooth_operator/associations/association_reflection"
|
|
4
4
|
module SmoothOperator
|
5
5
|
module Associations
|
6
6
|
|
7
|
-
def has_many(
|
8
|
-
accepts_nested_objects(
|
7
|
+
def has_many(association, options = {})
|
8
|
+
accepts_nested_objects(association, :has_many, options)
|
9
9
|
end
|
10
10
|
|
11
|
-
def has_one(
|
12
|
-
accepts_nested_objects(
|
11
|
+
def has_one(association, options = {})
|
12
|
+
accepts_nested_objects(association, :has_one, options)
|
13
13
|
end
|
14
14
|
|
15
|
-
def belongs_to(
|
16
|
-
accepts_nested_objects(
|
15
|
+
def belongs_to(association, options = {})
|
16
|
+
accepts_nested_objects(association, :belongs_to, options)
|
17
17
|
end
|
18
18
|
|
19
19
|
def reflections
|
@@ -28,8 +28,16 @@ module SmoothOperator
|
|
28
28
|
macro ? reflections.values.select { |reflection| reflection.macro == macro } : reflections.values
|
29
29
|
end
|
30
30
|
|
31
|
+
def rails_serialization
|
32
|
+
Helpers.get_instance_variable(self, :rails_serialization, false)
|
33
|
+
end
|
34
|
+
|
35
|
+
attr_writer :rails_serialization
|
36
|
+
|
31
37
|
protected ###################### PROTECTED ###################
|
32
38
|
|
39
|
+
# TODO: THIS MUST GO TO A HELPER_METHODS MODULE
|
40
|
+
|
33
41
|
def accepts_nested_objects(association, macro, options = {})
|
34
42
|
options = parse_options(options, { macro: macro })
|
35
43
|
|
@@ -93,6 +101,10 @@ module SmoothOperator
|
|
93
101
|
def parse_options(options, default_options)
|
94
102
|
options = options.is_a?(Hash) ? options.merge(default_options) : default_options
|
95
103
|
|
104
|
+
if options[:rails_serialization].nil?
|
105
|
+
options[:rails_serialization] = rails_serialization
|
106
|
+
end
|
107
|
+
|
96
108
|
Helpers.symbolyze_keys(options)
|
97
109
|
end
|
98
110
|
|
@@ -13,29 +13,42 @@ module SmoothOperator
|
|
13
13
|
|
14
14
|
attr_reader :_options, :_meta_data
|
15
15
|
|
16
|
-
def assign_attributes(
|
17
|
-
return nil unless
|
16
|
+
def assign_attributes(attributes = {}, options = {})
|
17
|
+
return nil unless attributes.is_a?(Hash)
|
18
18
|
|
19
|
-
attributes =
|
20
|
-
|
21
|
-
if _attributes.include?(self.class.resource_name)
|
22
|
-
attributes = _attributes.delete(self.class.resource_name)
|
23
|
-
@_meta_data = _attributes
|
24
|
-
end
|
19
|
+
attributes = _extract_attributes(attributes)
|
25
20
|
|
26
21
|
induce_errors(attributes.delete(self.class.errors_key))
|
27
22
|
|
28
|
-
|
23
|
+
@_options.merge!(options) if options.is_a? Hash
|
29
24
|
|
30
25
|
attributes.each do |name, value|
|
31
26
|
next unless allowed_attribute(name)
|
32
27
|
|
33
|
-
|
28
|
+
if respond_to?("#{name}=")
|
29
|
+
send("#{name}=", value)
|
30
|
+
else
|
31
|
+
internal_data_push(name, value)
|
32
|
+
end
|
34
33
|
end
|
35
34
|
end
|
36
35
|
|
37
36
|
protected ################# PROTECTED METHODS DOWN BELOW ###################
|
38
37
|
|
38
|
+
def _extract_attributes(attributes)
|
39
|
+
_attributes = Helpers.stringify_keys(attributes)
|
40
|
+
|
41
|
+
if _attributes.include?(self.class.resource_name)
|
42
|
+
attributes = _attributes.delete(self.class.resource_name)
|
43
|
+
@_meta_data = _attributes
|
44
|
+
else
|
45
|
+
attributes = _attributes
|
46
|
+
@_meta_data = {}
|
47
|
+
end
|
48
|
+
|
49
|
+
attributes
|
50
|
+
end
|
51
|
+
|
39
52
|
def before_initialize(attributes, options); end
|
40
53
|
|
41
54
|
def after_initialize(attributes, options); end
|
@@ -6,7 +6,6 @@ module SmoothOperator
|
|
6
6
|
Helpers.symbolyze_keys(serializable_hash(options) || {})
|
7
7
|
end
|
8
8
|
|
9
|
-
# alias :attributes :to_hash
|
10
9
|
def attributes; to_hash; end
|
11
10
|
|
12
11
|
def to_json(options = nil)
|
@@ -22,58 +21,98 @@ module SmoothOperator
|
|
22
21
|
def serializable_hash(options = nil)
|
23
22
|
hash = {}
|
24
23
|
options ||= {}
|
24
|
+
method_names = HelperMethods.method_names(self, options)
|
25
|
+
attribute_names = HelperMethods.attribute_names(self, options)
|
25
26
|
|
26
|
-
|
27
|
-
|
27
|
+
attribute_names.each do |attribute_name|
|
28
|
+
attribute_name, attribute_value = HelperMethods
|
29
|
+
.serialize_normal_or_rails_way(self, attribute_name, options)
|
30
|
+
|
31
|
+
hash[attribute_name] = attribute_value
|
28
32
|
end
|
29
33
|
|
30
|
-
method_names
|
34
|
+
method_names.each do |method_name|
|
31
35
|
hash[method_name.to_s] = send(method_name)
|
32
36
|
end
|
33
37
|
|
34
38
|
hash
|
35
39
|
end
|
36
40
|
|
41
|
+
module HelperMethods
|
42
|
+
|
43
|
+
extend self
|
44
|
+
|
45
|
+
def attribute_names(object, options)
|
46
|
+
attribute_names = object.internal_data.keys.sort
|
37
47
|
|
38
|
-
|
48
|
+
if only = options[:only]
|
49
|
+
white_list = [*only].map(&:to_s)
|
39
50
|
|
40
|
-
|
41
|
-
|
42
|
-
|
51
|
+
attribute_names &= white_list
|
52
|
+
elsif except = options[:except]
|
53
|
+
black_list = [*except].map(&:to_s)
|
43
54
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
attribute_names
|
55
|
+
attribute_names -= black_list
|
56
|
+
end
|
57
|
+
|
58
|
+
attribute_names
|
48
59
|
end
|
49
60
|
|
50
|
-
|
51
|
-
|
61
|
+
def method_names(object, options)
|
62
|
+
[*options[:methods]].select { |n| object.respond_to?(n) }
|
63
|
+
end
|
52
64
|
|
53
|
-
|
54
|
-
|
55
|
-
|
65
|
+
def serialize_normal_or_rails_way(object, attribute_name, options)
|
66
|
+
_attribute_name, attribute_sym = attribute_name, attribute_name.to_sym
|
67
|
+
|
68
|
+
reflection = object.class.reflect_on_association(attribute_sym)
|
69
|
+
|
70
|
+
attribute_options = options[attribute_sym]
|
56
71
|
|
57
|
-
|
58
|
-
|
72
|
+
if reflection && reflection.rails_serialization?
|
73
|
+
attribute_value = serialize_has_many_attribute(object, reflection, attribute_name, attribute_options)
|
59
74
|
|
60
|
-
|
75
|
+
_attribute_name = "#{attribute_name}_attributes"
|
76
|
+
end
|
61
77
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
_attribute_to_hash(object, _options)
|
78
|
+
attribute_value ||= serialize_normal_attribute(object, attribute_name, attribute_options)
|
79
|
+
|
80
|
+
[_attribute_name, attribute_value]
|
66
81
|
end
|
67
|
-
end
|
68
82
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
83
|
+
def serialize_has_many_attribute(parent_object, reflection, attribute_name, options)
|
84
|
+
return nil unless reflection.has_many?
|
85
|
+
|
86
|
+
object = parent_object.read_attribute_for_serialization(attribute_name)
|
87
|
+
|
88
|
+
object.reduce({}) do |hash, array_entry|
|
89
|
+
id = Helpers.present?(array_entry.id) ? array_entry.id : Helpers.generated_id
|
90
|
+
|
91
|
+
hash[id.to_s] = attribute_to_hash(array_entry, options)
|
92
|
+
|
93
|
+
hash
|
94
|
+
end
|
74
95
|
end
|
96
|
+
|
97
|
+
def serialize_normal_attribute(parent_object, attribute_name, options)
|
98
|
+
object = parent_object.read_attribute_for_serialization(attribute_name)
|
99
|
+
|
100
|
+
if object.is_a?(Array)
|
101
|
+
object.map { |array_entry| attribute_to_hash(array_entry, options) }
|
102
|
+
else
|
103
|
+
attribute_to_hash(object, options)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def attribute_to_hash(object, options = nil)
|
108
|
+
if object.respond_to?(:serializable_hash)
|
109
|
+
Helpers.symbolyze_keys(object.serializable_hash(options))
|
110
|
+
else
|
111
|
+
object
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
75
115
|
end
|
76
116
|
|
77
117
|
end
|
78
|
-
|
79
118
|
end
|
data/spec/support/models/post.rb
CHANGED
@@ -6,8 +6,10 @@ class Post < SmoothOperator::Base
|
|
6
6
|
|
7
7
|
self.endpoint = 'http://localhost:4567/'
|
8
8
|
|
9
|
-
|
9
|
+
self.rails_serialization = true
|
10
10
|
|
11
|
-
|
11
|
+
has_many :comments#, rails_serialization: true
|
12
|
+
|
13
|
+
belongs_to :address#, rails_serialization: true
|
12
14
|
|
13
15
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smooth_operator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.21.
|
4
|
+
version: 1.21.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- João Gonçalves
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|