bbservices 0.0.1

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/base/service.rb +147 -0
  3. data/lib/bbservices.rb +6 -0
  4. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 22aeb43995310deb16fea74f6252366b8ca9317fbbcbba4a11f14d6043c9ece5
4
+ data.tar.gz: bb7f6388efc24fc4aa503979947572073a2971c81004484daedff1af7b12a903
5
+ SHA512:
6
+ metadata.gz: 021d855f30e314293d71a97ac2ff52cdf42cea8b1de756e40ecfa813c31c42a9ada46ceb81f133ebefd9d258fe495ac2f4bb65c31439885a60f6c30d96bec754
7
+ data.tar.gz: 4c3f86baaa06b0b34e7c6d3b17b7ddf650539c15ca3f107c6082891701e3816299b9610b8a3458997c9cbd9d299784457323c89e507fca55545fd2ac63190195
@@ -0,0 +1,147 @@
1
+ module BBServices
2
+
3
+ ##
4
+ # This class handles the building of a basic / generic service
5
+ class Service
6
+
7
+ def self.service_class(klass)
8
+ @service_class = klass
9
+ end
10
+
11
+ def initialize
12
+
13
+ @object = nil
14
+
15
+ @successful = false
16
+
17
+ @errors = nil
18
+
19
+ @service_class = nil
20
+ end
21
+
22
+ def service_class=(klass)
23
+ @service_class = klass
24
+ end
25
+
26
+ def service_class
27
+ @service_class ? @service_class : self.class.instance_variable_get(:@service_class)
28
+ end
29
+
30
+ def run(&block)
31
+ begin
32
+ initialize_service
33
+ run_service
34
+ rescue StandardError => e
35
+ set_successful(false)
36
+ set_error(e)
37
+ ensure
38
+ call_block(&block)
39
+ end
40
+ end
41
+
42
+ def run!(&block)
43
+ begin
44
+ initialize_service
45
+ run_service!
46
+ rescue StandardError => e
47
+ set_successful(false)
48
+ set_error(e)
49
+ raise e
50
+ end
51
+ end
52
+
53
+ def params=(params)
54
+ @params = params
55
+ end
56
+
57
+ def associated_params=(params)
58
+ @associated_params = params
59
+ end
60
+
61
+ def params
62
+ @params
63
+ end
64
+
65
+ def associated_params
66
+ @associated_params
67
+ end
68
+
69
+ def param_for(key)
70
+ if @params
71
+ @params[key]
72
+ end
73
+ end
74
+
75
+ def associated_param_for(key)
76
+ if @associated_params
77
+ @associated_params[key]
78
+ end
79
+ end
80
+
81
+ def succeeded?
82
+ (@successful && !has_errors? )
83
+ end
84
+
85
+ def failed?
86
+ !succeeded?
87
+ end
88
+
89
+ def success(&block)
90
+ if succeeded?
91
+ yield
92
+ end
93
+ end
94
+
95
+ def failure(&block)
96
+ if failed?
97
+ yield
98
+ end
99
+ end
100
+
101
+ def object
102
+ @object
103
+ end
104
+
105
+ def errors
106
+ @errors
107
+ end
108
+
109
+ def has_errors?
110
+ @errors && @errors.length > 0
111
+ end
112
+
113
+ protected
114
+
115
+ def initialize_service
116
+
117
+ end
118
+
119
+ def run_service
120
+ @successful = true
121
+ end
122
+
123
+ def run_service!
124
+ @successful = true
125
+ end
126
+
127
+ def set_error(error)
128
+ if !@errors
129
+ @errors = []
130
+ end
131
+
132
+ @errors << error
133
+ end
134
+
135
+ def set_successful(successful = true)
136
+ @successful = successful
137
+ end
138
+
139
+ private
140
+
141
+ def call_block(&block)
142
+ if block_given?
143
+ yield(self)
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,6 @@
1
+ require_relative "base/service"
2
+ require_relative "rails/service"
3
+ require_relative "rails/new"
4
+ require_relative "rails/destroy"
5
+ require_relative "rails/create"
6
+ require_relative "rails/create_transactioned"
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bbservices
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stuart Farnaby, Big Bear Studios
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/base/service.rb
20
+ - lib/bbservices.rb
21
+ homepage: https://gitlab.com/big-bear-studios-open-source/bbservices
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.0.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: A simple service library for Ruby / Rails
44
+ test_files: []