dbAccessor 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.autotest +2 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/dbAccessor.gemspec +34 -0
- data/lib/dbAccessor/base.rb +100 -0
- data/lib/dbAccessor/dbConnector.rb +14 -0
- data/lib/dbAccessor/deleter/deleter.rb +73 -0
- data/lib/dbAccessor/reader/reader.rb +81 -0
- data/lib/dbAccessor/updater/updater.rb +83 -0
- data/lib/dbAccessor/version.rb +3 -0
- data/lib/dbAccessor/writer/writer.rb +70 -0
- data/lib/dbAccessor.rb +20 -0
- data/spec/deleter_spec.rb +90 -0
- data/spec/feed/dbTest/migration/add_deleted_to_utilisateur.rb +1 -0
- data/spec/feed/dbTest/migration/create_table_actions.rb +6 -0
- data/spec/feed/dbTest/migration/create_table_applications.rb +21 -0
- data/spec/feed/dbTest/migration/create_table_items.rb +4 -0
- data/spec/feed/dbTest/migration/create_table_users.rb +22 -0
- data/spec/feed/dbTest/migration/create_table_utilisateurs.rb +4 -0
- data/spec/feed/dbTest/model/action.rb +8 -0
- data/spec/feed/dbTest/model/application.rb +10 -0
- data/spec/feed/dbTest/model/item.rb +8 -0
- data/spec/feed/dbTest/model/user.rb +9 -0
- data/spec/feed/dbTest/model/utilisateur.rb +8 -0
- data/spec/feed/resources/do_migration.rb +5 -0
- data/spec/feed/resources/environment.rb +3 -0
- data/spec/feed/resources/populate.rb +28 -0
- data/spec/fixtures/env.rb +1 -0
- data/spec/reader_spec.rb +85 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/updater_spec.rb +96 -0
- data/spec/writer_spec.rb +72 -0
- metadata +288 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7779c4a2bb87572cd744bc3fb1f6a7ec72104936
|
4
|
+
data.tar.gz: ead38bab42b71e9df42c7539c57024ba298673ad
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7d9f4826add8a051d3258e64817b88a84153184982c1b091fddd542c1cb07a20cb3fa84713b4d78fb25d32583347387fff02007d20b6105ab4b107bd759e18f9
|
7
|
+
data.tar.gz: 740af56e79f80c311ca395cfb0bb8b46766bba327003dbd22f7edacf3bfbc3e04c2b4fe3270988cfc86dc3ac4706555890fc6d0364f58a1698a6d2bf694f5ed3
|
data/.autotest
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 fabira
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# DbAccessor
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'dbAccessor'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install dbAccessor
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/dbAccessor/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/dbAccessor.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dbAccessor/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dbAccessor"
|
8
|
+
spec.version = DbAccessor::VERSION
|
9
|
+
spec.authors = ["fabira"]
|
10
|
+
spec.email = ["fabira90@gmail.com"]
|
11
|
+
spec.summary = %q{"Module dbAccessor"}
|
12
|
+
spec.description = %q{"Module d'acces a la base de donnees "}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# Declare that the Gem is compatible with
|
22
|
+
# version 2.0 or greater
|
23
|
+
spec.required_ruby_version = ">= 2.0"
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
25
|
+
spec.add_development_dependency "autotest", "~> 4.4", ">= 4.4.6"
|
26
|
+
spec.add_development_dependency "uuid", "~> 2.3", ">= 2.3.7"
|
27
|
+
spec.add_development_dependency "autotest-fsevent", "~> 0.2", ">= 0.2.9"
|
28
|
+
spec.add_development_dependency "autotest-growl", "~> 0.2", ">= 0.2.16"
|
29
|
+
spec.add_development_dependency "ZenTest", "~> 4.10", ">= 4.10.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 2.6"
|
31
|
+
spec.add_development_dependency "activerecord", "~> 4.1", ">= 4.1.1"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.3", ">= 10.3.2"
|
33
|
+
spec.add_development_dependency "pg", "~> 0.17", ">= 0.17.1"
|
34
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
module Db_Accessor
|
2
|
+
|
3
|
+
class Base
|
4
|
+
USER_MODEL = 'User'
|
5
|
+
APP_MODEL = 'Application'
|
6
|
+
app_columns = [:id,:name,:api_key,:url,:user_id]
|
7
|
+
user_columns = [:id,:login,:password,:firstname,:lastname,:gender,:email]
|
8
|
+
|
9
|
+
def self.validate_argument(given_param)
|
10
|
+
resp = nil
|
11
|
+
given_param.values.each do |v|
|
12
|
+
if v.nil?
|
13
|
+
resp = true
|
14
|
+
break
|
15
|
+
elsif v.is_a?(String) && v.empty?
|
16
|
+
resp = true
|
17
|
+
break
|
18
|
+
else
|
19
|
+
resp = false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
return resp
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.return_response(code,msg)
|
26
|
+
{code: code,message: msg}
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.get_model_name(object_param)
|
30
|
+
Object.const_get(object_param[:model_name])
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.is_model?(param)
|
34
|
+
param == USER_MODEL || param == APP_MODEL
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.valid_hash?(param)
|
38
|
+
param.is_a?(Hash)
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def self.valid_keys?(param,waited_keys)
|
43
|
+
waited_keys.each do |k|
|
44
|
+
if !(param.has_key?(k))
|
45
|
+
return false
|
46
|
+
break
|
47
|
+
end
|
48
|
+
end
|
49
|
+
return true
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.verify_keys(keys,attributes)
|
53
|
+
keys.each do |k|
|
54
|
+
if !(attributes.include?(k))
|
55
|
+
return false
|
56
|
+
break
|
57
|
+
end
|
58
|
+
end
|
59
|
+
return true
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.valid_object_columns?(model_name,content)
|
63
|
+
keys = content.keys
|
64
|
+
user_attributes = [:id,:login,:password,:firstname,:lastname,:gender,:email]
|
65
|
+
app_attributes = [:id,:api_key,:name,:url,:user_id]
|
66
|
+
model_name == "User" ? attributes = user_attributes : attributes = app_attributes
|
67
|
+
verify_keys(keys,attributes)
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.valid_class?(keys,columns,attributes)
|
71
|
+
keys.each do |k|
|
72
|
+
if columns.include?(k)
|
73
|
+
if !(attributes[k].is_a?(String))
|
74
|
+
return false
|
75
|
+
break
|
76
|
+
end
|
77
|
+
else
|
78
|
+
if !(attributes[k].is_a?(Fixnum))
|
79
|
+
return false
|
80
|
+
break
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
return true
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.valid_attributes_values?(attributes)
|
88
|
+
string_columns = [:login,:password,:firstname,:lastname,:gender,:email,:api_key,:name,:url]
|
89
|
+
fixnum_columns = [:user_id,:id]
|
90
|
+
keys = attributes.keys
|
91
|
+
valid_class?(keys,string_columns,attributes)
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'pg'
|
3
|
+
|
4
|
+
ActiveRecord::Base.establish_connection(
|
5
|
+
adapter: 'postgresql',
|
6
|
+
host: 'localhost',
|
7
|
+
username: 'vagrant',
|
8
|
+
password: 'vagrant',
|
9
|
+
encoding: 'utf8',
|
10
|
+
pool: 5,
|
11
|
+
port: 5432,
|
12
|
+
database: 'nelam_recommend'
|
13
|
+
)
|
14
|
+
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require_relative '../../../spec/feed/resources/environment'
|
2
|
+
require_relative '../base'
|
3
|
+
|
4
|
+
|
5
|
+
class DbAccessor::Deleter < Db_Accessor::Base
|
6
|
+
|
7
|
+
|
8
|
+
def self.delete_all(given_object)
|
9
|
+
waited_keys = [:model_name,:condition]
|
10
|
+
if validate(given_object,waited_keys) && valid_condition?(given_object)
|
11
|
+
model = get_model_name(given_object)
|
12
|
+
condition = given_object[:condition]
|
13
|
+
condition == {} ? model.update_all("deleted = true") : model.where(condition).update_all("deleted = true")
|
14
|
+
return_response(200,"success")
|
15
|
+
else
|
16
|
+
return_response(400,"Not deleted")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
def self.delete_by_id(given_param)
|
22
|
+
waited_keys = [:model_name,:id]
|
23
|
+
if validate(given_param,waited_keys) && validate_id(given_param[:id])
|
24
|
+
model_name = get_model_name(given_param)
|
25
|
+
objects = model_name.find(given_param[:id])
|
26
|
+
objects.each do |o|
|
27
|
+
o.deleted=true
|
28
|
+
o.save
|
29
|
+
end
|
30
|
+
return_response(200,"success")
|
31
|
+
else
|
32
|
+
return_response(400,"Not deleted")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.validate(param,waited_keys)
|
37
|
+
param.is_a?(Hash) && valid_keys?(param,waited_keys) && !validate_argument(param) && is_model?(param[:model_name])
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.validate_id(param)
|
41
|
+
if param.is_a?(Array)
|
42
|
+
param.each do |v|
|
43
|
+
if !(v.is_a?(Fixnum))
|
44
|
+
return false
|
45
|
+
break
|
46
|
+
end
|
47
|
+
end
|
48
|
+
else
|
49
|
+
return false
|
50
|
+
end
|
51
|
+
return true
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.valid_condition?(param)
|
55
|
+
condition = param[:condition]
|
56
|
+
model_name = param[:model_name]
|
57
|
+
if condition.is_a?(Hash)
|
58
|
+
if !(condition.empty?)
|
59
|
+
keys = condition.keys
|
60
|
+
values = condition.values
|
61
|
+
if !(valid_object_columns?(model_name,condition)) || validate_argument(condition) || !(valid_attributes_values?(condition))
|
62
|
+
return false
|
63
|
+
end
|
64
|
+
else
|
65
|
+
return true
|
66
|
+
end
|
67
|
+
else
|
68
|
+
return false
|
69
|
+
end
|
70
|
+
return true
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require_relative '../../../spec/feed/resources/environment'
|
2
|
+
require_relative '../base'
|
3
|
+
|
4
|
+
class DbAccessor::Reader < Db_Accessor::Base
|
5
|
+
|
6
|
+
|
7
|
+
def self.simple_select(given_param)
|
8
|
+
waited_keys = [:model_name,:action_on]
|
9
|
+
if validate(given_param,waited_keys)
|
10
|
+
result = get_model_name(given_param).where(deleted: false).send(given_param[:action_on])
|
11
|
+
result.nil? ? return_response(404,"Not found") : return_response(200,result)
|
12
|
+
else
|
13
|
+
return_response(404,"Not found")
|
14
|
+
end
|
15
|
+
rescue ActiveRecord::RecordNotFound => e
|
16
|
+
return_response(404,"Not found")
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def self.condition_select(given_param)
|
21
|
+
waited_keys = [:model_name,:condition]
|
22
|
+
if validate(given_param,waited_keys)
|
23
|
+
result = get_model_name(given_param).where(given_param[:condition],deleted: false)
|
24
|
+
( result.nil? || result == []) ? return_response(404,"Not found") : return_response(200,result)
|
25
|
+
else
|
26
|
+
return_response(404,"Not found")
|
27
|
+
end
|
28
|
+
rescue ActiveRecord::RecordNotFound => e
|
29
|
+
return_response(400,"Not found")
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
def self.select_by_id(given_param)
|
34
|
+
waited_keys = [:model_name,:identifiers]
|
35
|
+
if validate(given_param,waited_keys) && validate_id(given_param[:identifiers])
|
36
|
+
model_name = get_model_name(given_param)
|
37
|
+
result = model_name.where(deleted: false).find(given_param[:identifiers])
|
38
|
+
result.nil? ? return_response(404,"Not found") : return_response(200,result)
|
39
|
+
else
|
40
|
+
return_response(404,"Not found")
|
41
|
+
end
|
42
|
+
rescue ActiveRecord::RecordNotFound => e
|
43
|
+
return_response(404,"Not found")
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
def self.linked_models_select(given_param)
|
49
|
+
waited_keys = [:model_name,:condition,:models]
|
50
|
+
if validate(given_param,waited_keys)
|
51
|
+
model_name = get_model_name(given_param)
|
52
|
+
condition = given_param[:condition]
|
53
|
+
models = given_param[:models]
|
54
|
+
result = model_name.where(condition,deleted: false).first
|
55
|
+
result.nil? ? return_response(404,"Not found") : return_response(200,result.send(models))
|
56
|
+
else
|
57
|
+
return_response(404,"Not found")
|
58
|
+
end
|
59
|
+
rescue ActiveRecord::RecordNotFound => e
|
60
|
+
return_response(400,"Not found")
|
61
|
+
end
|
62
|
+
private ##################################################################
|
63
|
+
|
64
|
+
def self.validate(param,waited_keys)
|
65
|
+
valid_hash?(param) && valid_keys?(param,waited_keys) && !validate_argument(param) && is_model?(param[:model_name])
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.validate_id(param)
|
69
|
+
test_id = lambda do |param|
|
70
|
+
param.each do |i|
|
71
|
+
if !(i.is_a?(Fixnum))
|
72
|
+
return false
|
73
|
+
break
|
74
|
+
end
|
75
|
+
end
|
76
|
+
return true
|
77
|
+
end
|
78
|
+
param.is_a?(Array) && test_id.call(param)
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require_relative '../../../spec/feed/resources/environment'
|
2
|
+
require_relative '../base'
|
3
|
+
|
4
|
+
class DbAccessor::Updater < Db_Accessor::Base
|
5
|
+
|
6
|
+
#updates given object(s) with given attributes using object's identifier named 'id'
|
7
|
+
def self.update_by_id(given_object)
|
8
|
+
waited_keys = [:model_name,:attributes]
|
9
|
+
if validate(given_object,waited_keys) && valid_attributes_pattern?(given_object[:model_name],given_object[:attributes])
|
10
|
+
get_model_name(given_object).update(get_attributes(given_object).keys,get_attributes(given_object).values)
|
11
|
+
return_response(200,"Succesfully updated")
|
12
|
+
else
|
13
|
+
return_response(400,"Not updated")
|
14
|
+
end
|
15
|
+
rescue ActiveRecord::RecordNotFound => e
|
16
|
+
return_response(400,"Not updated")
|
17
|
+
end
|
18
|
+
|
19
|
+
#updates all model's objects or model's objects that satisfy the given condition with given attributes
|
20
|
+
def self.update_all_objects(given_param,condition="")
|
21
|
+
waited_keys = [:model_name,:attributes]
|
22
|
+
if validate(given_param,waited_keys) && valid_object_columns?(given_param[:model_name],given_param[:attributes]) && valid_attributes_values?(given_param[:attributes])
|
23
|
+
|
24
|
+
|
25
|
+
model_name=Object.const_get(given_param[:model_name])
|
26
|
+
model_name.update_all(given_param[:attributes]) if condition.empty?
|
27
|
+
model_name.update_all(given_param[:attributes],condition) if condition.is_a?(String) && !condition.empty?
|
28
|
+
|
29
|
+
return_response(200,"Successfully updated")
|
30
|
+
else
|
31
|
+
return_response(400,"Not updated")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private ###################################################################
|
36
|
+
|
37
|
+
def self.get_attributes(given_param)
|
38
|
+
given_param[:attributes]
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.validate_attributes(given_param)
|
42
|
+
given_param.is_a?(Hash) && !given_param.empty?
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
def self.valid_fixnum_keys?(keys)
|
47
|
+
keys.each do |k|
|
48
|
+
if !(k.is_a?(Fixnum))
|
49
|
+
return false
|
50
|
+
break
|
51
|
+
end
|
52
|
+
end
|
53
|
+
return true
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.valid_values_pattern?(values,model_name)
|
57
|
+
values.each do |v|
|
58
|
+
if !(v.nil?)
|
59
|
+
if v.empty? || !(v.is_a?(Hash)) || !valid_object_columns?(model_name,v) || !valid_attributes_values?(v)
|
60
|
+
return false
|
61
|
+
break
|
62
|
+
end
|
63
|
+
else
|
64
|
+
return false
|
65
|
+
break
|
66
|
+
end
|
67
|
+
end
|
68
|
+
return true
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.valid_attributes_pattern?(model_name,attributes)
|
72
|
+
keys = attributes.keys
|
73
|
+
values = attributes.values
|
74
|
+
if valid_fixnum_keys?(keys) && valid_values_pattern?(values,model_name)
|
75
|
+
return true
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.validate(param,waited_keys)
|
80
|
+
param.is_a?(Hash) && valid_keys?(param,waited_keys) && is_model?(param[:model_name]) && validate_attributes(param[:attributes]) && !validate_argument(param)
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require_relative '../../../spec/feed/resources/environment'
|
2
|
+
require_relative '../base'
|
3
|
+
|
4
|
+
class DbAccessor::Writer < Db_Accessor::Base
|
5
|
+
|
6
|
+
USER_MODEL = 'User'
|
7
|
+
APP_MODEL = 'Application'
|
8
|
+
|
9
|
+
def self.write_object(object_param)
|
10
|
+
waited_keys =[:model_name,:content]
|
11
|
+
if validate(object_param,waited_keys)
|
12
|
+
@given_object = get_object(object_param)
|
13
|
+
@given_object.save
|
14
|
+
!@given_object.new_record? ? code = 201 : code = 400
|
15
|
+
return_response(code,"Success")
|
16
|
+
else
|
17
|
+
return_response(400,"Failed")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private ######################################################################
|
22
|
+
|
23
|
+
def self.get_content(object_param)
|
24
|
+
object_param[:content]
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.get_object(object_param)
|
28
|
+
get_model_name(object_param).new(get_content(object_param))
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.validate_required_params(param)
|
32
|
+
content = param[:content]
|
33
|
+
model_name = param[:model_name]
|
34
|
+
if model_name == 'User'
|
35
|
+
email_pattern = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
|
36
|
+
content[:login].is_a?(String) && content[:password].is_a?(String) && content[:email].is_a?(String) && !(email_pattern.match(content[:email])).nil?
|
37
|
+
elsif model_name == 'Application'
|
38
|
+
content[:name].is_a?(String) && content[:api_key].is_a?(String) && content[:url].is_a?(String)
|
39
|
+
else
|
40
|
+
false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
def self.validate(param,waited_keys)
|
46
|
+
valid_hash?(param) && valid_keys?(param,waited_keys) && is_model?(param[:model_name]) && valid_object_columns?(param[:model_name],param[:content]) && !validate_argument(param) && !validate_argument(param[:content]) && validate_required_params(param)
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
data/lib/dbAccessor.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "dbAccessor/version"
|
2
|
+
require "dbAccessor/writer/writer"
|
3
|
+
require "dbAccessor/reader/reader"
|
4
|
+
require "dbAccessor/updater/updater"
|
5
|
+
require "dbAccessor/deleter/deleter"
|
6
|
+
|
7
|
+
module DbAccessor
|
8
|
+
|
9
|
+
class Writer
|
10
|
+
end
|
11
|
+
|
12
|
+
class Reader
|
13
|
+
end
|
14
|
+
|
15
|
+
class Updater
|
16
|
+
end
|
17
|
+
|
18
|
+
class Deleter
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe DbAccessor::Deleter do
|
4
|
+
|
5
|
+
describe ".delete_all" do
|
6
|
+
given_param = { model_name: "User",condition: {id: 5}}
|
7
|
+
context "when the object is deleted" do
|
8
|
+
it {expect((DbAccessor::Deleter.delete_all(given_param))[:code]).to eq 200 }
|
9
|
+
|
10
|
+
it {expect((DbAccessor::Deleter.delete_all({model_name: "Application",condition: {name: 'Wikia'}}))[:code]).to eq 200 }
|
11
|
+
|
12
|
+
it {expect((DbAccessor::Deleter.delete_all({model_name: "User",condition: {login: 'fabira',password: 'passera'}}))[:code]).to eq 200 }
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when the conditon format is not valid" do
|
16
|
+
param = {model_name: "User",condition: {ta: 1}}
|
17
|
+
|
18
|
+
it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
|
19
|
+
|
20
|
+
param[:condition] = "coucou"
|
21
|
+
it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
|
22
|
+
|
23
|
+
param[:condition] = nil
|
24
|
+
it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
|
25
|
+
|
26
|
+
param[:condition] = {login: 1}
|
27
|
+
it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
|
28
|
+
|
29
|
+
param[:model_name] = "Application"
|
30
|
+
param[:condition] = {login: 'fabi'}
|
31
|
+
it {expect((DbAccessor::Deleter.delete_all(param))[:code]).to eq 400 }
|
32
|
+
end
|
33
|
+
|
34
|
+
context "when model name is invalid" do
|
35
|
+
it {expect((DbAccessor::Deleter.delete_all({model_name: "A",condition: {}}))[:code]).to eq 400 }
|
36
|
+
end
|
37
|
+
|
38
|
+
context "when one of the parameter's keys is invalid" do
|
39
|
+
it {expect((DbAccessor::Deleter.delete_all({model: "User",condition: {}}))[:code]).to eq 400 }
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when the object which satisfy the given condition is deleted" do
|
43
|
+
it {expect((DbAccessor::Deleter.delete_all({model_name: "User",condition: {id: 4}}))[:code]).to eq 200 }
|
44
|
+
|
45
|
+
it {expect((DbAccessor::Deleter.delete_all({model_name: "Application",condition: {id: 4}}))[:code]).to eq 200 }
|
46
|
+
end
|
47
|
+
|
48
|
+
context "when one or many parameters are empty or nil" do
|
49
|
+
it {expect((DbAccessor::Deleter.delete_all({model_name: nil,condition: {id: 3}}))[:code]).to eq 400 }
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
describe ".delete_by_id" do
|
55
|
+
context "when the object find by id is deleted" do
|
56
|
+
given_param={model_name: "Application", id: [4]}
|
57
|
+
it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 200 }
|
58
|
+
|
59
|
+
given_param={model_name: "User", id: [4]}
|
60
|
+
it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 200 }
|
61
|
+
end
|
62
|
+
|
63
|
+
context "when the id's format is invalid" do
|
64
|
+
given_param={model_name: "User", id: "coucou"}
|
65
|
+
it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
context "when one or many parameters are empty or nil" do
|
70
|
+
given_param={model_name: "User", id: nil}
|
71
|
+
it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
|
72
|
+
end
|
73
|
+
|
74
|
+
context "when the model name is invalid" do
|
75
|
+
given_param={model_name: "U3466", id: [1,2,3]}
|
76
|
+
it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
|
77
|
+
end
|
78
|
+
|
79
|
+
context "when parameter's keys are invalid" do
|
80
|
+
given_param={model_name: "User", i: [1,2] }
|
81
|
+
it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
|
82
|
+
given_param = {model: "Application",id: [5]}
|
83
|
+
it {expect((DbAccessor::Deleter.delete_by_id(given_param))[:code]).to eq 400 }
|
84
|
+
end
|
85
|
+
|
86
|
+
context "when the parameter is nil" do
|
87
|
+
it {expect((DbAccessor::Deleter.delete_by_id(nil))[:code]).to eq 400 }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
ActiveRecord::Migration.add_column :utilisateurs, :deleted, :boolean, default: false
|