Skip to content

Commit f84f4ae

Browse files
authored
Create base service and base custom object (#26)
1 parent 5528b10 commit f84f4ae

File tree

11 files changed

+61
-48
lines changed

11 files changed

+61
-48
lines changed

Gemfile

+12-3
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,24 @@ gem 'kaminari', '~> 1.2', '>= 1.2.2'
9595
# Useful, common monads in idiomatic Ruby [https://github.com/dry-rb/dry-monads]
9696
gem 'dry-monads', '~> 1.4'
9797

98+
# A simple validation library [https://github.com/dry-rb/dry-validation]
99+
gem 'dry-validation', '~> 1.6'
100+
101+
# Typed structs and value objects [https://github.com/dry-rb/dry-struct]
102+
gem 'dry-struct', '~> 1.4'
103+
104+
# Type system for Ruby supporting coercions, constraints and complex types like structs, value objects, enums etc [https://github.com/dry-rb/dry-types]
105+
gem 'dry-types', '~> 1.5', '>= 1.5.1'
106+
107+
# DSL for declaring params and options of the initializer [https://github.com/dry-rb/dry-initializer]
108+
gem 'dry-initializer', '~> 3.1', '>= 3.1.1'
109+
98110
# Form object decoupled from models with validation, population and presentation [https://github.com/trailblazer/reform]
99111
gem 'reform', '~> 2.6'
100112

101113
# Automatically load and include all common Rails form features. [https://github.com/trailblazer/reform-rails]
102114
gem 'reform-rails', '~> 0.2.2'
103115

104-
# A simple validation library [https://github.com/dry-rb/dry-validation]
105-
gem 'dry-validation', '~> 1.6'
106-
107116
# Flexible authentication solution for Rails with Warden [https://github.com/heartcombo/devise]
108117
gem 'devise', '~> 4.8', '>= 4.8.1'
109118

Gemfile.lock

+8
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ GEM
133133
dry-initializer (~> 3.0)
134134
dry-logic (~> 1.0)
135135
dry-types (~> 1.5)
136+
dry-struct (1.4.0)
137+
dry-core (~> 0.5, >= 0.5)
138+
dry-types (~> 1.5)
139+
ice_nine (~> 0.11)
136140
dry-types (1.5.1)
137141
concurrent-ruby (~> 1.0)
138142
dry-container (~> 0.3)
@@ -155,6 +159,7 @@ GEM
155159
activesupport (>= 5.0)
156160
i18n (1.9.1)
157161
concurrent-ruby (~> 1.0)
162+
ice_nine (0.11.2)
158163
io-console (0.5.11)
159164
io-wait (0.2.1)
160165
irb (1.4.1)
@@ -326,7 +331,10 @@ DEPENDENCIES
326331
debug
327332
devise (~> 4.8, >= 4.8.1)
328333
doorkeeper (~> 5.5, >= 5.5.4)
334+
dry-initializer (~> 3.1, >= 3.1.1)
329335
dry-monads (~> 1.4)
336+
dry-struct (~> 1.4)
337+
dry-types (~> 1.5, >= 1.5.1)
330338
dry-validation (~> 1.6)
331339
factory_bot_rails (~> 6.2)
332340
faker!

app/lib/custom_objects/base.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
require 'dry-struct'
4+
5+
module CustomObjects
6+
class Base < Dry::Struct
7+
transform_keys(&:to_sym)
8+
end
9+
end

app/lib/types.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
require 'dry-types'
4+
5+
module Types
6+
include Dry.Types()
7+
end

app/services/base.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
require 'dry/monads/all'
4+
require 'dry-initializer'
5+
6+
class Base
7+
extend Dry::Initializer
8+
end

app/services/registration_service/register.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
# frozen_string_literal: true
22

3-
require 'dry/monads/do'
4-
5-
class RegistrationService::Register
3+
class RegistrationService::Register < Base
64
include Dry::Monads[:result]
75
include Dry::Monads::Do.for(:call)
86

9-
attr_reader :params
10-
11-
def initialize(params:)
12-
@params = params
13-
end
7+
option :params, type: Types::Hash
148

159
def call
1610
ActiveRecord::Base.transaction(requires_new: true) do

app/services/user_service/create.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
# frozen_string_literal: true
22

3-
require 'dry/monads/do'
4-
5-
class UserService::Create
3+
class UserService::Create < Base
64
include Dry::Monads[:result]
75
include Dry::Monads::Do.for(:call)
86

9-
attr_reader :params
10-
11-
def initialize(params:)
12-
@params = params
13-
end
7+
option :params, type: Types::Hash
148

159
def call
1610
user = User.new(params)

lib/generators/service/templates/create.html.erb

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
# frozen_string_literal: true
22

3-
require "dry/monads/do"
4-
5-
class <%= service_class_name %>::Create
3+
class <%= service_class_name %>::Create < Base
64
include Dry::Monads[:result]
75
include Dry::Monads::Do.for(:call)
86

9-
attr_reader :params
10-
11-
def initialize(params:)
12-
@params = params
13-
end
7+
option :params, type: Types::Hash
148

159
def call
1610
ActiveRecord::Base.transaction(requires_new: true) do

lib/generators/service/templates/destroy.html.erb

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
# frozen_string_literal: true
22

3-
require "dry/monads/do"
4-
5-
class <%= service_class_name %>::Destroy
3+
class <%= service_class_name %>::Destroy < Base
64
include Dry::Monads[:result]
75
include Dry::Monads::Do.for(:call)
86

9-
attr_reader :record
10-
11-
def initialize(record:)
12-
@record = record
13-
end
7+
option :record, type: Types.Constructor(Record)
148

159
def call
1610
ActiveRecord::Base.transaction(requires_new: true) do
@@ -28,3 +22,4 @@ class <%= service_class_name %>::Destroy
2822
end
2923
end
3024
end
25+
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# frozen_string_literal: true
22

3-
require "dry/monads/do"
4-
5-
class <%= service_class_name %>::List
3+
class <%= service_class_name %>::List < Base
64
include Dry::Monads[:result]
75
include Dry::Monads::Do.for(:call)
86

9-
def call(pagination: nil, query: nil)
7+
option :pagination, type: Types.Constructor(ParameterObject::Pagination)
8+
option :query, type: Types.Constructor(ParameterObject::Query)
9+
10+
def call
1011
Success(records.ransack(query.query).result.page(pagination.page).per(pagination.per_page))
1112
end
1213
end

lib/generators/service/templates/update.html.erb

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
# frozen_string_literal: true
22

3-
require "dry/monads/do"
4-
5-
class <%= service_class_name %>::Update
3+
class <%= service_class_name %>::Update < Base
64
include Dry::Monads[:result]
75
include Dry::Monads::Do.for(:call)
86

9-
attr_reader :params, :record
10-
11-
def initialize(params:, record:)
12-
@params = params
13-
@record = record
14-
end
7+
option :record, type: Types.Constructor(Record)
8+
option :params, type: Types::Hash
159

1610
def call
1711
ActiveRecord::Base.transaction(requires_new: true) do

0 commit comments

Comments
 (0)