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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- N2I1ZjBjNDAwNjFjNzE5ODJkNmE4ZTA0MGViZjYxMjgzOGNiNzM2ZQ==
4
+ MGI1ZGJkM2IzYmEyNDhmNjg0OGU2MDExNjExYWFmZmFiMDQ3NTVmMg==
5
5
  data.tar.gz: !binary |-
6
- YzBmMWExNmJiMzJjNWMxMTFhNDFlYWQ4YjBmMGI4ZmNlNTUyZjJlOQ==
6
+ ZjUzODkzZjVjNDc4YTZjZjI4ODY0YmUyNmMwYjhhMjUwYTQ0NDNlMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YTYxZmRjNmVlNGViM2MyZjU0NjJiNDhkNWMyN2QyNmU0MmU1YmEzOWY4ZWRh
10
- NDE2MDliOTAwZjNjMzRkZDliYmQ3MDc4NjAyMDZjMGRjMGFhZjQ4OWU2ZGYw
11
- YjVlYzkwNjYxZjJiNTgzNDM5M2E4MzFkNzQ0Y2U3MGM2YTRhYTA=
9
+ ZDU3MGZjZWU0YzI0MzVlYTVhMWRlM2VjMDhjYzRlN2ZjOTYzMjQ1ZGNjMTgz
10
+ M2Q2NTg3Mzc4MTRiMWVhNWM5ZDcxOGM0MmM1ZTQ4OWQxYmEyNzMwZGE5NmQy
11
+ ZjIxOGVmMDA0M2E5NmM2YmQxYjgzNTliZmUwYmFlOTE2NDBmZDE=
12
12
  data.tar.gz: !binary |-
13
- N2I4MjM1YjI1MzE5YjhjOGJjNDZkMzU0MTBhMzE5OGFhMTA3MDIwOWNlYzE2
14
- OWI2N2I0MjdiYTg1NDhlZDNkMmRiNDI5YmEyOWNjNzY2YWVhZGZlMzcyMDUw
15
- ZWMzZmNjYjZkNDAzOTdmOWJjNTAxMTU3MjA2MzQ2M2VkOGRkNWE=
13
+ MTk5NTg5N2QzNGExZWEwYzZkOTg4MDA0ODJmMDU5ZmVkYzExYTFlMDVlNmU2
14
+ ODA5ZWQxNTExMzE1OWQ0NmM1ODhiMzUxMmRiZWZkYzBiNzJhODk5YmIzMzVi
15
+ MTkwMTdjNDhhMWJjZDk4YzI5MzMxZmJiOGM0OTliNWRhNTBkOGQ=
@@ -60,6 +60,10 @@ module SmoothOperator
60
60
  has_many?
61
61
  end
62
62
 
63
+ def rails_serialization?
64
+ options[:rails_serialization] == true
65
+ end
66
+
63
67
  private ################################# private
64
68
 
65
69
  def foreign_key_default
@@ -4,16 +4,16 @@ require "smooth_operator/associations/association_reflection"
4
4
  module SmoothOperator
5
5
  module Associations
6
6
 
7
- def has_many(nested_object_name, options = {})
8
- accepts_nested_objects(nested_object_name, :has_many, options)
7
+ def has_many(association, options = {})
8
+ accepts_nested_objects(association, :has_many, options)
9
9
  end
10
10
 
11
- def has_one(nested_object_name, options = {})
12
- accepts_nested_objects(nested_object_name, :has_one, options)
11
+ def has_one(association, options = {})
12
+ accepts_nested_objects(association, :has_one, options)
13
13
  end
14
14
 
15
- def belongs_to(nested_object_name, options = {})
16
- accepts_nested_objects(nested_object_name, :belongs_to, options)
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(_attributes = {}, options = {})
17
- return nil unless _attributes.is_a?(Hash)
16
+ def assign_attributes(attributes = {}, options = {})
17
+ return nil unless attributes.is_a?(Hash)
18
18
 
19
- attributes = _attributes = Helpers.stringify_keys(_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
- options.each { |key, value| @_options[key] = value } if options.is_a? Hash
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
- internal_data_push(name, value)
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
@@ -4,6 +4,10 @@ module SmoothOperator
4
4
 
5
5
  extend self
6
6
 
7
+ def generated_id
8
+ Time.now.to_f.to_s.split('.')[1]
9
+ end
10
+
7
11
  def primary_key(object)
8
12
  object.internal_data_get(object.class.primary_key)
9
13
  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
- _attribute_names(options).each do |attribute_name|
27
- hash[attribute_name] = _read_attribute_for_hashing(attribute_name, options)
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(options).each do |method_name|
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
- protected ##################### PROTECTED ###################
48
+ if only = options[:only]
49
+ white_list = [*only].map(&:to_s)
39
50
 
40
- # TODO: COMPLEX METHOD
41
- def _attribute_names(options)
42
- attribute_names = internal_data.keys.sort
51
+ attribute_names &= white_list
52
+ elsif except = options[:except]
53
+ black_list = [*except].map(&:to_s)
43
54
 
44
- if only = options[:only]
45
- attribute_names &= [*only].map(&:to_s)
46
- elsif except = options[:except]
47
- attribute_names -= [*except].map(&:to_s)
55
+ attribute_names -= black_list
56
+ end
57
+
58
+ attribute_names
48
59
  end
49
60
 
50
- attribute_names
51
- end
61
+ def method_names(object, options)
62
+ [*options[:methods]].select { |n| object.respond_to?(n) }
63
+ end
52
64
 
53
- def method_names(options)
54
- [*options[:methods]].select { |n| respond_to?(n) }
55
- end
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
- def _read_attribute_for_hashing(attribute_name, options)
58
- object = read_attribute_for_serialization(attribute_name)
72
+ if reflection && reflection.rails_serialization?
73
+ attribute_value = serialize_has_many_attribute(object, reflection, attribute_name, attribute_options)
59
74
 
60
- _options = options[attribute_name] || options[attribute_name.to_sym]
75
+ _attribute_name = "#{attribute_name}_attributes"
76
+ end
61
77
 
62
- if object.is_a?(Array)
63
- object.map { |array_entry| _attribute_to_hash(array_entry, _options) }
64
- else
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
- def _attribute_to_hash(object, options = nil)
70
- if object.respond_to?(:serializable_hash)
71
- Helpers.symbolyze_keys(object.serializable_hash(options))
72
- else
73
- object
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
@@ -1,3 +1,3 @@
1
1
  module SmoothOperator
2
- VERSION = "1.21.1"
2
+ VERSION = "1.21.3"
3
3
  end
@@ -6,8 +6,10 @@ class Post < SmoothOperator::Base
6
6
 
7
7
  self.endpoint = 'http://localhost:4567/'
8
8
 
9
- has_many :comments
9
+ self.rails_serialization = true
10
10
 
11
- belongs_to :address
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.1
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-01 00:00:00.000000000 Z
11
+ date: 2014-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler