moist-action 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ca7a3cb8001d0746c9934e3879b29b5593658d5
4
+ data.tar.gz: 3b8a4888f4ed289f8722658af446400094c15da2
5
+ SHA512:
6
+ metadata.gz: 26c64c8a5c3f9c93ba8e50a3739d7da50cba9417eb9463b11d4395c4ad6b436d318821d542b73c5ee9b9e1cc7ce467d7b21b9ab720b802d18bb43bf9d94a9c9e
7
+ data.tar.gz: 2b97a3ab4a86f7f04a913c0d6853dbb60d1ed3d8226c6ffa840b82f920c9167fc12ea44d3d231fe639c22092c6095daa01d93baaa680c4e72f7f61680916f7c6
@@ -0,0 +1,73 @@
1
+ require 'dry/validation'
2
+ require 'fear/either'
3
+ require 'moist/action/version'
4
+
5
+ module Moist
6
+ # This module provides validation logic for controller actions.
7
+ # @example
8
+ # class RegisterUser
9
+ # include Moist::Action
10
+ #
11
+ # params do
12
+ # required(:name).filled(:str?)
13
+ # required(:email).filled(format?: EMAIL_REGEX)
14
+ # required(:age).maybe(:int?)
15
+ # end
16
+ #
17
+ # def perform(params)
18
+ # User.create(params)
19
+ # end
20
+ #
21
+ module Action
22
+ include Fear::Either::Mixin
23
+
24
+ module Params # rubocop:disable Style/Documentation
25
+ EMPTY_FORM = Dry::Validation.Form()
26
+
27
+ # Specify params validator
28
+ # @example
29
+ # class ChangeUsername
30
+ # include Moist::Action
31
+ #
32
+ # params do
33
+ # required(:username).filled?(:str?)
34
+ # end
35
+ # end
36
+ #
37
+ # @see http://dry-rb.org/gems/dry-validation/forms/
38
+ def params(&block)
39
+ if block_given?
40
+ @params = Dry::Validation.Form(&block)
41
+ else
42
+ @params || EMPTY_FORM
43
+ end
44
+ end
45
+ end
46
+
47
+ class << self
48
+ def included(base)
49
+ base.extend(Params)
50
+ end
51
+ end
52
+
53
+ # @param params [{Symbol => any}]
54
+ # @return [Fear::Right<any>, Fear::Left<{Symbol => <String>}>] returns +Fear::Right+ in
55
+ # case of successful validation or +Fear::Left+ in case of error.
56
+ #
57
+ def call(params)
58
+ validation_result = self.class.params.(params)
59
+ if validation_result.success?
60
+ Right(perform(validation_result.output))
61
+ else
62
+ Left(validation_result.messages)
63
+ end
64
+ end
65
+
66
+ # Template method to be overridden by an Action
67
+ # @param _params [{Symbol => any}]
68
+ # @return [<any>]
69
+ def perform(_params)
70
+ raise NotImplementedError
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ module Moist
2
+ module Action
3
+ VERSION = '0.2.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fear/rspec'
4
+ require 'spec_helper'
5
+
6
+ RSpec.describe Moist::Action do
7
+ context 'actions without defined params' do
8
+ subject { action.new.(foo: 'bar') }
9
+
10
+ let(:action) do
11
+ Class.new do
12
+ include Moist::Action
13
+
14
+ def perform(params)
15
+ params
16
+ end
17
+ end
18
+ end
19
+
20
+ it 'strip out all given params' do
21
+ is_expected.to be_right_of({})
22
+ end
23
+ end
24
+
25
+ context 'actions with required params' do
26
+ let(:action) do
27
+ Class.new do
28
+ include Moist::Action
29
+
30
+ params do
31
+ required(:name).filled(:str?)
32
+ end
33
+
34
+ def perform(params)
35
+ params
36
+ end
37
+ end
38
+ end
39
+
40
+ context 'required param given' do
41
+ subject { action.new.(name: 'Carl', surname: 'Lazlo') }
42
+
43
+ it 'keep this param and strip out other params' do
44
+ is_expected.to be_right_of(name: 'Carl')
45
+ end
46
+ end
47
+
48
+ context 'required param is invalid' do
49
+ subject { action.new.(name: '') }
50
+
51
+ it 'keep this param and strip out other params' do
52
+ is_expected.to be_left_of(name: ['must be filled'])
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,17 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'bundler/setup'
5
+ require 'moist/action'
6
+
7
+ RSpec.configure do |config|
8
+ # Enable flags like --only-failures and --next-failure
9
+ config.example_status_persistence_file_path = '.rspec_status'
10
+
11
+ # Disable RSpec exposing methods globally on `Module` and `main`
12
+ config.disable_monkey_patching!
13
+
14
+ config.expect_with :rspec do |c|
15
+ c.syntax = :expect
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moist-action
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Tema Bolshakov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-14 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.16.1
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.16.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: fear-rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 12.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: 12.3.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: 3.7.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.7.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 0.53.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.53.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.15.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 0.15.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: dry-validation
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 0.11.1
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 0.11.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: fear
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '='
116
+ - !ruby/object:Gem::Version
117
+ version: 0.7.0
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '='
123
+ - !ruby/object:Gem::Version
124
+ version: 0.7.0
125
+ description: Better controller actions
126
+ email:
127
+ - either.free@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - lib/moist/action.rb
133
+ - lib/moist/action/version.rb
134
+ - spec/moist/action_spec.rb
135
+ - spec/spec_helper.rb
136
+ homepage: https://github.com/moist-rb/action
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.5.2.1
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Better controller actions
160
+ test_files:
161
+ - spec/spec_helper.rb
162
+ - spec/moist/action_spec.rb