smooth_operator 0.4.4 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +9 -9
- data/.gitignore +2 -1
- data/.rspec +4 -0
- data/Gemfile +13 -0
- data/README.md +258 -10
- data/console.rb +44 -0
- data/lib/smooth_operator/array_with_meta_data.rb +31 -0
- data/lib/smooth_operator/attribute_assignment.rb +102 -0
- data/lib/smooth_operator/attribute_methods.rb +87 -0
- data/lib/smooth_operator/attributes/base.rb +107 -0
- data/lib/smooth_operator/attributes/dirty.rb +29 -0
- data/lib/smooth_operator/attributes/normal.rb +15 -0
- data/lib/smooth_operator/delegation.rb +60 -0
- data/lib/smooth_operator/finder_methods.rb +43 -0
- data/lib/smooth_operator/helpers.rb +79 -0
- data/lib/smooth_operator/model_schema.rb +81 -0
- data/lib/smooth_operator/open_struct.rb +37 -0
- data/lib/smooth_operator/operator.rb +145 -0
- data/lib/smooth_operator/operators/faraday.rb +75 -0
- data/lib/smooth_operator/operators/typhoeus.rb +77 -0
- data/lib/smooth_operator/persistence.rb +144 -0
- data/lib/smooth_operator/relation/array_relation.rb +13 -0
- data/lib/smooth_operator/relation/association_reflection.rb +75 -0
- data/lib/smooth_operator/relation/associations.rb +75 -0
- data/lib/smooth_operator/relation/reflection.rb +41 -0
- data/lib/smooth_operator/relation/single_relation.rb +14 -0
- data/lib/smooth_operator/remote_call/base.rb +80 -0
- data/lib/smooth_operator/remote_call/errors/connection_failed.rb +20 -0
- data/lib/smooth_operator/remote_call/errors/timeout.rb +20 -0
- data/lib/smooth_operator/remote_call/faraday.rb +19 -0
- data/lib/smooth_operator/remote_call/typhoeus.rb +19 -0
- data/lib/smooth_operator/serialization.rb +79 -0
- data/lib/smooth_operator/translation.rb +27 -0
- data/lib/smooth_operator/validations.rb +15 -0
- data/lib/smooth_operator/version.rb +1 -1
- data/lib/smooth_operator.rb +26 -5
- data/smooth_operator.gemspec +12 -3
- data/spec/factories/user_factory.rb +34 -0
- data/spec/require_helper.rb +11 -0
- data/spec/smooth_operator/attribute_assignment_spec.rb +351 -0
- data/spec/smooth_operator/attributes_dirty_spec.rb +53 -0
- data/spec/smooth_operator/delegation_spec.rb +139 -0
- data/spec/smooth_operator/finder_methods_spec.rb +105 -0
- data/spec/smooth_operator/model_schema_spec.rb +31 -0
- data/spec/smooth_operator/operator_spec.rb +46 -0
- data/spec/smooth_operator/persistence_spec.rb +424 -0
- data/spec/smooth_operator/remote_call_spec.rb +320 -0
- data/spec/smooth_operator/serialization_spec.rb +80 -0
- data/spec/smooth_operator/validations_spec.rb +42 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/support/helpers/persistence_helper.rb +38 -0
- data/spec/support/localhost_server.rb +97 -0
- data/spec/support/models/address.rb +14 -0
- data/spec/support/models/comment.rb +3 -0
- data/spec/support/models/post.rb +13 -0
- data/spec/support/models/user.rb +41 -0
- data/spec/support/models/user_with_address_and_posts.rb +89 -0
- data/spec/support/test_server.rb +165 -0
- metadata +108 -18
- data/lib/smooth_operator/base.rb +0 -30
- data/lib/smooth_operator/core.rb +0 -218
- data/lib/smooth_operator/http_handlers/typhoeus/base.rb +0 -58
- data/lib/smooth_operator/http_handlers/typhoeus/orm.rb +0 -34
- data/lib/smooth_operator/http_handlers/typhoeus/remote_call.rb +0 -28
- data/lib/smooth_operator/operator/base.rb +0 -43
- data/lib/smooth_operator/operator/exceptions.rb +0 -64
- data/lib/smooth_operator/operator/orm.rb +0 -118
- data/lib/smooth_operator/operator/remote_call.rb +0 -84
@@ -0,0 +1,89 @@
|
|
1
|
+
module UserWithAddressAndPosts
|
2
|
+
|
3
|
+
class Father < User::Base
|
4
|
+
|
5
|
+
self.resource_name = 'user'
|
6
|
+
|
7
|
+
schema(
|
8
|
+
posts: Post,
|
9
|
+
address: Address
|
10
|
+
)
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
class Son < Father
|
15
|
+
|
16
|
+
schema(
|
17
|
+
age: :int,
|
18
|
+
dob: :date,
|
19
|
+
price: :float,
|
20
|
+
manager: :bool,
|
21
|
+
date: :datetime,
|
22
|
+
complex_field: nil,
|
23
|
+
first_name: :string
|
24
|
+
)
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
class SoftBehaviour < Son
|
29
|
+
|
30
|
+
self.strict_behaviour = false
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
class WithPatch < Son
|
35
|
+
|
36
|
+
self.update_http_verb = :patch
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
module UserBlackListed
|
42
|
+
|
43
|
+
class Father < ::UserWithAddressAndPosts::Son
|
44
|
+
|
45
|
+
attributes_black_list_add "last_name"
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
class Son < Father
|
50
|
+
|
51
|
+
attributes_black_list_add :admin
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
module UserWhiteListed
|
58
|
+
|
59
|
+
class Father < ::UserWithAddressAndPosts::Son
|
60
|
+
|
61
|
+
attributes_white_list_add "id"
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
class Son < Father
|
66
|
+
|
67
|
+
attributes_white_list_add :first_name
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
class UserWithMyMethod < UserWithAddressAndPosts::Son
|
74
|
+
|
75
|
+
def my_method
|
76
|
+
'my_method'
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
class DirtyAttributes < UserWithAddressAndPosts::Son
|
82
|
+
|
83
|
+
self.dirty_attributes
|
84
|
+
|
85
|
+
self.unknown_hash_class = SmoothOperator::OpenStruct::Dirty
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1,165 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'sinatra/json'
|
3
|
+
require 'pry'
|
4
|
+
|
5
|
+
class TestServer < Sinatra::Base
|
6
|
+
|
7
|
+
use Rack::Auth::Basic, "Restricted Area" do |username, password|
|
8
|
+
username == 'admin' and password == 'admin'
|
9
|
+
end
|
10
|
+
|
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
|
+
get '/users/test_hash_with_array' do
|
23
|
+
status test_hash_with_array
|
24
|
+
end
|
25
|
+
|
26
|
+
post '/users/test_hash_with_array' do
|
27
|
+
status test_hash_with_array
|
28
|
+
end
|
29
|
+
|
30
|
+
get '/users/test_query_string' do
|
31
|
+
status test_query_string
|
32
|
+
end
|
33
|
+
|
34
|
+
post '/users/test_query_string' do
|
35
|
+
status test_query_string
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
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]
|
58
|
+
end
|
59
|
+
|
60
|
+
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
|
+
|
67
|
+
json data
|
68
|
+
end
|
69
|
+
|
70
|
+
get '/users/bad_json' do
|
71
|
+
'ok'
|
72
|
+
end
|
73
|
+
|
74
|
+
get '/users/:id' do
|
75
|
+
json FactoryGirl.attributes_for(:user_with_address_and_posts)
|
76
|
+
end
|
77
|
+
|
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
|
+
|
90
|
+
post '/users' do
|
91
|
+
common_response
|
92
|
+
end
|
93
|
+
|
94
|
+
post '/users/timeout' do
|
95
|
+
# sleep 2 # for typhoeus tests
|
96
|
+
sleep 1
|
97
|
+
json 'ok'
|
98
|
+
end
|
99
|
+
|
100
|
+
put '/users/:id' do
|
101
|
+
common_response
|
102
|
+
end
|
103
|
+
|
104
|
+
patch '/users/:id' do
|
105
|
+
common_response
|
106
|
+
end
|
107
|
+
|
108
|
+
delete '/users/:id' do
|
109
|
+
common_response
|
110
|
+
end
|
111
|
+
|
112
|
+
not_found do
|
113
|
+
# binding.pry
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
protected ################ PROTECTED ################
|
118
|
+
|
119
|
+
def common_response
|
120
|
+
status params[:status]
|
121
|
+
|
122
|
+
data = stringify_data FactoryGirl.attributes_for(:user_with_address_and_posts)
|
123
|
+
data.delete('id')
|
124
|
+
|
125
|
+
query_params = (params[:query_string_param] == 'true')
|
126
|
+
|
127
|
+
internal_data_match = params[:user] ? (params[:user] == data) : true
|
128
|
+
|
129
|
+
json({ user: { server_response: true }, http_verb: env["REQUEST_METHOD"].downcase, internal_data_match: internal_data_match, query_params: query_params })
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_hash_with_array
|
133
|
+
data = stringify_data FactoryGirl.attributes_for(:user_with_address_and_posts)
|
134
|
+
|
135
|
+
params.delete("query_string_param")
|
136
|
+
|
137
|
+
(params == data) ? 200 : 422
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_query_string
|
141
|
+
params[:normal_param] == 'true' && params[:query_string_param] == 'true' ? 200 : 422
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
private ######################### PRIVATE #####################
|
146
|
+
|
147
|
+
def stringify_data(object)
|
148
|
+
if object.is_a?(Hash)
|
149
|
+
data = {}
|
150
|
+
object.each { |key, value| data[key.to_s] = stringify_data(value) }
|
151
|
+
data
|
152
|
+
elsif object.is_a?(Array)
|
153
|
+
object.map { |_value| stringify_data(_value) }
|
154
|
+
else
|
155
|
+
object.to_s
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
if app_file == $0
|
160
|
+
# require './spec/spec_helper' unless defined? SmoothOperator
|
161
|
+
# require './spec/require_helper' unless defined? SmoothOperator
|
162
|
+
run!
|
163
|
+
end
|
164
|
+
|
165
|
+
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:
|
4
|
+
version: 1.2.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-
|
11
|
+
date: 2014-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -25,19 +25,47 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: json
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
|
-
type: :
|
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
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: faraday
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.8.9
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.8.9
|
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.8
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.6.8
|
41
69
|
description: ActiveResource alternative
|
42
70
|
email:
|
43
71
|
- goncalves.joao@gmail.com
|
@@ -46,22 +74,64 @@ extensions: []
|
|
46
74
|
extra_rdoc_files: []
|
47
75
|
files:
|
48
76
|
- .gitignore
|
77
|
+
- .rspec
|
49
78
|
- Gemfile
|
50
79
|
- LICENSE.txt
|
51
80
|
- README.md
|
52
81
|
- Rakefile
|
82
|
+
- console.rb
|
53
83
|
- lib/smooth_operator.rb
|
54
|
-
- lib/smooth_operator/
|
55
|
-
- lib/smooth_operator/
|
56
|
-
- lib/smooth_operator/
|
57
|
-
- lib/smooth_operator/
|
58
|
-
- lib/smooth_operator/
|
59
|
-
- lib/smooth_operator/
|
60
|
-
- lib/smooth_operator/
|
61
|
-
- lib/smooth_operator/
|
62
|
-
- lib/smooth_operator/
|
84
|
+
- lib/smooth_operator/array_with_meta_data.rb
|
85
|
+
- lib/smooth_operator/attribute_assignment.rb
|
86
|
+
- lib/smooth_operator/attribute_methods.rb
|
87
|
+
- lib/smooth_operator/attributes/base.rb
|
88
|
+
- lib/smooth_operator/attributes/dirty.rb
|
89
|
+
- lib/smooth_operator/attributes/normal.rb
|
90
|
+
- lib/smooth_operator/delegation.rb
|
91
|
+
- lib/smooth_operator/finder_methods.rb
|
92
|
+
- lib/smooth_operator/helpers.rb
|
93
|
+
- lib/smooth_operator/model_schema.rb
|
94
|
+
- lib/smooth_operator/open_struct.rb
|
95
|
+
- lib/smooth_operator/operator.rb
|
96
|
+
- lib/smooth_operator/operators/faraday.rb
|
97
|
+
- lib/smooth_operator/operators/typhoeus.rb
|
98
|
+
- lib/smooth_operator/persistence.rb
|
99
|
+
- lib/smooth_operator/relation/array_relation.rb
|
100
|
+
- lib/smooth_operator/relation/association_reflection.rb
|
101
|
+
- lib/smooth_operator/relation/associations.rb
|
102
|
+
- lib/smooth_operator/relation/reflection.rb
|
103
|
+
- lib/smooth_operator/relation/single_relation.rb
|
104
|
+
- lib/smooth_operator/remote_call/base.rb
|
105
|
+
- lib/smooth_operator/remote_call/errors/connection_failed.rb
|
106
|
+
- lib/smooth_operator/remote_call/errors/timeout.rb
|
107
|
+
- lib/smooth_operator/remote_call/faraday.rb
|
108
|
+
- lib/smooth_operator/remote_call/typhoeus.rb
|
109
|
+
- lib/smooth_operator/serialization.rb
|
110
|
+
- lib/smooth_operator/translation.rb
|
111
|
+
- lib/smooth_operator/validations.rb
|
63
112
|
- lib/smooth_operator/version.rb
|
64
113
|
- smooth_operator.gemspec
|
114
|
+
- spec/factories/user_factory.rb
|
115
|
+
- spec/require_helper.rb
|
116
|
+
- spec/smooth_operator/attribute_assignment_spec.rb
|
117
|
+
- spec/smooth_operator/attributes_dirty_spec.rb
|
118
|
+
- spec/smooth_operator/delegation_spec.rb
|
119
|
+
- spec/smooth_operator/finder_methods_spec.rb
|
120
|
+
- spec/smooth_operator/model_schema_spec.rb
|
121
|
+
- spec/smooth_operator/operator_spec.rb
|
122
|
+
- spec/smooth_operator/persistence_spec.rb
|
123
|
+
- spec/smooth_operator/remote_call_spec.rb
|
124
|
+
- spec/smooth_operator/serialization_spec.rb
|
125
|
+
- spec/smooth_operator/validations_spec.rb
|
126
|
+
- spec/spec_helper.rb
|
127
|
+
- spec/support/helpers/persistence_helper.rb
|
128
|
+
- spec/support/localhost_server.rb
|
129
|
+
- spec/support/models/address.rb
|
130
|
+
- spec/support/models/comment.rb
|
131
|
+
- spec/support/models/post.rb
|
132
|
+
- spec/support/models/user.rb
|
133
|
+
- spec/support/models/user_with_address_and_posts.rb
|
134
|
+
- spec/support/test_server.rb
|
65
135
|
homepage: https://github.com/goncalvesjoao/smooth_operator
|
66
136
|
licenses:
|
67
137
|
- MIT
|
@@ -82,10 +152,30 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
152
|
version: '0'
|
83
153
|
requirements: []
|
84
154
|
rubyforge_project:
|
85
|
-
rubygems_version: 2.
|
155
|
+
rubygems_version: 2.1.10
|
86
156
|
signing_key:
|
87
157
|
specification_version: 4
|
88
|
-
summary: Simple and fully customizable alternative to ActiveResource,
|
89
|
-
gem
|
90
|
-
test_files:
|
91
|
-
|
158
|
+
summary: Simple and fully customizable alternative to ActiveResource, using faraday
|
159
|
+
gem to stablish remote calls"
|
160
|
+
test_files:
|
161
|
+
- spec/factories/user_factory.rb
|
162
|
+
- spec/require_helper.rb
|
163
|
+
- spec/smooth_operator/attribute_assignment_spec.rb
|
164
|
+
- spec/smooth_operator/attributes_dirty_spec.rb
|
165
|
+
- spec/smooth_operator/delegation_spec.rb
|
166
|
+
- spec/smooth_operator/finder_methods_spec.rb
|
167
|
+
- spec/smooth_operator/model_schema_spec.rb
|
168
|
+
- spec/smooth_operator/operator_spec.rb
|
169
|
+
- spec/smooth_operator/persistence_spec.rb
|
170
|
+
- spec/smooth_operator/remote_call_spec.rb
|
171
|
+
- spec/smooth_operator/serialization_spec.rb
|
172
|
+
- spec/smooth_operator/validations_spec.rb
|
173
|
+
- spec/spec_helper.rb
|
174
|
+
- spec/support/helpers/persistence_helper.rb
|
175
|
+
- spec/support/localhost_server.rb
|
176
|
+
- spec/support/models/address.rb
|
177
|
+
- spec/support/models/comment.rb
|
178
|
+
- spec/support/models/post.rb
|
179
|
+
- spec/support/models/user.rb
|
180
|
+
- spec/support/models/user_with_address_and_posts.rb
|
181
|
+
- spec/support/test_server.rb
|
data/lib/smooth_operator/base.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
require "smooth_operator/operator/base"
|
2
|
-
require "smooth_operator/operator/orm"
|
3
|
-
|
4
|
-
require "smooth_operator/http_handlers/typhoeus/base"
|
5
|
-
require "smooth_operator/http_handlers/typhoeus/orm"
|
6
|
-
|
7
|
-
module SmoothOperator
|
8
|
-
|
9
|
-
class Base < OpenStruct
|
10
|
-
include SmoothOperator::Core
|
11
|
-
include SmoothOperator::Operator::Base
|
12
|
-
include SmoothOperator::Operator::ORM
|
13
|
-
|
14
|
-
protected ################### PROTECTED ###################
|
15
|
-
|
16
|
-
def self.http_handler_base
|
17
|
-
SmoothOperator::HttpHandlers::Typhoeus::Base
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.http_handler_orm
|
21
|
-
@http_handler_orm ||= SmoothOperator::HttpHandlers::Typhoeus::ORM.new(self)
|
22
|
-
end
|
23
|
-
|
24
|
-
def http_handler_orm
|
25
|
-
self.class.http_handler_orm
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
data/lib/smooth_operator/core.rb
DELETED
@@ -1,218 +0,0 @@
|
|
1
|
-
module SmoothOperator
|
2
|
-
module Core
|
3
|
-
|
4
|
-
def table_hash
|
5
|
-
@table
|
6
|
-
end
|
7
|
-
|
8
|
-
def as_json(options = nil)
|
9
|
-
@table.as_json(options)
|
10
|
-
end
|
11
|
-
|
12
|
-
def new_record?
|
13
|
-
!persisted?
|
14
|
-
end
|
15
|
-
|
16
|
-
def persisted?
|
17
|
-
try(:id).present?
|
18
|
-
end
|
19
|
-
|
20
|
-
def to_partial_path
|
21
|
-
class_name_plural = self.class.table_name
|
22
|
-
"#{class_name_plural}/#{class_name_plural.singularize}"
|
23
|
-
end
|
24
|
-
|
25
|
-
def valid?(context = nil)
|
26
|
-
errors.blank?
|
27
|
-
end
|
28
|
-
|
29
|
-
def invalid?
|
30
|
-
!valid?
|
31
|
-
end
|
32
|
-
|
33
|
-
def assign_attributes(attributes = {})
|
34
|
-
return if attributes.blank?
|
35
|
-
|
36
|
-
attributes.each do |name, value|
|
37
|
-
send("#{name}=", value)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def safe_table_hash
|
42
|
-
safe_hash = table_hash.dup
|
43
|
-
|
44
|
-
if self.class.save_attr_white_list.present?
|
45
|
-
safe_hash.slice!(*self.class.save_attr_white_list)
|
46
|
-
else
|
47
|
-
self.class.save_attr_black_list.each { |attribute| safe_hash.delete(attribute) }
|
48
|
-
end
|
49
|
-
|
50
|
-
safe_hash
|
51
|
-
end
|
52
|
-
|
53
|
-
private ######################### PRIVATE ###############################
|
54
|
-
|
55
|
-
def get_nested_object_variable(nested_object_symbol, nested_object_class_or_options)
|
56
|
-
nested_object_variable = instance_variable_get("@#{nested_object_symbol}")
|
57
|
-
|
58
|
-
return nested_object_variable if nested_object_variable.present?
|
59
|
-
|
60
|
-
nested_object_variable = initialize_nested_object_variable(table_hash[nested_object_symbol], nested_object_class_or_options, nested_object_symbol)
|
61
|
-
|
62
|
-
instance_variable_set("@#{nested_object_symbol}", nested_object_variable)
|
63
|
-
|
64
|
-
nested_object_variable
|
65
|
-
end
|
66
|
-
|
67
|
-
def initialize_nested_object_variable(attributes, nested_object_class_or_options, nested_object_symbol)
|
68
|
-
if attributes.kind_of?(Array)
|
69
|
-
get_nested_objects(attributes, nested_object_class_or_options, nested_object_symbol)
|
70
|
-
else
|
71
|
-
get_nested_object(attributes, nested_object_class_or_options, nested_object_symbol)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def get_nested_objects(nested_objects_attributes, nested_object_class_or_options, nested_object_symbol)
|
76
|
-
nested_objects = nested_objects_attributes.map { |attributes| get_nested_object(attributes, nested_object_class_or_options, nested_object_symbol) }
|
77
|
-
|
78
|
-
if nested_object_class_or_options.kind_of?(Hash)
|
79
|
-
nested_objects = order_by(nested_objects, nested_object_class_or_options[:order_by], nested_object_class_or_options[:order])
|
80
|
-
end
|
81
|
-
|
82
|
-
nested_objects
|
83
|
-
end
|
84
|
-
|
85
|
-
def get_nested_object(nested_objects_attributes, nested_object_class_or_options, nested_object_symbol)
|
86
|
-
return (plural?(nested_object_symbol) ? [] : nil) if nested_objects_attributes.blank?
|
87
|
-
|
88
|
-
return get_nested_object_according_to_options(nested_objects_attributes, nested_object_class_or_options, nested_object_symbol) if nested_object_class_or_options.kind_of?(Hash)
|
89
|
-
|
90
|
-
nested_object_class_or_options.new(nested_objects_attributes)
|
91
|
-
end
|
92
|
-
|
93
|
-
def get_nested_object_according_to_options(nested_objects_attributes, options, nested_object_symbol)
|
94
|
-
nested_objects_attributes = options[:parser].call(nested_objects_attributes) if options[:parser].present?
|
95
|
-
|
96
|
-
begin
|
97
|
-
options[:class].new(nested_objects_attributes)
|
98
|
-
rescue
|
99
|
-
options[:default].present? ? options[:default] : nested_objects_attributes
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def plural?(string)
|
104
|
-
string = string.to_s
|
105
|
-
string == string.pluralize
|
106
|
-
end
|
107
|
-
|
108
|
-
def order_by(list, method, order)
|
109
|
-
return list if method.blank? || order.blank?
|
110
|
-
|
111
|
-
if order == :asc
|
112
|
-
list.sort { |a, b| a.send(method) <=> b.send(method) }
|
113
|
-
elsif order == :desc
|
114
|
-
list.sort { |a, b| b.send(method) <=> a.send(method) }
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
|
119
|
-
def self.included(base)
|
120
|
-
base.extend(ClassMethods)
|
121
|
-
end
|
122
|
-
|
123
|
-
module ClassMethods
|
124
|
-
|
125
|
-
def nested_objects_classes(hash)
|
126
|
-
hash.each do |nested_object_symbol, nested_object_class_or_options|
|
127
|
-
define_method(nested_object_symbol.to_s) do
|
128
|
-
get_nested_object_variable(nested_object_symbol, nested_object_class_or_options)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
attr_writer :endpoint
|
134
|
-
def endpoint
|
135
|
-
if defined?(@endpoint)
|
136
|
-
@endpoint
|
137
|
-
else
|
138
|
-
@endpoint = superclass_responds_to?(:endpoint) ? superclass.endpoint : ENV["API_ENDPOINT"]
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
attr_writer :endpoint_user
|
143
|
-
def endpoint_user
|
144
|
-
if defined?(@endpoint_user)
|
145
|
-
@endpoint_user
|
146
|
-
else
|
147
|
-
@endpoint_user = superclass_responds_to?(:endpoint_user) ? superclass.endpoint_user : ENV["API_USER"]
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
attr_writer :endpoint_pass
|
152
|
-
def endpoint_pass
|
153
|
-
if defined?(@endpoint_pass)
|
154
|
-
@endpoint_pass
|
155
|
-
else
|
156
|
-
@endpoint_pass = superclass_responds_to?(:endpoint_pass) ? superclass.endpoint_pass : ENV["API_PASS"]
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
attr_writer :save_attr_black_list
|
161
|
-
def save_attr_black_list
|
162
|
-
@save_attr_black_list ||= [:id, :created_at, :updated_at, :errors]
|
163
|
-
end
|
164
|
-
|
165
|
-
attr_writer :save_attr_white_list
|
166
|
-
def save_attr_white_list
|
167
|
-
@save_attr_white_list ||= []
|
168
|
-
end
|
169
|
-
|
170
|
-
attr_writer :model_name
|
171
|
-
def model_name
|
172
|
-
@model_name ||= name.split('::').last.underscore.capitalize
|
173
|
-
end
|
174
|
-
|
175
|
-
def model_name_downcase
|
176
|
-
model_name.downcase
|
177
|
-
end
|
178
|
-
|
179
|
-
attr_writer :table_name
|
180
|
-
def table_name
|
181
|
-
@table_name ||= model_name_downcase.to_s.pluralize
|
182
|
-
end
|
183
|
-
|
184
|
-
def human_attribute_name(attribute_key_name, options = {})
|
185
|
-
defaults = []
|
186
|
-
defaults << "smooth_operator.attributes.#{name.underscore}.#{attribute_key_name}"
|
187
|
-
defaults << "activerecord.attributes.#{name.underscore}.#{attribute_key_name}".to_sym
|
188
|
-
defaults << options[:default] if options[:default]
|
189
|
-
defaults.flatten!
|
190
|
-
defaults << attribute_key_name.to_s.humanize
|
191
|
-
options[:count] ||= 1
|
192
|
-
I18n.translate(defaults.shift, options.merge(default: defaults))
|
193
|
-
end
|
194
|
-
|
195
|
-
private ################################ PRIVATE #########################
|
196
|
-
|
197
|
-
def superclass_responds_to?(method_name)
|
198
|
-
superclass.name.split('::')[0] != 'SmoothOperator' && superclass.respond_to?(method_name)
|
199
|
-
end
|
200
|
-
|
201
|
-
def build_url(relative_path)
|
202
|
-
slash = '/' if relative_path.present?
|
203
|
-
extention = '.json'
|
204
|
-
[endpoint, table_name, slash, relative_path.to_s, extention].join
|
205
|
-
end
|
206
|
-
|
207
|
-
def get_basic_auth_credentials
|
208
|
-
if endpoint_user.present? || endpoint_pass.present?
|
209
|
-
{ username: endpoint_user, password: endpoint_pass }
|
210
|
-
else
|
211
|
-
nil
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
end
|
216
|
-
|
217
|
-
end
|
218
|
-
end
|