gate 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 31bc2ddaaad2763723a6461081c9af24bf28638bf1a65508ee78c161fc59ea8b
4
- data.tar.gz: 2d1996e71c376ce3b20798c57bbeaf158144cbeced2823f34ba3864906ffa0b9
3
+ metadata.gz: c22d3f53ed88e1d283f720231af1c4a8021a1be6e0a622344b6060cf5b205f21
4
+ data.tar.gz: d796d15296a46a91024811b362338d35c53dca326955f561609861518a0844ec
5
5
  SHA512:
6
- metadata.gz: 1ceccd09976e8a763273033318bc5b6db8a740155d4d75f3badc06793f51fe9f37a955baed090f96b7e229b2d3fc46b39d32e6ee9ba9fc76636d61184fc6bbf5
7
- data.tar.gz: 4a578fb794d00e413e4114eb7463f14307313d73f286cd644d6308b388e7fc5a38c7407be7ac03498a43c9402c00730ad542524a0209342592047778e9b59d18
6
+ metadata.gz: d3ec912ce725441be96af00f68272dc069be63c035e26999d2d8c63ae3376402b9960d66646ae0891b87614d8c28c7f96f95ed77cba38b85965e73f25cb43fa2
7
+ data.tar.gz: 4e9eaf0e4c2bde5ed8ae01815e62737622f4bfba29ef6e045e1c9ae3fc163224996d213c35feb5ff1d695cb9ee50d47b25ad2cbf65e905e5feb79cc6ce97ccf1
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /test/dummy/log/*
data/README.md CHANGED
@@ -6,7 +6,9 @@
6
6
  [![Code Climate](https://codeclimate.com/github/monterail/gate/badges/gpa.svg)](https://codeclimate.com/github/monterail/gate)
7
7
  [![Test Coverage](https://codeclimate.com/github/monterail/gate/badges/coverage.svg)](https://codeclimate.com/github/monterail/gate/coverage)
8
8
 
9
- Gate is a small wrapper on [dry-validation](http://dry-rb.org/gems/dry-validation/) that might be used as Command in CQRS pattern. It will raise `InvalidCommand` error for invalid input and provide simple struct with access to coerced input.
9
+ Gate is a small wrapper on [dry-validation](http://dry-rb.org/gems/dry-validation/) that might be used as Command in CQRS pattern or replace [Strong Params](http://api.rubyonrails.org/classes/ActionController/Parameters.html) in [Ruby on Rails](https://rubyonrails.org/) applications.
10
+
11
+ `Gate::Command` will raise `InvalidCommand` error for invalid input and provide simple struct with access to coerced input.
10
12
 
11
13
  ## Installation
12
14
 
@@ -24,7 +26,7 @@ Or install it yourself as:
24
26
 
25
27
  $ gem install gate
26
28
 
27
- ## Usage
29
+ ## Command Usage
28
30
 
29
31
  Define structure
30
32
 
@@ -54,6 +56,39 @@ rescue DoSomethingCommand::InvalidCommand => e
54
56
  end
55
57
  ```
56
58
 
59
+ ## Rails Usage
60
+
61
+ Define schemas per action
62
+
63
+ ```ruby
64
+ class ExampleController < ActionController::Base
65
+ include Gate::Rails
66
+
67
+ before_action :validate_params, if: { |c| c.params_schema_registered? }
68
+
69
+ # Define schema just before action method
70
+ def_schema do
71
+ required(:id).filled
72
+ required(:message).schema do
73
+ required(:title).filled
74
+ optional(:value).maybe(:decimal?)
75
+ end
76
+ end
77
+
78
+ def foo
79
+ # you can access Dry::Validation result with:
80
+ validated_params
81
+ end
82
+
83
+ # default handler for invalid params which you can override
84
+ def handle_invalid_params(_errors)
85
+ # errors is Dry::Validation messages hash
86
+
87
+ head :bad_request
88
+ end
89
+ end
90
+ ```
91
+
57
92
  ## Development
58
93
 
59
94
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -26,5 +26,6 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "codeclimate-test-reporter", "~> 1.0"
27
27
  spec.add_development_dependency "minitest", "~> 5.11"
28
28
  spec.add_development_dependency "pry", "~> 0.11"
29
+ spec.add_development_dependency "rails", "~> 5.2"
29
30
  spec.add_development_dependency "rake", "~> 12.0"
30
31
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Gate
2
4
  InvalidCommand = Class.new(StandardError)
3
5
  end
@@ -5,4 +7,5 @@ end
5
7
  require "dry-validation"
6
8
 
7
9
  require "gate/command"
10
+ require "gate/rails"
8
11
  require "gate/version"
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gate
4
+ module Rails
5
+ attr_reader :validated_params
6
+
7
+ SchemaNotDefined = Class.new(StandardError)
8
+
9
+ def self.included(base)
10
+ base.send(:extend, ClassMethods)
11
+ end
12
+
13
+ module ClassMethods
14
+ def params_schemas
15
+ @params_schemas ||= {}
16
+ end
17
+
18
+ def fetch_params_schema(schema_name)
19
+ params_schemas.fetch(schema_name) do
20
+ raise SchemaNotDefined, "Missing `#{schema_name}` schema"
21
+ end
22
+ end
23
+
24
+ def def_schema(&block)
25
+ @_schema = Dry::Validation.Params(&block)
26
+ end
27
+
28
+ def method_added(method_name)
29
+ return unless instance_variable_defined?(:@_schema)
30
+
31
+ params_schemas[method_name] = @_schema
32
+ remove_instance_variable(:@_schema)
33
+ end
34
+ end
35
+
36
+ def validate_params
37
+ result = params_schema.call(request.params)
38
+
39
+ if result.success?
40
+ @validated_params = result.output
41
+ else
42
+ handle_invalid_params(result.messages)
43
+ end
44
+ end
45
+
46
+ def handle_invalid_params(_errors)
47
+ head :bad_request
48
+ end
49
+
50
+ def params_schema_registered?
51
+ self.class.params_schemas.key?(params_schema_name)
52
+ end
53
+
54
+ def params_schema
55
+ self.class.fetch_params_schema(params_schema_name)
56
+ end
57
+
58
+ def params_schema_name
59
+ action_name.to_sym
60
+ end
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module Gate
2
- VERSION = "0.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Dudulski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-12 00:00:00.000000000 Z
11
+ date: 2018-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-validation
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0.11'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.2'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rake
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -116,6 +130,7 @@ files:
116
130
  - gate.gemspec
117
131
  - lib/gate.rb
118
132
  - lib/gate/command.rb
133
+ - lib/gate/rails.rb
119
134
  - lib/gate/version.rb
120
135
  homepage: https://github.com/monterail/gate
121
136
  licenses: