light-services 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,156 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ light-services (0.1.0)
5
+ rails (> 4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.0.0.rc1)
11
+ actionpack (= 5.0.0.rc1)
12
+ nio4r (~> 1.2)
13
+ websocket-driver (~> 0.6.1)
14
+ actionmailer (5.0.0.rc1)
15
+ actionpack (= 5.0.0.rc1)
16
+ actionview (= 5.0.0.rc1)
17
+ activejob (= 5.0.0.rc1)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 1.0, >= 1.0.5)
20
+ actionpack (5.0.0.rc1)
21
+ actionview (= 5.0.0.rc1)
22
+ activesupport (= 5.0.0.rc1)
23
+ rack (~> 2.x)
24
+ rack-test (~> 0.6.3)
25
+ rails-dom-testing (~> 1.0, >= 1.0.5)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.0.0.rc1)
28
+ activesupport (= 5.0.0.rc1)
29
+ builder (~> 3.1)
30
+ erubis (~> 2.7.0)
31
+ rails-dom-testing (~> 1.0, >= 1.0.5)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
33
+ activejob (5.0.0.rc1)
34
+ activesupport (= 5.0.0.rc1)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.0.0.rc1)
37
+ activesupport (= 5.0.0.rc1)
38
+ activerecord (5.0.0.rc1)
39
+ activemodel (= 5.0.0.rc1)
40
+ activesupport (= 5.0.0.rc1)
41
+ arel (~> 7.0)
42
+ activesupport (5.0.0.rc1)
43
+ concurrent-ruby (~> 1.0, >= 1.0.2)
44
+ i18n (~> 0.7)
45
+ minitest (~> 5.1)
46
+ tzinfo (~> 1.1)
47
+ appraisal (2.1.0)
48
+ bundler
49
+ rake
50
+ thor (>= 0.14.0)
51
+ arel (7.0.0)
52
+ builder (3.2.2)
53
+ codeclimate-test-reporter (0.5.1)
54
+ simplecov (>= 0.7.1, < 1.0.0)
55
+ concurrent-ruby (1.0.2)
56
+ diff-lcs (1.2.5)
57
+ docile (1.1.5)
58
+ erubis (2.7.0)
59
+ globalid (0.3.6)
60
+ activesupport (>= 4.1.0)
61
+ i18n (0.7.0)
62
+ json (1.8.6)
63
+ loofah (2.0.3)
64
+ nokogiri (>= 1.5.9)
65
+ mail (2.6.4)
66
+ mime-types (>= 1.16, < 4)
67
+ method_source (0.8.2)
68
+ mime-types (3.1)
69
+ mime-types-data (~> 3.2015)
70
+ mime-types-data (3.2016.0521)
71
+ mini_portile2 (2.1.0)
72
+ minitest (5.9.0)
73
+ nio4r (1.2.1)
74
+ nokogiri (1.6.8)
75
+ mini_portile2 (~> 2.1.0)
76
+ pkg-config (~> 1.1.7)
77
+ pkg-config (1.1.7)
78
+ rack (2.0.0.rc1)
79
+ json
80
+ rack-test (0.6.3)
81
+ rack (>= 1.0)
82
+ rails (5.0.0.rc1)
83
+ actioncable (= 5.0.0.rc1)
84
+ actionmailer (= 5.0.0.rc1)
85
+ actionpack (= 5.0.0.rc1)
86
+ actionview (= 5.0.0.rc1)
87
+ activejob (= 5.0.0.rc1)
88
+ activemodel (= 5.0.0.rc1)
89
+ activerecord (= 5.0.0.rc1)
90
+ activesupport (= 5.0.0.rc1)
91
+ bundler (>= 1.3.0, < 2.0)
92
+ railties (= 5.0.0.rc1)
93
+ sprockets-rails (>= 2.0.0)
94
+ rails-deprecated_sanitizer (1.0.3)
95
+ activesupport (>= 4.2.0.alpha)
96
+ rails-dom-testing (1.0.7)
97
+ activesupport (>= 4.2.0.beta, < 5.0)
98
+ nokogiri (~> 1.6.0)
99
+ rails-deprecated_sanitizer (>= 1.0.1)
100
+ rails-html-sanitizer (1.0.3)
101
+ loofah (~> 2.0)
102
+ railties (5.0.0.rc1)
103
+ actionpack (= 5.0.0.rc1)
104
+ activesupport (= 5.0.0.rc1)
105
+ method_source
106
+ rake (>= 0.8.7)
107
+ thor (>= 0.18.1, < 2.0)
108
+ rake (10.5.0)
109
+ rspec (3.4.0)
110
+ rspec-core (~> 3.4.0)
111
+ rspec-expectations (~> 3.4.0)
112
+ rspec-mocks (~> 3.4.0)
113
+ rspec-core (3.4.4)
114
+ rspec-support (~> 3.4.0)
115
+ rspec-expectations (3.4.0)
116
+ diff-lcs (>= 1.2.0, < 2.0)
117
+ rspec-support (~> 3.4.0)
118
+ rspec-mocks (3.4.1)
119
+ diff-lcs (>= 1.2.0, < 2.0)
120
+ rspec-support (~> 3.4.0)
121
+ rspec-support (3.4.1)
122
+ simplecov (0.11.2)
123
+ docile (~> 1.1.0)
124
+ json (~> 1.8)
125
+ simplecov-html (~> 0.10.0)
126
+ simplecov-html (0.10.0)
127
+ sprockets (3.6.1)
128
+ concurrent-ruby (~> 1.0)
129
+ rack (> 1, < 3)
130
+ sprockets-rails (3.0.4)
131
+ actionpack (>= 4.0)
132
+ activesupport (>= 4.0)
133
+ sprockets (>= 3.0.0)
134
+ thor (0.19.1)
135
+ thread_safe (0.3.5)
136
+ tzinfo (1.2.2)
137
+ thread_safe (~> 0.1)
138
+ websocket-driver (0.6.4)
139
+ websocket-extensions (>= 0.1.0)
140
+ websocket-extensions (0.1.2)
141
+
142
+ PLATFORMS
143
+ ruby
144
+
145
+ DEPENDENCIES
146
+ appraisal (~> 2.1)
147
+ bundler (~> 1.12)
148
+ codeclimate-test-reporter
149
+ light-services!
150
+ rails (= 5.0.0.rc1)
151
+ rake (~> 10.0)
152
+ rspec (~> 3.0)
153
+ simplecov (~> 0.11.2)
154
+
155
+ BUNDLED WITH
156
+ 1.14.4
@@ -0,0 +1,57 @@
1
+ module Light
2
+ module Services
3
+ class Base
4
+ # Includes
5
+ include Light::Services::Parameters
6
+ include Light::Services::Callbacks
7
+
8
+ # Getters
9
+ attr_reader :errors, :warnings
10
+
11
+ def initialize(args = {})
12
+ @args = args
13
+
14
+ initialize_params
15
+ initialize_outputs
16
+
17
+ @errors = Light::Services::Messages.new
18
+ @warnings = Light::Services::Messages.new
19
+ end
20
+
21
+ def call
22
+ run_service
23
+ end
24
+
25
+ def success?
26
+ errors.blank?
27
+ end
28
+
29
+ def any_warnings?
30
+ warnings.any?
31
+ end
32
+
33
+ class << self
34
+ def call(args = {})
35
+ service = new(args)
36
+ service.call
37
+ service
38
+ end
39
+
40
+ alias run call
41
+ end
42
+
43
+ private
44
+
45
+ # Getters
46
+ attr_reader :args
47
+
48
+ def run_service
49
+ run_callbacks(:before)
50
+ run if success?
51
+ run_callbacks(:after) if success?
52
+ run_callbacks(:finally, break: false)
53
+ success?
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,45 @@
1
+ module Light
2
+ module Services
3
+ module Callbacks
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.class_eval do
7
+ class << self
8
+ attr_accessor :callbacks
9
+ end
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def run_callbacks(type, opts = {})
16
+ opts[:break] = true unless opts.key?(:break)
17
+
18
+ callbacks = (self.class.callbacks || []).select { |callback| callback[:type] == type }
19
+ callbacks.each do |callback|
20
+ break if !success? && opts[:break]
21
+ send(callback[:method])
22
+ end
23
+ end
24
+
25
+ module ClassMethods
26
+ def before(method)
27
+ set_callback(:before, method)
28
+ end
29
+
30
+ def after(method)
31
+ set_callback(:after, method)
32
+ end
33
+
34
+ def finally(method)
35
+ set_callback(:finally, method)
36
+ end
37
+
38
+ def set_callback(type, method)
39
+ self.callbacks ||= []
40
+ self.callbacks << { type: type, method: method }
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ module Light
2
+ module Services
3
+ class Error < StandardError; end
4
+ class ParamRequired < Light::Services::Error; end
5
+ class ParamType < Light::Services::Error; end
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ module Light
2
+ module Services
3
+ class Messages
4
+ def initialize
5
+ @storage = {}
6
+ end
7
+
8
+ def add(key, message)
9
+ @storage[key] ||= []
10
+ @storage[key] << message
11
+ end
12
+
13
+ def from_record(record)
14
+ record.errors.to_h.each do |key, value|
15
+ add(key, value)
16
+ end
17
+ end
18
+
19
+ def delete(key)
20
+ @storage.delete(key)
21
+ end
22
+
23
+ def blank?
24
+ @storage.empty?
25
+ end
26
+
27
+ def any?
28
+ !blank?
29
+ end
30
+
31
+ def to_hash
32
+ @storage
33
+ end
34
+
35
+ private
36
+
37
+ # Getters/Setters
38
+ attr_accessor :storage
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,97 @@
1
+ module Light
2
+ module Services
3
+ module Parameters
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.class_eval do
7
+ class << self
8
+ attr_accessor :parameters
9
+ attr_accessor :outputs
10
+ end
11
+ end
12
+ end
13
+
14
+ def params
15
+ @params_storage.to_hash
16
+ end
17
+
18
+ def outputs
19
+ @outputs_storage.to_hash
20
+ end
21
+
22
+ private
23
+
24
+ def initialize_params
25
+ @params_storage = Light::Services::Variables.new
26
+
27
+ (self.class.parameters || []).each do |options|
28
+ # Skip or raise exception if parameter not exist
29
+ unless args.key?(options[:name])
30
+ next unless options[:required]
31
+ raise Light::Services::ParamRequired, "Parameter \"#{options[:name]}\" is required"
32
+ end
33
+
34
+ # Load parameter value
35
+ value = args[options[:name]]
36
+
37
+ # Check type of parameter
38
+ if options[:type] && !value.is_a?(options[:type])
39
+ raise Light::Services::ParamType, "Type of \"#{options[:name]}\" must be \"#{options[:type]}\""
40
+ end
41
+
42
+ # Create instance variable and getter
43
+ @params_storage.add(options[:name], value)
44
+
45
+ define_singleton_method options[:name] do
46
+ @params_storage.get(options[:name])
47
+ end
48
+
49
+ define_singleton_method "#{options[:name]}=" do |val|
50
+ @params_storage.add(options[:name], val)
51
+ end
52
+ end
53
+
54
+ @params_storage
55
+ end
56
+
57
+ def initialize_outputs
58
+ @outputs_storage = Light::Services::Variables.new
59
+
60
+ (self.class.outputs || []).each do |options|
61
+ @outputs_storage.add(options[:name], options[:value])
62
+
63
+ define_singleton_method options[:name] do
64
+ @outputs_storage.get(options[:name])
65
+ end
66
+
67
+ define_singleton_method "#{options[:name]}=" do |value|
68
+ @outputs_storage.add(options[:name], value)
69
+ end
70
+ end
71
+
72
+ @outputs_storage
73
+ end
74
+
75
+ module ClassMethods
76
+ def param(name, options = {})
77
+ self.parameters ||= []
78
+ self.parameters << {
79
+ name: name,
80
+ required: options.fetch(:required, true),
81
+ public: options.fetch(:private, false),
82
+ type: options[:type] || nil
83
+ }
84
+ end
85
+
86
+ def output(name, value = nil, options = {})
87
+ self.outputs ||= []
88
+ self.outputs << {
89
+ name: name,
90
+ value: value,
91
+ public: options.fetch(:private, false)
92
+ }
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,38 @@
1
+ module Light
2
+ module Services
3
+ class Variables
4
+ def initialize
5
+ @storage = {}
6
+ end
7
+
8
+ def add(key, variable)
9
+ @storage[key] = variable
10
+ end
11
+
12
+ def get(key)
13
+ @storage[key]
14
+ end
15
+
16
+ def delete(key)
17
+ @storage.delete(key)
18
+ end
19
+
20
+ def blank?
21
+ @storage.empty?
22
+ end
23
+
24
+ def any?
25
+ !blank?
26
+ end
27
+
28
+ def to_hash
29
+ @storage
30
+ end
31
+
32
+ private
33
+
34
+ # Getters/Setters
35
+ attr_accessor :storage
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ module Light
2
+ module Services
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require 'light/services/version'
2
+ require 'light/services/exceptions'
3
+ require 'light/services/messages'
4
+ require 'light/services/variables'
5
+ require 'light/services/parameters'
6
+ require 'light/services/callbacks'
7
+ require 'light/services/base'
8
+
9
+ module Light
10
+ module Services
11
+ end
12
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: 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 = Light::Services::VERSION
9
+ spec.authors = ['Andrew Emelianenko']
10
+ spec.email = ['emelianenko.web@gmail.com']
11
+
12
+ spec.summary = 'Light pattern Services Object for Ruby/Rails'
13
+ spec.description = 'Light pattern Services Object for Ruby/Rails from Light Ruby'
14
+ spec.homepage = 'https://github.com/light-ruby/light-services'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`
18
+ .split("\x0")
19
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
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_dependency 'rails', '> 4.0'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.12'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'appraisal', '~> 2.1'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ spec.add_development_dependency 'simplecov', '~> 0.11.2'
32
+ spec.add_development_dependency 'codeclimate-test-reporter'
33
+ end
metadata ADDED
@@ -0,0 +1,173 @@
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
+ - Andrew Emelianenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
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.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
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: appraisal
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.11.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.11.2
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Light pattern Services Object for Ruby/Rails from Light Ruby
112
+ email:
113
+ - emelianenko.web@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".codeclimate.yml"
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".rubocop.yml"
122
+ - ".ruby-version"
123
+ - ".travis.yml"
124
+ - Appraisals
125
+ - CODE_OF_CONDUCT.md
126
+ - Gemfile
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - bin/console
131
+ - bin/setup
132
+ - gemfiles/rails_4_0.gemfile
133
+ - gemfiles/rails_4_0.gemfile.lock
134
+ - gemfiles/rails_4_1.gemfile
135
+ - gemfiles/rails_4_1.gemfile.lock
136
+ - gemfiles/rails_4_2.gemfile
137
+ - gemfiles/rails_4_2.gemfile.lock
138
+ - gemfiles/rails_5_0.gemfile
139
+ - gemfiles/rails_5_0.gemfile.lock
140
+ - lib/light/services.rb
141
+ - lib/light/services/base.rb
142
+ - lib/light/services/callbacks.rb
143
+ - lib/light/services/exceptions.rb
144
+ - lib/light/services/messages.rb
145
+ - lib/light/services/parameters.rb
146
+ - lib/light/services/variables.rb
147
+ - lib/light/services/version.rb
148
+ - light-services.gemspec
149
+ homepage: https://github.com/light-ruby/light-services
150
+ licenses:
151
+ - MIT
152
+ metadata: {}
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 2.6.10
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: Light pattern Services Object for Ruby/Rails
173
+ test_files: []