smooth_operator 1.3.0 → 1.8.0

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.
Files changed (60) hide show
  1. checksums.yaml +8 -8
  2. data/Gemfile +0 -5
  3. data/README.md +11 -308
  4. data/console.rb +3 -28
  5. data/lib/smooth_operator.rb +7 -75
  6. data/lib/smooth_operator/array_with_meta_data.rb +21 -20
  7. data/lib/smooth_operator/attribute_assignment.rb +53 -57
  8. data/lib/smooth_operator/delegation.rb +34 -15
  9. data/lib/smooth_operator/finder_methods.rb +17 -33
  10. data/lib/smooth_operator/helpers.rb +7 -37
  11. data/lib/smooth_operator/internal_attribute.rb +49 -0
  12. data/lib/smooth_operator/model_schema.rb +72 -0
  13. data/lib/smooth_operator/open_struct.rb +4 -7
  14. data/lib/smooth_operator/operator.rb +64 -102
  15. data/lib/smooth_operator/persistence.rb +64 -94
  16. data/lib/smooth_operator/remote_call.rb +70 -0
  17. data/lib/smooth_operator/serialization.rb +33 -89
  18. data/lib/smooth_operator/translation.rb +13 -26
  19. data/lib/smooth_operator/type_converter.rb +69 -0
  20. data/lib/smooth_operator/validations.rb +3 -25
  21. data/lib/smooth_operator/version.rb +1 -1
  22. data/smooth_operator.gemspec +5 -9
  23. data/spec/factories/user_factory.rb +4 -5
  24. data/spec/smooth_operator/attribute_assignment_spec.rb +11 -145
  25. data/spec/smooth_operator/delegation_spec.rb +54 -57
  26. data/spec/smooth_operator/finder_methods_spec.rb +2 -91
  27. data/spec/smooth_operator/{resource_name_spec.rb → model_schema_spec.rb} +2 -2
  28. data/spec/smooth_operator/operator_spec.rb +1 -1
  29. data/spec/smooth_operator/persistence_spec.rb +20 -140
  30. data/spec/smooth_operator/serialization_spec.rb +4 -28
  31. data/spec/spec_helper.rb +9 -7
  32. data/spec/support/models/address.rb +0 -9
  33. data/spec/support/models/post.rb +3 -9
  34. data/spec/support/models/user.rb +7 -30
  35. data/spec/support/models/user_with_address_and_posts.rb +12 -20
  36. data/spec/support/test_server.rb +7 -63
  37. metadata +18 -55
  38. data/lib/smooth_operator/associations.rb +0 -110
  39. data/lib/smooth_operator/associations/association_reflection.rb +0 -79
  40. data/lib/smooth_operator/associations/has_many_relation.rb +0 -45
  41. data/lib/smooth_operator/associations/reflection.rb +0 -41
  42. data/lib/smooth_operator/cookie_jar.rb +0 -21
  43. data/lib/smooth_operator/http_methods.rb +0 -17
  44. data/lib/smooth_operator/internal_data.rb +0 -45
  45. data/lib/smooth_operator/operators/connection_wrapper.rb +0 -15
  46. data/lib/smooth_operator/operators/faraday.rb +0 -75
  47. data/lib/smooth_operator/operators/typhoeus.rb +0 -87
  48. data/lib/smooth_operator/options.rb +0 -30
  49. data/lib/smooth_operator/remote_call/base.rb +0 -76
  50. data/lib/smooth_operator/remote_call/errors/connection_failed.rb +0 -20
  51. data/lib/smooth_operator/remote_call/errors/timeout.rb +0 -20
  52. data/lib/smooth_operator/remote_call/faraday.rb +0 -19
  53. data/lib/smooth_operator/remote_call/typhoeus.rb +0 -19
  54. data/lib/smooth_operator/resource_name.rb +0 -46
  55. data/lib/smooth_operator/schema.rb +0 -21
  56. data/lib/smooth_operator/type_casting.rb +0 -127
  57. data/spec/require_helper.rb +0 -11
  58. data/spec/smooth_operator/remote_call_spec.rb +0 -340
  59. data/spec/smooth_operator/validations_spec.rb +0 -42
  60. data/spec/support/models/comment.rb +0 -5
