nifty_services 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +26 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/nifty_services.rb +29 -0
- data/lib/nifty_services/base_action_service.rb +80 -0
- data/lib/nifty_services/base_create_service.rb +99 -0
- data/lib/nifty_services/base_crud_service.rb +107 -0
- data/lib/nifty_services/base_delete_service.rb +50 -0
- data/lib/nifty_services/base_service.rb +290 -0
- data/lib/nifty_services/base_update_service.rb +78 -0
- data/lib/nifty_services/configuration.rb +59 -0
- data/lib/nifty_services/errors.rb +13 -0
- data/lib/nifty_services/version.rb +3 -0
- data/nifty_services.gemspec +29 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fbc637a70ce76172e4046bea262d1e5dc441812e
|
4
|
+
data.tar.gz: b9eab049883acf9e7326016148867c0c2a8d0955
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e7418c345da76821b3d923f4713daad3bad32b3c99ba5e2e1f86984082d97c7e0a1e5b951d3c008a6cc27b6819adafa44198eaac48b8d6e4478b5889b531be7d
|
7
|
+
data.tar.gz: aada69447972ebce79dad95e7e2251907aae2af998973c96040b1c19e4607b5a108e60d49c72810b3e8e3242332d96da812dd5e38bcd54305d5cb558214d1703
|
data/.editorconfig
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# editorconfig.org
|
2
|
+
root = true
|
3
|
+
|
4
|
+
[*]
|
5
|
+
indent_style = tab
|
6
|
+
end_of_line = lf
|
7
|
+
charset = utf-8
|
8
|
+
trim_trailing_whitespace = true
|
9
|
+
insert_final_newline = true
|
10
|
+
indent_style = space
|
11
|
+
indent_size = 2
|
12
|
+
|
13
|
+
[*.md]
|
14
|
+
trim_trailing_whitespace = false
|
15
|
+
|
16
|
+
[*.rb]
|
17
|
+
indent_style = space
|
18
|
+
indent_size = 2
|
19
|
+
|
20
|
+
[*.yml]
|
21
|
+
indent_style = space
|
22
|
+
indent_size = 2
|
23
|
+
|
24
|
+
[Gemfile]
|
25
|
+
indent_style = space
|
26
|
+
indent_size = 2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Rafael Fidelis
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# NiftyServices
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/nifty_services`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'nifty_services'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install nifty_services
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nifty_services. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "nifty_services"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'nifty_services/version'
|
2
|
+
require 'active_support/core_ext/object/blank'
|
3
|
+
require 'active_support/core_ext/hash/keys'
|
4
|
+
require 'active_support/core_ext/string/inflections'
|
5
|
+
|
6
|
+
module NiftyServices
|
7
|
+
|
8
|
+
autoload :BaseService, 'nifty_services/base_service'
|
9
|
+
autoload :BaseActionService, 'nifty_services/base_action_service'
|
10
|
+
autoload :BaseCrudService, 'nifty_services/base_crud_service'
|
11
|
+
autoload :BaseCreateService, 'nifty_services/base_create_service'
|
12
|
+
autoload :BaseDeleteService, 'nifty_services/base_delete_service'
|
13
|
+
autoload :BaseUpdateService, 'nifty_services/base_update_service'
|
14
|
+
autoload :Configuration, 'nifty_services/configuration'
|
15
|
+
autoload :Error, 'nifty_services/errors'
|
16
|
+
autoload :Errors, 'nifty_services/errors'
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def configuration(&block)
|
20
|
+
@configuration ||= Configuration.new
|
21
|
+
|
22
|
+
yield(@configuration) if block_given?
|
23
|
+
|
24
|
+
@configuration
|
25
|
+
end
|
26
|
+
|
27
|
+
alias :config :configuration
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module NiftyServices
|
2
|
+
class BaseActionService < BaseService
|
3
|
+
|
4
|
+
def self.action_name(action_name, options={})
|
5
|
+
define_method :action_name do
|
6
|
+
action_name
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def execute
|
11
|
+
with_before_and_after_callbacks(:action) do
|
12
|
+
if can_execute_action?
|
13
|
+
|
14
|
+
execute_action
|
15
|
+
|
16
|
+
if success_runned_action?
|
17
|
+
success_response
|
18
|
+
else
|
19
|
+
errors = action_errors
|
20
|
+
bad_request_error(errors) if errors.present?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
success?
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def action_errors
|
30
|
+
[]
|
31
|
+
end
|
32
|
+
|
33
|
+
def can_execute_action?
|
34
|
+
unless valid_record?
|
35
|
+
return not_found_error!("#{record_error_key}.not_found")
|
36
|
+
end
|
37
|
+
|
38
|
+
unless valid_user?
|
39
|
+
return not_found_error!('users.not_found')
|
40
|
+
end
|
41
|
+
|
42
|
+
unless can_execute?
|
43
|
+
if @errors.blank?
|
44
|
+
return unprocessable_entity_error!("#{record_error_key}.user_cant_execute_#{action_name}")
|
45
|
+
else
|
46
|
+
return false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
return true
|
51
|
+
end
|
52
|
+
|
53
|
+
def can_execute?
|
54
|
+
return false unless valid_user?
|
55
|
+
return false unless valid_record?
|
56
|
+
|
57
|
+
user_can_execute_action?
|
58
|
+
end
|
59
|
+
|
60
|
+
def success_runned_action?
|
61
|
+
not_implemented_exception(__method__)
|
62
|
+
end
|
63
|
+
|
64
|
+
def user_can_execute_action?
|
65
|
+
not_implemented_exception(__method__)
|
66
|
+
end
|
67
|
+
|
68
|
+
def execute_action
|
69
|
+
not_implemented_exception(__method__)
|
70
|
+
end
|
71
|
+
|
72
|
+
def valid_record?
|
73
|
+
not_implemented_exception(__method__)
|
74
|
+
end
|
75
|
+
|
76
|
+
def record_error_key
|
77
|
+
not_implemented_exception(__method__)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
module NiftyServices
|
2
|
+
class BaseCreateService < BaseCrudService
|
3
|
+
|
4
|
+
def initialize(user, options = {})
|
5
|
+
@user = user
|
6
|
+
super(nil, user, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
def execute
|
10
|
+
with_before_and_after_callbacks(:create) do
|
11
|
+
if can_execute_action?
|
12
|
+
@record = build_record
|
13
|
+
|
14
|
+
if save_record
|
15
|
+
after_execute_success_response
|
16
|
+
else
|
17
|
+
errors = create_error_response(@record)
|
18
|
+
after_error_response(errors)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
success?
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def save_record
|
28
|
+
@record.save
|
29
|
+
end
|
30
|
+
|
31
|
+
def create_error_response(record)
|
32
|
+
record.errors
|
33
|
+
end
|
34
|
+
|
35
|
+
def after_error_response(errors)
|
36
|
+
unprocessable_entity_error(errors) if errors.present?
|
37
|
+
end
|
38
|
+
|
39
|
+
def after_execute_success_response
|
40
|
+
success_created_response
|
41
|
+
end
|
42
|
+
|
43
|
+
def after_success
|
44
|
+
end
|
45
|
+
|
46
|
+
def build_record
|
47
|
+
_record_params = record_allowed_params
|
48
|
+
|
49
|
+
if record_type.present? && _record_params.present?
|
50
|
+
return build_from_record_type(_record_params)
|
51
|
+
end
|
52
|
+
|
53
|
+
return not_implemented_exception(__method__)
|
54
|
+
end
|
55
|
+
|
56
|
+
def build_from_record_type(record_params)
|
57
|
+
record_type.send(:new, record_params)
|
58
|
+
end
|
59
|
+
|
60
|
+
def can_execute_action?
|
61
|
+
if validate_ip_on_create? && !can_create_with_ip?
|
62
|
+
return forbidden_error!(%s(users.ip_temporarily_blocked))
|
63
|
+
end
|
64
|
+
|
65
|
+
unless valid_user?
|
66
|
+
return not_found_error!(%s(users.not_found))
|
67
|
+
end
|
68
|
+
|
69
|
+
unless can_create?
|
70
|
+
if @errors.blank?
|
71
|
+
return forbidden_error!("#{record_error_key}.user_cant_create")
|
72
|
+
else
|
73
|
+
return false
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
return valid?
|
78
|
+
end
|
79
|
+
|
80
|
+
def can_create?
|
81
|
+
return false unless valid_user?
|
82
|
+
|
83
|
+
return user_can_create_record?
|
84
|
+
end
|
85
|
+
|
86
|
+
def can_create_with_ip?
|
87
|
+
true
|
88
|
+
end
|
89
|
+
|
90
|
+
def validate_ip_on_create?
|
91
|
+
# TODO: Use NiftyService.config.validate_ip_on_create?
|
92
|
+
false
|
93
|
+
end
|
94
|
+
|
95
|
+
def user_can_create_record?
|
96
|
+
not_implemented_exception(__method__)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
module NiftyServices
|
2
|
+
module Concerns
|
3
|
+
end
|
4
|
+
|
5
|
+
class BaseCrudService < BaseService
|
6
|
+
|
7
|
+
attr_reader :record
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def record_type(record_type, options = {})
|
11
|
+
define_method :record_type do
|
12
|
+
record_type
|
13
|
+
end
|
14
|
+
|
15
|
+
record_alias = options.delete(:alias_name) || record_type.to_s.underscore
|
16
|
+
|
17
|
+
alias_method record_alias.to_sym, :record
|
18
|
+
end
|
19
|
+
|
20
|
+
def include_concern(namespace, concern_type)
|
21
|
+
_module = "#{services_concern_namespace}::#{namespace.to_s.camelize}::#{concern_type.to_s.camelize}"
|
22
|
+
|
23
|
+
self.include(_module.constantize)
|
24
|
+
end
|
25
|
+
|
26
|
+
def services_concern_namespace
|
27
|
+
NiftyServices.config.service_concerns_namespace
|
28
|
+
end
|
29
|
+
|
30
|
+
alias_method :concern, :include_concern
|
31
|
+
end
|
32
|
+
|
33
|
+
def initialize(record, user, options = {})
|
34
|
+
@record = record
|
35
|
+
@user = user
|
36
|
+
|
37
|
+
super(options)
|
38
|
+
end
|
39
|
+
|
40
|
+
def execute
|
41
|
+
not_implemented_exception(__method__)
|
42
|
+
end
|
43
|
+
|
44
|
+
def record_type
|
45
|
+
not_implemented_exception(__method__)
|
46
|
+
end
|
47
|
+
|
48
|
+
def record_params
|
49
|
+
not_implemented_exception(__method__)
|
50
|
+
end
|
51
|
+
|
52
|
+
def record_params_whitelist
|
53
|
+
not_implemented_exception(__method__)
|
54
|
+
end
|
55
|
+
|
56
|
+
def record_allowed_params
|
57
|
+
filter_hash(record_params, record_params_whitelist)
|
58
|
+
end
|
59
|
+
|
60
|
+
alias :record_whitelisted_params :record_allowed_params
|
61
|
+
alias :record_safe_params :record_allowed_params
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def valid_user?
|
66
|
+
user_class = NiftyServices.config.user_class
|
67
|
+
|
68
|
+
raise 'Invalid User class. Use NitfyService.config.user_class = ClassName' if user_class.blank?
|
69
|
+
|
70
|
+
valid_object?(@user, user_class)
|
71
|
+
end
|
72
|
+
|
73
|
+
def array_values_from_hash(options, key, root = nil)
|
74
|
+
options = options.symbolize_keys
|
75
|
+
|
76
|
+
if root.present?
|
77
|
+
options = (options[root.to_sym] || {}).symbolize_keys
|
78
|
+
end
|
79
|
+
|
80
|
+
return [] unless options.key?(key.to_sym)
|
81
|
+
|
82
|
+
values = options[key.to_sym]
|
83
|
+
|
84
|
+
return values if values.is_a?(Array)
|
85
|
+
|
86
|
+
array_values_from_string(values)
|
87
|
+
end
|
88
|
+
|
89
|
+
alias :array_values_from_params :array_values_from_hash
|
90
|
+
|
91
|
+
def array_values_from_string(string)
|
92
|
+
string.to_s.split(/\,/).map(&:squish)
|
93
|
+
end
|
94
|
+
|
95
|
+
def record_params
|
96
|
+
not_implemented_exception(__method__)
|
97
|
+
end
|
98
|
+
|
99
|
+
def record_error_key
|
100
|
+
record_type.to_s.pluralize.underscore
|
101
|
+
end
|
102
|
+
|
103
|
+
def valid_record?
|
104
|
+
valid_object?(@record, record_type)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module NiftyServices
|
2
|
+
class BaseDeleteService < BaseCrudService
|
3
|
+
|
4
|
+
def execute
|
5
|
+
with_before_and_after_callbacks(:delete) do
|
6
|
+
if can_execute_action?
|
7
|
+
if destroy_record
|
8
|
+
success_response
|
9
|
+
else
|
10
|
+
bad_request_error(@record.errors)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
success?
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def destroy_record
|
20
|
+
@record.destroy
|
21
|
+
end
|
22
|
+
|
23
|
+
def can_execute_action?
|
24
|
+
unless valid_record?
|
25
|
+
return not_found_error!("#{record_error_key}.not_found")
|
26
|
+
end
|
27
|
+
|
28
|
+
unless valid_user?
|
29
|
+
return not_found_error!('users.not_found')
|
30
|
+
end
|
31
|
+
|
32
|
+
unless can_delete?
|
33
|
+
return forbidden_error!("#{record_error_key}.user_cant_delete")
|
34
|
+
end
|
35
|
+
|
36
|
+
return true
|
37
|
+
end
|
38
|
+
|
39
|
+
def can_delete?
|
40
|
+
return false unless valid_user?
|
41
|
+
return false unless valid_record?
|
42
|
+
|
43
|
+
return user_can_delete_record?
|
44
|
+
end
|
45
|
+
|
46
|
+
def user_can_delete_record?
|
47
|
+
@record.user_can_delete?(@user)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,290 @@
|
|
1
|
+
module NiftyServices
|
2
|
+
class BaseService
|
3
|
+
|
4
|
+
attr_reader :options, :response_status, :errors
|
5
|
+
|
6
|
+
CALLBACKS = [
|
7
|
+
:after_initialize,
|
8
|
+
:before_error,
|
9
|
+
:after_error,
|
10
|
+
:before_success,
|
11
|
+
:after_success,
|
12
|
+
:before_create,
|
13
|
+
:after_create,
|
14
|
+
:before_update,
|
15
|
+
:after_update,
|
16
|
+
:before_delete,
|
17
|
+
:after_delete,
|
18
|
+
:before_action,
|
19
|
+
:after_action
|
20
|
+
].freeze
|
21
|
+
|
22
|
+
@@registered_callbacks = Hash.new {|k,v| k[v] = Hash.new }
|
23
|
+
|
24
|
+
class << self
|
25
|
+
def register_callback(callback_name, method_name, &block)
|
26
|
+
method_name = "#{method_name.to_s.gsub(/\Z_callback/, '')}_callback"
|
27
|
+
|
28
|
+
@@registered_callbacks[self.name.to_sym][callback_name] ||= []
|
29
|
+
@@registered_callbacks[self.name.to_sym][callback_name] << method_name
|
30
|
+
|
31
|
+
register_callback_action(method_name, &block)
|
32
|
+
end
|
33
|
+
|
34
|
+
def register_callback_action(callback_name, &block)
|
35
|
+
define_method(callback_name, &block)
|
36
|
+
end
|
37
|
+
|
38
|
+
def register_error_response_method(reason_string, status_code)
|
39
|
+
NiftyServices::Configuration.add_response_error_method(reason_string, status_code)
|
40
|
+
define_error_response_method(reason_string, status_code)
|
41
|
+
end
|
42
|
+
|
43
|
+
def define_error_response_method(reason_string, status_code)
|
44
|
+
method_name = "#{reason_string.to_s.gsub(/\Z_error/, '')}_error"
|
45
|
+
|
46
|
+
define_method method_name do |message_key, options = {}|
|
47
|
+
error(status_code, message_key, options)
|
48
|
+
end
|
49
|
+
|
50
|
+
define_method "#{method_name}!" do |message_key, options = {}|
|
51
|
+
error!(status_code, message_key, options)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
CALLBACKS.each do |callback_name|
|
57
|
+
# empty method call (just returns nil)
|
58
|
+
define_method callback_name, -> {}
|
59
|
+
end
|
60
|
+
|
61
|
+
def initialize(options = {}, initial_response_status = 400)
|
62
|
+
@options = options.to_options!
|
63
|
+
@response_status = initial_response_status
|
64
|
+
@errors = []
|
65
|
+
|
66
|
+
initial_callbacks_setup
|
67
|
+
|
68
|
+
call_callback(:after_initialize)
|
69
|
+
end
|
70
|
+
|
71
|
+
def valid?
|
72
|
+
return @errors.blank?
|
73
|
+
end
|
74
|
+
|
75
|
+
def success?
|
76
|
+
@success == true && valid?
|
77
|
+
end
|
78
|
+
|
79
|
+
def fail?
|
80
|
+
!success?
|
81
|
+
end
|
82
|
+
|
83
|
+
def response_status
|
84
|
+
@response_status ||= 400
|
85
|
+
end
|
86
|
+
|
87
|
+
def changed?
|
88
|
+
changed_attributes.any?
|
89
|
+
end
|
90
|
+
|
91
|
+
def callback_fired?(callback_name)
|
92
|
+
return (
|
93
|
+
callback_fired_in?(@fired_callbacks, callback_name) ||
|
94
|
+
callback_fired_in?(@custom_fired_callbacks, callback_name) ||
|
95
|
+
callback_fired_in?(@custom_fired_callbacks, "#{callback_name}_callback")
|
96
|
+
)
|
97
|
+
end
|
98
|
+
|
99
|
+
alias :callback_called? :callback_fired?
|
100
|
+
|
101
|
+
def option_exists?(key)
|
102
|
+
@options && @options.key?(key.to_sym)
|
103
|
+
end
|
104
|
+
|
105
|
+
def option_enabled?(key)
|
106
|
+
option_exists?(key) && @options[key.to_sym] == true
|
107
|
+
end
|
108
|
+
|
109
|
+
def option_disabled?(key)
|
110
|
+
!option_enabled?(key)
|
111
|
+
end
|
112
|
+
|
113
|
+
def register_callback(callback_name, method_name, &block)
|
114
|
+
method_name = :"#{method_name.to_s.gsub(/\Z_callback/, '')}_callback"
|
115
|
+
|
116
|
+
@registered_callbacks[callback_name.to_sym] << method_name
|
117
|
+
end
|
118
|
+
|
119
|
+
def register_callback_action(callback_name, &block)
|
120
|
+
cb_name = :"#{callback_name.to_s.gsub(/\Z_callback/, '')}_callback"
|
121
|
+
@callbacks_actions[cb_name] = block
|
122
|
+
end
|
123
|
+
|
124
|
+
def add_error(error)
|
125
|
+
add_method = error.is_a?(Array) ? :concat : :push
|
126
|
+
@errors.send(add_method, error)
|
127
|
+
end
|
128
|
+
|
129
|
+
private
|
130
|
+
def initial_callbacks_setup
|
131
|
+
@fired_callbacks, @custom_fired_callbacks = {}, {}
|
132
|
+
@callbacks_actions = {}
|
133
|
+
@registered_callbacks = Hash.new {|k,v| k[v] = [] }
|
134
|
+
end
|
135
|
+
|
136
|
+
def success_response(status = 200)
|
137
|
+
call_callback(:before_success)
|
138
|
+
|
139
|
+
@response_status = status
|
140
|
+
@success = true
|
141
|
+
|
142
|
+
call_callback(:after_success)
|
143
|
+
end
|
144
|
+
|
145
|
+
def success_created_response
|
146
|
+
success_response(201)
|
147
|
+
end
|
148
|
+
|
149
|
+
def error(status, message_key, options={})
|
150
|
+
with_before_and_after_callbacks(:error) do
|
151
|
+
@response_status = status
|
152
|
+
error_message = process_error_message_for_key(message_key, options)
|
153
|
+
|
154
|
+
add_error(error_message)
|
155
|
+
|
156
|
+
error_message
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def error!(status, message_key, options = {})
|
161
|
+
error(status, message_key, options)
|
162
|
+
|
163
|
+
# TODO:
|
164
|
+
# maybe throw a Exception making bang(!) semantic
|
165
|
+
# raise "NiftyServices::V1::Exceptions::#{status.titleize}".constantize
|
166
|
+
return false
|
167
|
+
end
|
168
|
+
|
169
|
+
def call_callback(callback_name)
|
170
|
+
callback_name = callback_name.to_s.underscore.to_sym
|
171
|
+
|
172
|
+
if self.respond_to?(callback_name, true) # include private methods
|
173
|
+
@fired_callbacks[callback_name.to_sym] = true
|
174
|
+
|
175
|
+
invoke_callback(method(callback_name))
|
176
|
+
call_registered_callbacks_for(callback_name)
|
177
|
+
end
|
178
|
+
|
179
|
+
# allow chained methods
|
180
|
+
self
|
181
|
+
end
|
182
|
+
|
183
|
+
def valid_object?(record, expected_class)
|
184
|
+
record.present? && record.is_a?(expected_class)
|
185
|
+
end
|
186
|
+
|
187
|
+
def filter_hash(hash, whitelist_keys = [])
|
188
|
+
(hash || {}).symbolize_keys.slice(*whitelist_keys.map(&:to_sym))
|
189
|
+
end
|
190
|
+
|
191
|
+
def changes(old, current, attributes = {})
|
192
|
+
changes = []
|
193
|
+
|
194
|
+
return changes if old.blank? || current.blank?
|
195
|
+
|
196
|
+
old_attributes = old.attributes.slice(*attributes.map(&:to_s))
|
197
|
+
new_attributes = current.attributes.slice(*attributes.map(&:to_s))
|
198
|
+
|
199
|
+
new_attributes.each do |attribute, value|
|
200
|
+
changes << attribute if (old_attributes[attribute] != value)
|
201
|
+
end
|
202
|
+
|
203
|
+
changes
|
204
|
+
end
|
205
|
+
|
206
|
+
def i18n_namespace
|
207
|
+
NiftyServices.configuration.i18n_namespace
|
208
|
+
end
|
209
|
+
|
210
|
+
def i18n_errors_namespace
|
211
|
+
"#{i18n_namespace}.errors"
|
212
|
+
end
|
213
|
+
|
214
|
+
def process_error_message_for_key(message_key, options)
|
215
|
+
if message_key.class.to_s == 'ActiveModel::Errors'
|
216
|
+
message = message_key.messages
|
217
|
+
elsif message_key.is_a?(Array) && message_key.first.is_a?(Hash)
|
218
|
+
message = message_key
|
219
|
+
else
|
220
|
+
message = I18n.t("#{i18n_errors_namespace}.#{message_key}", options)
|
221
|
+
end
|
222
|
+
|
223
|
+
message
|
224
|
+
end
|
225
|
+
|
226
|
+
def with_before_and_after_callbacks(callback_basename, &block)
|
227
|
+
call_callback(:"before_#{callback_basename}")
|
228
|
+
|
229
|
+
response = yield(block) if block_given?
|
230
|
+
|
231
|
+
call_callback(:"after_#{callback_basename}")
|
232
|
+
|
233
|
+
response
|
234
|
+
end
|
235
|
+
|
236
|
+
def call_registered_callbacks_for(callback_name)
|
237
|
+
instance_call_all_custom_registered_callbacks_for(callback_name)
|
238
|
+
class_call_all_custom_registered_callbacks_for(callback_name)
|
239
|
+
end
|
240
|
+
|
241
|
+
def instance_call_all_custom_registered_callbacks_for(callback_name)
|
242
|
+
@fired_callbacks[callback_name] = true
|
243
|
+
|
244
|
+
callbacks = @registered_callbacks[callback_name.to_sym]
|
245
|
+
|
246
|
+
callbacks.each do |cb|
|
247
|
+
if callback = @callbacks_actions[cb.to_sym]
|
248
|
+
@custom_fired_callbacks[cb.to_sym] = true
|
249
|
+
invoke_callback(callback)
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
def class_call_all_custom_registered_callbacks_for(callback_name)
|
255
|
+
classes_chain = self.class.ancestors.map(&:to_s).grep /\ANiftyServices/
|
256
|
+
klasses = @@registered_callbacks.keys.map(&:to_s) & classes_chain
|
257
|
+
|
258
|
+
klasses.each do |klass|
|
259
|
+
class_call_all_custom_registered_callbacks_for_class(klass, callback_name)
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
def class_call_all_custom_registered_callbacks_for_class(class_name, callback_name)
|
264
|
+
class_callbacks = @@registered_callbacks[class_name.to_sym]
|
265
|
+
callbacks = class_callbacks[callback_name.to_sym] || []
|
266
|
+
|
267
|
+
callbacks.each do |cb|
|
268
|
+
@custom_fired_callbacks[cb.to_sym] = true
|
269
|
+
invoke_callback(method(cb))
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
def callback_fired_in?(callback_list, callback_name)
|
274
|
+
return callback_list[callback_name.to_sym].present?
|
275
|
+
end
|
276
|
+
|
277
|
+
def invoke_callback(method)
|
278
|
+
method.call
|
279
|
+
end
|
280
|
+
|
281
|
+
NiftyServices::Configuration.response_errors_list.each do |reason_string, status_code|
|
282
|
+
define_error_response_method(reason_string, status_code)
|
283
|
+
end
|
284
|
+
|
285
|
+
protected
|
286
|
+
def not_implemented_exception(method_name)
|
287
|
+
raise NotImplementedError, "#{method_name} must be implemented in subclass"
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module NiftyServices
|
2
|
+
class BaseUpdateService < BaseCrudService
|
3
|
+
|
4
|
+
def execute
|
5
|
+
with_before_and_after_callbacks(:update) do
|
6
|
+
if can_execute_action?
|
7
|
+
@updated_record = update_record
|
8
|
+
@updated_record ||= @record
|
9
|
+
|
10
|
+
if success_updated?
|
11
|
+
success_response
|
12
|
+
else
|
13
|
+
errors = update_errors
|
14
|
+
bad_request_error(errors) if errors.present?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
success?
|
20
|
+
end
|
21
|
+
|
22
|
+
def changed_attributes
|
23
|
+
return [] if fail?
|
24
|
+
@changed_attributes ||= changes(@old_record, @record, changed_attributes_array)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def changed_attributes_array
|
30
|
+
record_params.keys
|
31
|
+
end
|
32
|
+
|
33
|
+
def success_updated?
|
34
|
+
@updated_record.valid?
|
35
|
+
end
|
36
|
+
|
37
|
+
def update_errors
|
38
|
+
@updated_record.errors
|
39
|
+
end
|
40
|
+
|
41
|
+
def update_record
|
42
|
+
@record.class.send(:update, @record.id, record_params)
|
43
|
+
end
|
44
|
+
|
45
|
+
def can_execute_action?
|
46
|
+
|
47
|
+
unless valid_record?
|
48
|
+
return not_found_error!("#{record_error_key}.not_found")
|
49
|
+
end
|
50
|
+
|
51
|
+
unless valid_user?
|
52
|
+
return not_found_error!('users.not_found')
|
53
|
+
end
|
54
|
+
|
55
|
+
unless can_update?
|
56
|
+
return forbidden_error!("#{record_error_key}.user_cant_update")
|
57
|
+
end
|
58
|
+
|
59
|
+
return true
|
60
|
+
end
|
61
|
+
|
62
|
+
def can_update?
|
63
|
+
return false unless valid_user?
|
64
|
+
return false unless valid_record?
|
65
|
+
|
66
|
+
return user_can_update_record?
|
67
|
+
end
|
68
|
+
|
69
|
+
def user_can_update_record?
|
70
|
+
@record.user_can_update?(@user)
|
71
|
+
end
|
72
|
+
|
73
|
+
def after_success
|
74
|
+
@old_record = @record.dup
|
75
|
+
@record = @updated_record
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module NiftyServices
|
4
|
+
class Configuration
|
5
|
+
|
6
|
+
DEFAULT_I18N_NAMESPACE = "nifty_services"
|
7
|
+
DEFAULT_SERVICE_CONCERN_NAMESPACE = 'NitfyServices::Concerns'
|
8
|
+
|
9
|
+
RESPONSE_ERRORS_LIST = {
|
10
|
+
:bad_request => 400,
|
11
|
+
:not_authorized => 401,
|
12
|
+
:forbidden => 403,
|
13
|
+
:not_found => 404,
|
14
|
+
:unprocessable_entity => 422,
|
15
|
+
:internal_server => 500,
|
16
|
+
:not_implemented => 501
|
17
|
+
}
|
18
|
+
|
19
|
+
class << self
|
20
|
+
def response_errors_list
|
21
|
+
RESPONSE_ERRORS_LIST
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_response_error_method(reason, status_code)
|
25
|
+
response_errors_list[reason.to_sym] = status_code.to_i
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
attr_reader :logger, :options
|
30
|
+
|
31
|
+
attr_accessor :user_class, :service_concerns_namespace
|
32
|
+
|
33
|
+
def initialize(options = {})
|
34
|
+
@options = options
|
35
|
+
@service_concerns_namespace = default_service_concerns_namespace
|
36
|
+
@user_class = options[:user_class] || default_user_class
|
37
|
+
@logger = options[:logger] || default_logger
|
38
|
+
end
|
39
|
+
|
40
|
+
def i18n_namespace
|
41
|
+
@options[:i18n_namespace] || DEFAULT_I18N_NAMESPACE
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def default_service_concerns_namespace
|
46
|
+
DEFAULT_SERVICE_CONCERN_NAMESPACE
|
47
|
+
end
|
48
|
+
|
49
|
+
def default_user_class
|
50
|
+
nil
|
51
|
+
end
|
52
|
+
|
53
|
+
def default_logger
|
54
|
+
logger = Logger.new("/dev/null")
|
55
|
+
logger.level = Logger::INFO
|
56
|
+
logger
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'nifty_services/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "nifty_services"
|
9
|
+
spec.version = NiftyServices::VERSION
|
10
|
+
spec.authors = ["Rafael Fidelis"]
|
11
|
+
spec.email = ["rafa_fidelis@yahoo.com.br"]
|
12
|
+
|
13
|
+
spec.summary = %q{Nifty and awesome service oriented architecture library for Ruby applications.}
|
14
|
+
spec.description = %q{The dead simple services object oriented layer for Ruby applications to give robustness and cohesion back to your code.}
|
15
|
+
spec.homepage = "https://github.com/fidelisrafael/nifty_services"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency 'activesupport', '~> 4.2.2'
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec"
|
28
|
+
spec.add_development_dependency "pry"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nifty_services
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rafael Fidelis
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.2.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: The dead simple services object oriented layer for Ruby applications
|
84
|
+
to give robustness and cohesion back to your code.
|
85
|
+
email:
|
86
|
+
- rafa_fidelis@yahoo.com.br
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".editorconfig"
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- ".travis.yml"
|
95
|
+
- CODE_OF_CONDUCT.md
|
96
|
+
- Gemfile
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- lib/nifty_services.rb
|
103
|
+
- lib/nifty_services/base_action_service.rb
|
104
|
+
- lib/nifty_services/base_create_service.rb
|
105
|
+
- lib/nifty_services/base_crud_service.rb
|
106
|
+
- lib/nifty_services/base_delete_service.rb
|
107
|
+
- lib/nifty_services/base_service.rb
|
108
|
+
- lib/nifty_services/base_update_service.rb
|
109
|
+
- lib/nifty_services/configuration.rb
|
110
|
+
- lib/nifty_services/errors.rb
|
111
|
+
- lib/nifty_services/version.rb
|
112
|
+
- nifty_services.gemspec
|
113
|
+
homepage: https://github.com/fidelisrafael/nifty_services
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata: {}
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.5.1
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: Nifty and awesome service oriented architecture library for Ruby applications.
|
137
|
+
test_files: []
|