dry_generators_rails 0.4.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 83dce3a1768ef657b9feab5680b32c33041e380b40bfe87ab3b59f814705661d
4
- data.tar.gz: 6c8ccb76d8cdd7058f32e2fa52af30663ae455025437fd0246906000741e90ac
3
+ metadata.gz: d85b4609b83ce411b59c8dc93e7fff04df610795980a97958aa9c687896bb055
4
+ data.tar.gz: 8d2a07defac4fe612c29706b89323d6e6050b16b46513734f6e73612bf314b44
5
5
  SHA512:
6
- metadata.gz: 479fcad4c31a7a9f685e16be697640b2e002ef3a632f2db39e81f519ebea15852cd66cf1bbef341d6a32c5e1f822f9e3182609940818bea4582418e737f59cee
7
- data.tar.gz: ae494ad3d792f64f152ee55f0f48ce4514ea68d28bf16edce498d29a599877efc428181d87536ef2f6ff360d6b30002c8b8fb104408e66a2c2b02b75d87454a8
6
+ metadata.gz: 1861d68963bd25bd1ebb031fe9d755027b7750b81a1fdc074ee91d2a37ada58386c05a0302fe351834cc6bbea6afad90181539f8ab6d3937cf459d10a656c255
7
+ data.tar.gz: ee2556816624e1fd0d254d4df0cb373b56860306dab368d8e0482887a698a653e113f54fba4da3d136198bec234841578469a5207ce589ecbc8b86a299ea04f9
@@ -1,3 +1,3 @@
1
1
  module DryGeneratorsRails
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -1,8 +1,19 @@
1
1
  Description:
2
2
  Generates a dry-validation contract
3
3
 
4
+ Accepts an Array of params.
5
+
6
+ A single param name defaults to an optional string [email]
7
+ A name name and type will be optional [age:integer]
8
+ A type must be specified to make it required [age:integer:required]
9
+ Any number of params can be supplied [age:integer:required, alive:boolean]
10
+
4
11
  Example:
5
12
  rails generate validation Thing
13
+ rails generate validation Thing --params email
14
+ rails generate validation Thing --params age:integer
15
+ rails generate validation Thing --params age:integer:required
16
+ rails generate validation Thing --params age:integer:required alive:boolean
6
17
 
7
18
  This will create:
8
19
  app/dry/validations/thing.rb
@@ -0,0 +1,13 @@
1
+ module Validations
2
+ class <%= file_name.camelize %> < Dry::Validation::Contract
3
+ params do
4
+ <% @fields.each do |field| %>
5
+ <% if field[:required] %>
6
+ <%= "required(:#{field[:name]}).value(:#{field[:type]})" %>
7
+ <% else %>
8
+ <%= "optional(:#{field[:name]}).value(:#{field[:type]})" %>
9
+ <% end %>
10
+ <% end %>
11
+ end
12
+ end
13
+ end
@@ -4,6 +4,7 @@ module Dry
4
4
  module Generators
5
5
  class ValidationGenerator < Base
6
6
  source_root File.expand_path('templates', __dir__)
7
+ class_option :params, type: :array, default: []
7
8
 
8
9
  def check_requirements
9
10
  raise 'NAME must be provided' unless file_name.present?
@@ -12,18 +13,36 @@ module Dry
12
13
  def set_instance_variables
13
14
  @name = file_name.underscore
14
15
  @class_name = "#{@name.camelize}"
16
+ @fields = []
17
+ end
18
+
19
+ # [:name, :type, :required]
20
+ def process_params
21
+ options['params'].each do |field|
22
+ fields = field.split(':')
23
+ required = false
24
+ type = 'string'
25
+
26
+ if fields.length == 3
27
+ required = true
28
+ fields.pop
29
+ end
30
+
31
+ type = fields.pop if fields.length == 2
32
+ @fields << { name: fields.first, type: type, required: required }
33
+ end
15
34
  end
16
35
 
17
36
  def copy_validator
18
37
  path = File.join(VALIDATIONS_PATH, "#{@name}.rb")
19
- template 'validation_template.rb', path
38
+ template 'validation_template.erb', path
20
39
  end
21
40
 
22
41
  def copy_spec
23
42
  if Dir.exist? File.join(Rails.root, 'spec')
24
43
  spec_path = File.join(SPEC_PATH, 'validations')
25
44
  path = File.join(spec_path, "#{@name}_spec.rb")
26
- template 'validation_spec_template.rb', path
45
+ template 'validation_spec_template.erb', path
27
46
  end
28
47
  end
29
48
 
@@ -31,7 +50,7 @@ module Dry
31
50
  if Dir.exist? File.join(Rails.root, 'test')
32
51
  test_path = File.join(TEST_PATH, 'validations')
33
52
  path = File.join(test_path, "#{@name}_test.rb")
34
- template 'validation_test_template.rb', path
53
+ template 'validation_test_template.erb', path
35
54
  end
36
55
  end
37
56
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry_generators_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Turknett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-16 00:00:00.000000000 Z
11
+ date: 2020-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -111,9 +111,9 @@ files:
111
111
  - lib/generators/dry/struct/templates/struct_template.rb
112
112
  - lib/generators/dry/struct/templates/struct_test_template.rb
113
113
  - lib/generators/dry/validation/USAGE
114
- - lib/generators/dry/validation/templates/validation_spec_template.rb
115
- - lib/generators/dry/validation/templates/validation_template.rb
116
- - lib/generators/dry/validation/templates/validation_test_template.rb
114
+ - lib/generators/dry/validation/templates/validation_spec_template.erb
115
+ - lib/generators/dry/validation/templates/validation_template.erb
116
+ - lib/generators/dry/validation/templates/validation_test_template.erb
117
117
  - lib/generators/dry/validation/validation_generator.rb
118
118
  - lib/tasks/dry_generators_rails_tasks.rake
119
119
  homepage: https://github.com/johnTurknett/dry_generators_rails
@@ -1,9 +0,0 @@
1
- module Validations
2
- class <%= file_name.camelize %> < Dry::Validation::Contract
3
- params do
4
- # required(:age).filled(:integer)
5
- end
6
-
7
- # rule(:age) { key.failure("must be greater than 18") if value < 18 }
8
- end
9
- end