@@ -1,8 +1,6 @@
1
1
  module UserWithAddressAndPosts
2
-
3
- class Father < User::Base
4
-
5
- options resource_name: 'user'
2
+
3
+ class Father < User
6
4
 
7
5
  schema(
8
6
  posts: Post,
@@ -13,33 +11,27 @@ module UserWithAddressAndPosts
13
11
 
14
12
  class Son < Father
15
13
 
14
+ self.table_name = 'users'
15
+
16
16
  schema(
17
17
  age: :int,
18
18
  dob: :date,
19
- price: :float,
20
- manager: :bool,
21
- date: :datetime,
22
- complex_field: nil,
23
- first_name: :string
19
+ manager: :bool
24
20
  )
25
21
 
26
22
  end
27
23
 
28
- class SoftBehaviour < Son
29
-
30
- options strict_behaviour: false
31
-
32
- end
33
-
34
24
  class WithPatch < Son
35
25
 
36
- options update_http_verb: 'patch'
26
+ self.table_name = 'users'
37
27
 
38
- end
28
+ self.save_http_verb = :patch
39
29
 
30
+ end
31
+
40
32
 
41
33
  module UserBlackListed
42
-
34
+
43
35
  class Father < ::UserWithAddressAndPosts::Son
44
36
 
45
37
  attributes_black_list_add "last_name"
@@ -55,7 +47,7 @@ module UserWithAddressAndPosts
55
47
  end
56
48
 
57
49
  module UserWhiteListed
58
-
50
+
59
51
  class Father < ::UserWithAddressAndPosts::Son
60
52
 
61
53
  attributes_white_list_add "id"
@@ -69,7 +61,7 @@ module UserWithAddressAndPosts
69
61
  end
70
62
 
71
63
  end
72
-
64
+
73
65
  class UserWithMyMethod < UserWithAddressAndPosts::Son
74
66
 
75
67
  def my_method
@@ -1,6 +1,5 @@
1
1
  require 'sinatra/base'
2
2
  require 'sinatra/json'
3
- require 'pry'
4
3
 
5
4
  class TestServer < Sinatra::Base
6
5
 
@@ -8,17 +7,6 @@ class TestServer < Sinatra::Base
8
7
  username == 'admin' and password == 'admin'
9
8
  end
10
9
 
11
- get '/posts/:id' do
12
- post_data = { id: 1, body: 'from_resource_url' }
13
- json post_data
14
- end
15
-
16
- get '/users/:id/posts/:id' do
17
- post_data = { id: 1, body: 'from_nested_url' }
18
- json post_data
19
- end
20
-
21
-
22
10
  get '/users/test_hash_with_array' do
23
11
  status test_hash_with_array
24
12
  end
@@ -37,70 +25,27 @@ class TestServer < Sinatra::Base
37
25
 
38
26
 
39
27
  get '/users' do
40
- users = [FactoryGirl.attributes_for(:user_with_address_and_posts), FactoryGirl.attributes_for(:user_with_address_and_posts)]
41
-
42
- users[0][:id] = 1
43
- users[1][:id] = 2
44
-
45
- json users
46
- end
47
-
48
- get '/users/array_with_nested_users' do
49
- nested_users = [{ id: 1, first_name: '1' }, { id: 2, first_name: '2' }]
50
-
51
- users = [{ id: 1, users: nested_users}, { id: 2, users: nested_users}]
52
-
53
- json users
54
- end
55
-
56
- get '/users/misc_array' do
57
- json [FactoryGirl.attributes_for(:user_with_address_and_posts), 2]
28
+ json [{ id: 1 }, { id: 2 }]
58
29
  end
59
30
 
60
31
  get '/users/with_metadata' do
61
- nested_users = [{ id: 1, first_name: '1' }, { id: 2, first_name: '2' }]
62
-
63
- users = [{ id: 1, users: nested_users}, { id: 2, users: nested_users}]
64
-
65
- data = { page: 1, total: 6, users: users }
66
-
32
+ data = { page: 1, total: 6, users: [{ id: 1 }, { id: 2 }] }
67
33
  json data
68
34
  end
69
-
70
- get '/users/bad_json' do
71
- 'ok'
72
- end
73
-
35
+
74
36
  get '/users/:id' do
75
37
  json FactoryGirl.attributes_for(:user_with_address_and_posts)
76
38
  end
77
39
 
78
- get '/users/:id/with_metadata' do
79
- user_data = { user: FactoryGirl.attributes_for(:user_with_address_and_posts), status: 1 }
80
- json user_data
81
- end
82
-
83
-
84
- put '/users/:id/send_error' do
85
- data_with_error = { id: 1, errors: [{ first_name: ["can't be blank"] }] }
86
- json data_with_error
87
- end
88
-
89
40
 
90
41
  post '/users' do
91
42
  common_response
92
43
  end
93
44
 
94
- post '/users/timeout' do
95
- # sleep 2 # for typhoeus tests
96
- sleep 1
97
- json 'ok'
98
- end
99
-
100
45
  put '/users/:id' do
101
46
  common_response
102
47
  end
103
-
48
+
104
49
  patch '/users/:id' do
105
50
  common_response
106
51
  end
@@ -110,7 +55,7 @@ class TestServer < Sinatra::Base
110
55
  end
111
56
 
112
57
  not_found do
113
- # binding.pry
58
+ binding.pry
114
59
  end
115
60
 
116
61
 
@@ -123,7 +68,7 @@ class TestServer < Sinatra::Base
123
68
  data.delete('id')
124
69
 
125
70
  query_params = (params[:query_string_param] == 'true')
126
-
71
+
127
72
  internal_data_match = params[:user] ? (params[:user] == data) : true
128
73
 
129
74
  json({ user: { server_response: true }, http_verb: env["REQUEST_METHOD"].downcase, internal_data_match: internal_data_match, query_params: query_params })
@@ -157,8 +102,7 @@ class TestServer < Sinatra::Base
157
102
  end
158
103
 
159
104
  if app_file == $0
160
- # require './spec/spec_helper' unless defined? SmoothOperator
161
- # require './spec/require_helper' unless defined? SmoothOperator
105
+ require './spec/spec_helper' unless defined? SmoothOperator
162
106
  run!
163
107
  end
164
108
 
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.3.0
4
+ version: 1.8.0
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-07-22 00:00:00.000000000 Z
11
+ date: 2014-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,44 +28,30 @@ dependencies:
28
28
  name: json
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - ! '>='
32
32
  - !ruby/object:Gem::Version
33
- version: '1.5'
33
+ version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - ! '>='
39
39
  - !ruby/object:Gem::Version
40
- version: '1.5'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: faraday
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - ! '>'
46
46
  - !ruby/object:Gem::Version
47
- version: '0.8'
47
+ version: 0.9.0.rc5
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
53
- - !ruby/object:Gem::Version
54
- version: '0.8'
55
- - !ruby/object:Gem::Dependency
56
- name: typhoeus
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: '0.6'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ~>
52
+ - - ! '>'
67
53
  - !ruby/object:Gem::Version
68
- version: '0.6'
54
+ version: 0.9.0.rc5
69
55
  description: ActiveResource alternative
70
56
  email:
71
57
  - goncalves.joao@gmail.com
@@ -82,53 +68,34 @@ files:
82
68
  - console.rb
83
69
  - lib/smooth_operator.rb
84
70
  - lib/smooth_operator/array_with_meta_data.rb
85
- - lib/smooth_operator/associations.rb
86
- - lib/smooth_operator/associations/association_reflection.rb
87
- - lib/smooth_operator/associations/has_many_relation.rb
88
- - lib/smooth_operator/associations/reflection.rb
89
71
  - lib/smooth_operator/attribute_assignment.rb
90
- - lib/smooth_operator/cookie_jar.rb
91
72
  - lib/smooth_operator/delegation.rb
92
73
  - lib/smooth_operator/finder_methods.rb
93
74
  - lib/smooth_operator/helpers.rb
94
- - lib/smooth_operator/http_methods.rb
95
- - lib/smooth_operator/internal_data.rb
75
+ - lib/smooth_operator/internal_attribute.rb
76
+ - lib/smooth_operator/model_schema.rb
96
77
  - lib/smooth_operator/open_struct.rb
97
78
  - lib/smooth_operator/operator.rb
98
- - lib/smooth_operator/operators/connection_wrapper.rb
99
- - lib/smooth_operator/operators/faraday.rb
100
- - lib/smooth_operator/operators/typhoeus.rb
101
- - lib/smooth_operator/options.rb
102
79
  - lib/smooth_operator/persistence.rb
103
- - lib/smooth_operator/remote_call/base.rb
104
- - lib/smooth_operator/remote_call/errors/connection_failed.rb
105
- - lib/smooth_operator/remote_call/errors/timeout.rb
106
- - lib/smooth_operator/remote_call/faraday.rb
107
- - lib/smooth_operator/remote_call/typhoeus.rb
108
- - lib/smooth_operator/resource_name.rb
109
- - lib/smooth_operator/schema.rb
80
+ - lib/smooth_operator/remote_call.rb
110
81
  - lib/smooth_operator/serialization.rb
111
82
  - lib/smooth_operator/translation.rb
112
- - lib/smooth_operator/type_casting.rb
83
+ - lib/smooth_operator/type_converter.rb
113
84
  - lib/smooth_operator/validations.rb
114
85
  - lib/smooth_operator/version.rb
115
86
  - smooth_operator.gemspec
116
87
  - spec/factories/user_factory.rb
117
- - spec/require_helper.rb
118
88
  - spec/smooth_operator/attribute_assignment_spec.rb
119
89
  - spec/smooth_operator/delegation_spec.rb
120
90
  - spec/smooth_operator/finder_methods_spec.rb
91
+ - spec/smooth_operator/model_schema_spec.rb
121
92
  - spec/smooth_operator/operator_spec.rb
122
93
  - spec/smooth_operator/persistence_spec.rb
123
- - spec/smooth_operator/remote_call_spec.rb
124
- - spec/smooth_operator/resource_name_spec.rb
125
94
  - spec/smooth_operator/serialization_spec.rb
126
- - spec/smooth_operator/validations_spec.rb
127
95
  - spec/spec_helper.rb
128
96
  - spec/support/helpers/persistence_helper.rb
129
97
  - spec/support/localhost_server.rb
130
98
  - spec/support/models/address.rb
131
- - spec/support/models/comment.rb
132
99
  - spec/support/models/post.rb
133
100
  - spec/support/models/user.rb
134
101
  - spec/support/models/user_with_address_and_posts.rb
@@ -153,28 +120,24 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
120
  version: '0'
154
121
  requirements: []
155
122
  rubyforge_project:
156
- rubygems_version: 2.2.2
123
+ rubygems_version: 2.1.10
157
124
  signing_key:
158
125
  specification_version: 4
159
126
  summary: Simple and fully customizable alternative to ActiveResource, using faraday
160
- gem to stablish remote calls"
127
+ gem to stablish parallel remote calls"
161
128
  test_files:
162
129
  - spec/factories/user_factory.rb
163
- - spec/require_helper.rb
164
130
  - spec/smooth_operator/attribute_assignment_spec.rb
165
131
  - spec/smooth_operator/delegation_spec.rb
166
132
  - spec/smooth_operator/finder_methods_spec.rb
133
+ - spec/smooth_operator/model_schema_spec.rb
167
134
  - spec/smooth_operator/operator_spec.rb
168
135
  - spec/smooth_operator/persistence_spec.rb
169
- - spec/smooth_operator/remote_call_spec.rb
170
- - spec/smooth_operator/resource_name_spec.rb
171
136
  - spec/smooth_operator/serialization_spec.rb
172
- - spec/smooth_operator/validations_spec.rb
173
137
  - spec/spec_helper.rb
174
138
  - spec/support/helpers/persistence_helper.rb
175
139
  - spec/support/localhost_server.rb
176
140
  - spec/support/models/address.rb
177
- - spec/support/models/comment.rb
178
141
  - spec/support/models/post.rb
179
142
  - spec/support/models/user.rb
180
143
  - spec/support/models/user_with_address_and_posts.rb
@@ -1,110 +0,0 @@
1
- require "smooth_operator/associations/has_many_relation"
2
- require "smooth_operator/associations/association_reflection"
3
-
4
- module SmoothOperator
5
- module Associations
6
-
7
- def has_many(association, options = {})
8
- accepts_nested_objects(association, :has_many, options)
9
- end
10
-
11
- def has_one(association, options = {})
12
- accepts_nested_objects(association, :has_one, options)
13
- end
14
-
15
- def belongs_to(association, options = {})
16
- accepts_nested_objects(association, :belongs_to, options)
17
- end
18
-
19
- def reflections
20
- Helpers.get_instance_variable(self, :reflections, {})
21
- end
22
-
23
- def reflect_on_association(association)
24
- reflections[association]
25
- end
26
-
27
- def reflect_on_all_associations(macro = nil)
28
- macro ? reflections.values.select { |reflection| reflection.macro == macro } : reflections.values
29
- end
30
-
31
- def rails_serialization
32
- get_option :rails_serialization, false
33
- end
34
-
35
- protected ###################### PROTECTED ###################
36
-
37
- # TODO: THIS MUST GO TO A HELPER_METHODS MODULE
38
-
39
- def accepts_nested_objects(association, macro, options = {})
40
- options = parse_options(options, { macro: macro })
41
-
42
- reflection = AssociationReflection.new(association, Reflection.new(name, {}), options)
43
-
44
- schema(association => reflection.klass)
45
-
46
- reflections.merge!(association => reflection)
47
-
48
- if reflection.has_many?
49
- define_has_many_association_method(reflection, association)
50
- else
51
- define_single_association_method(reflection, association)
52
- end
53
-
54
- self.send(:attr_reader, "#{association}_attributes".to_sym)
55
-
56
- define_attributes_setter_methods(reflection, association)
57
- end
58
-
59
- private ####################### PRIVATE ######################
60
-
61
- def define_has_many_association_method(reflection, association)
62
- define_method(association) do
63
- has_many_relation = instance_variable_get("@#{association}")
64
-
65
- if has_many_relation.nil?
66
- has_many_relation = HasManyRelation.new(self, association)
67
-
68
- instance_variable_set("@#{association}", has_many_relation)
69
- end
70
-
71
- has_many_relation.send(:refresh)
72
-
73
- has_many_relation
74
- end
75
- end
76
-
77
- def define_single_association_method(reflection, association)
78
- define_method(association) { internal_data_get(association.to_s) }
79
-
80
- define_method("build_#{association}") do |attributes = {}|
81
- new_instance = reflection.klass.new(attributes)
82
-
83
- internal_data_push(association, new_instance)
84
-
85
- new_instance
86
- end
87
- end
88
-
89
- def define_attributes_setter_methods(reflection, association)
90
- define_method("#{association}_attributes=") do |attributes|
91
- instance_variable_set("@#{association}_attributes", attributes)
92
-
93
- attributes = attributes.values if reflection.has_many?
94
-
95
- internal_data_push(association.to_s, attributes)
96
- end
97
- end
98
-
99
- def parse_options(options, default_options)
100
- options = options.is_a?(Hash) ? options.merge(default_options) : default_options
101
-
102
- if options[:rails_serialization].nil?
103
- options[:rails_serialization] = rails_serialization
104
- end
105
-
106
- Helpers.symbolyze_keys(options)
107
- end
108
-
109
- end
110
- end