light_services 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +131 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/hello_world.rb +43 -0
- data/lib/light_services/base.rb +90 -0
- data/lib/light_services/service.rb +54 -0
- data/lib/light_services/version.rb +3 -0
- data/lib/light_services.rb +6 -0
- data/light_services.gemspec +30 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 30c9c842a952e3a04bef9a44dcccdaa9ff545ebe
|
4
|
+
data.tar.gz: e7da3333fc8bf4a3f81b63b3436e0b7b8a632f9a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7f4e060b32891610835ff51fcc755403308982e2e8f9370c88b73cdc350c4b74ad0a0640b598489927cd007380c18849adba1bf60ae7a7199d92b5bc3422c592
|
7
|
+
data.tar.gz: 7913d2912006b81c76b345543b9d0ff549a71492039905a03c38132a4069ad7e7203cdc3c9b20b1a2a6dda3597dd64ce40cd51fc9583a57216a71a35031ede25
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Nicolas Lima
|
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,131 @@
|
|
1
|
+
# LightServices
|
2
|
+
|
3
|
+
LightServices is a simple base to help build a service layer
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'light_services'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install light_services
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
|
25
|
+
# app/models/user.rb
|
26
|
+
class User
|
27
|
+
|
28
|
+
attr_reader :errors
|
29
|
+
attr_accessor :first_name, :last_name, :errors
|
30
|
+
|
31
|
+
def initialize(first_name, last_name)
|
32
|
+
@first_name = first_name
|
33
|
+
@last_name = last_name
|
34
|
+
@errors = []
|
35
|
+
end
|
36
|
+
|
37
|
+
def save
|
38
|
+
puts "saving in some db..."
|
39
|
+
end
|
40
|
+
|
41
|
+
def add_errors(message)
|
42
|
+
@errors.push message
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# app/services/user_creator.rb
|
47
|
+
class UserCreator < LightServices::Service
|
48
|
+
|
49
|
+
attributes :first_name, :last_name
|
50
|
+
validates :first_name, :last_name, presence: true
|
51
|
+
execute :create_user, if: :input_valid?, fallback: :add_error
|
52
|
+
|
53
|
+
returns User do |user|
|
54
|
+
@user = user.new @first_name, @last_name
|
55
|
+
end
|
56
|
+
|
57
|
+
def create_user
|
58
|
+
@user.save
|
59
|
+
# ... other complexity logic
|
60
|
+
end
|
61
|
+
|
62
|
+
def add_error
|
63
|
+
@user.add_errors( "Opss, something it's wrong" )
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
user = UserCreator.new(first_name: 'Aragorn II', last_name: 'son of Arathorn').call
|
69
|
+
|
70
|
+
```
|
71
|
+
|
72
|
+
### API doc
|
73
|
+
|
74
|
+
* **attributes**
|
75
|
+
|
76
|
+
`attributes` defines service inputs and will declares a instance variabel with the same name.
|
77
|
+
You need pass the keywords from `initialize` like:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
|
81
|
+
### hello_world_service.rb
|
82
|
+
attributes :name # declares @name
|
83
|
+
|
84
|
+
### hello_world_controller.rb
|
85
|
+
HelloWorldService.new(name: 'Aragorn')
|
86
|
+
```
|
87
|
+
|
88
|
+
* **validates**
|
89
|
+
|
90
|
+
`validates` is from ActiveModel::Validations
|
91
|
+
|
92
|
+
* **execute**
|
93
|
+
|
94
|
+
`execute` delegates which method will be called when the `#call` is invoked.
|
95
|
+
|
96
|
+
*options*
|
97
|
+
|
98
|
+
`if:` - validation method, you can override it or pass other method (need returns boolean value)
|
99
|
+
|
100
|
+
`fallback:` - is invoked when the validations method isn't valid
|
101
|
+
|
102
|
+
* **returns**
|
103
|
+
|
104
|
+
declares what will be returned.
|
105
|
+
You can access `returns` through the instance variable with the same name.
|
106
|
+
|
107
|
+
`returns` accept block to initialize the returns e.g.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
#hello_world_service.rb
|
111
|
+
|
112
|
+
returns User do |user|
|
113
|
+
@user = user.new
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
## Development
|
118
|
+
|
119
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
120
|
+
|
121
|
+
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).
|
122
|
+
|
123
|
+
## Contributing
|
124
|
+
|
125
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nicolaslima/light_services.
|
126
|
+
|
127
|
+
|
128
|
+
## License
|
129
|
+
|
130
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
131
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "light_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,43 @@
|
|
1
|
+
class User
|
2
|
+
|
3
|
+
attr_reader :errors
|
4
|
+
attr_accessor :first_name, :last_name, :errors
|
5
|
+
|
6
|
+
def initialize(first_name, last_name)
|
7
|
+
@first_name = first_name
|
8
|
+
@last_name = last_name
|
9
|
+
@errors = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def save
|
13
|
+
puts "saving in some db..."
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_errors(message)
|
17
|
+
@errors.push message
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
class UserCreator < LightServices::Service
|
23
|
+
|
24
|
+
attributes :first_name, :last_name
|
25
|
+
validates :first_name, :last_name, presence: true
|
26
|
+
execute :create_user, if: :input_valid?, fallback: :add_error
|
27
|
+
|
28
|
+
returns User do |user|
|
29
|
+
@user = user.new @first_name, @last_name
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_user
|
33
|
+
@user.save
|
34
|
+
# ... other complexity logic
|
35
|
+
end
|
36
|
+
|
37
|
+
def add_error
|
38
|
+
@user.add_errors( "Opss, something it's wrong" )
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
user = UserCreator.new(first_name: 'Aragorn II', last_name: 'son of Arathorn').call
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module LightServices
|
2
|
+
module Base
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
def attributes(*args)
|
6
|
+
define_class_attributes(args)
|
7
|
+
@attributes = args
|
8
|
+
end
|
9
|
+
|
10
|
+
def get_attributes
|
11
|
+
@attributes || {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def returns(resource, &block)
|
15
|
+
@returns = resource
|
16
|
+
@returns_block = block if block_given?
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_returns
|
20
|
+
@returns || ''
|
21
|
+
end
|
22
|
+
|
23
|
+
def get_returns_block
|
24
|
+
@returns_block
|
25
|
+
end
|
26
|
+
|
27
|
+
def execute(method_name, options = {})
|
28
|
+
@method_name = method_name
|
29
|
+
@execute_options = { if: options[:if], fallback: options[:fallback] }
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_execute_method_name
|
33
|
+
@method_name
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_execute_method_options
|
37
|
+
@execute_options
|
38
|
+
end
|
39
|
+
|
40
|
+
def define_class_attributes(attributes)
|
41
|
+
attributes.map do |attr_name|
|
42
|
+
send(:attr_reader, attr_name)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
module InstanceMethods
|
48
|
+
def attributes
|
49
|
+
self.class.get_attributes
|
50
|
+
end
|
51
|
+
|
52
|
+
def returns
|
53
|
+
self.class.get_returns
|
54
|
+
end
|
55
|
+
|
56
|
+
def returns_name
|
57
|
+
self.class.get_returns.to_s.downcase
|
58
|
+
end
|
59
|
+
|
60
|
+
def returns_block
|
61
|
+
self.class.get_returns_block
|
62
|
+
end
|
63
|
+
|
64
|
+
def execute_method_name
|
65
|
+
self.class.get_execute_method_name
|
66
|
+
end
|
67
|
+
|
68
|
+
def execute_method_options
|
69
|
+
self.class.get_execute_method_options
|
70
|
+
end
|
71
|
+
|
72
|
+
def initialize_class_attributes(args)
|
73
|
+
args[0].map do |attr_name, value|
|
74
|
+
if attributes.include? attr_name
|
75
|
+
instance_variable_set("@#{ attr_name }", value)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def initialize_returns_attribute
|
81
|
+
instance_variable_set("@#{ returns_name }", returns)
|
82
|
+
end
|
83
|
+
|
84
|
+
def setup_returns
|
85
|
+
self.instance_exec( returns, &returns_block )
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
module LightServices
|
4
|
+
class Service
|
5
|
+
include ActiveModel::Validations
|
6
|
+
include LightServices::Base::InstanceMethods
|
7
|
+
extend LightServices::Base::ClassMethods
|
8
|
+
|
9
|
+
alias :input_valid? :valid?
|
10
|
+
|
11
|
+
def initialize(*args)
|
12
|
+
validate_arguments(args)
|
13
|
+
initialize_class_attributes(args)
|
14
|
+
initialize_returns_attribute if has_returns?
|
15
|
+
setup_returns if returns_block
|
16
|
+
end
|
17
|
+
|
18
|
+
def call
|
19
|
+
if has_conditional?
|
20
|
+
executed_method_return = validate_conditional
|
21
|
+
else
|
22
|
+
executed_method_return = send( execute_method_name )
|
23
|
+
end
|
24
|
+
|
25
|
+
has_returns? ? instance_variable_get("@#{ returns_name }") : executed_method_return
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def has_returns?
|
31
|
+
!returns.nil?
|
32
|
+
end
|
33
|
+
|
34
|
+
def has_conditional?
|
35
|
+
!execute_method_options[:if].nil? && !execute_method_options[:if].empty?
|
36
|
+
end
|
37
|
+
|
38
|
+
def validate_conditional
|
39
|
+
conditional_pass = send( execute_method_options[:if] )
|
40
|
+
|
41
|
+
if conditional_pass
|
42
|
+
send( execute_method_name )
|
43
|
+
else
|
44
|
+
send( execute_method_options[:fallback] ) if execute_method_options[:fallback]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def validate_arguments(args)
|
49
|
+
raise InvalidArgumentError, "Arguments required, but was given a #{args.class}" if args.empty?
|
50
|
+
raise InvalidArgumentError, "keyword arguments are required" if args[0].class != Hash
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'light_services/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "light_services"
|
8
|
+
spec.version = LightServices::VERSION
|
9
|
+
spec.authors = ["Nicolas Lima"]
|
10
|
+
spec.email = ["lima.nicolasmateus@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple base to help build Services}
|
13
|
+
spec.description = %q{A simple base to help build Services}
|
14
|
+
spec.homepage = "https://github.com/nicolaslima/light_services"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
|
29
|
+
spec.add_runtime_dependency "activemodel", "4.2.6"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: light_services
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicolas Lima
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activemodel
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.2.6
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.2.6
|
69
|
+
description: A simple base to help build Services
|
70
|
+
email:
|
71
|
+
- lima.nicolasmateus@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- examples/hello_world.rb
|
86
|
+
- lib/light_services.rb
|
87
|
+
- lib/light_services/base.rb
|
88
|
+
- lib/light_services/service.rb
|
89
|
+
- lib/light_services/version.rb
|
90
|
+
- light_services.gemspec
|
91
|
+
homepage: https://github.com/nicolaslima/light_services
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.6.8
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: A simple base to help build Services
|
115
|
+
test_files: []
|