mutations 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mutations (0.5.0)
4
+ mutations (0.5.2)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -177,7 +177,7 @@ Here, we pass two hashes to CreateComment. Even if the params[:comment] hash has
177
177
  end
178
178
  ```
179
179
 
180
- See a full list of options here: TODO
180
+ See a full list of options [here](https://github.com/cypriss/mutations/wiki/Filtering-Input).
181
181
 
182
182
  ## How do I write an execute method?
183
183
 
@@ -249,7 +249,7 @@ outcome.errors.symbolic # => {password_confirmation: :doesnt_match}
249
249
  outcome.errors.message # => {password_confirmation: "Your passwords don't match"}
250
250
  ```
251
251
 
252
- If you want to tie the validation messages into your I18n system, you'll need to write a custom error message generator. TODO: See docs.
252
+ If you want to tie the validation messages into your I18n system, you'll need to [write a custom error message generator](https://github.com/cypriss/mutations/wiki/Custom-Error-Messages).
253
253
 
254
254
  ## FAQs
255
255
 
@@ -65,6 +65,11 @@ module Mutations
65
65
  end
66
66
  end
67
67
 
68
+ # Validates input, but doesn't call execute. Returns an Outcome with errors anyway.
69
+ def validate(*args)
70
+ new(*args).validation_outcome
71
+ end
72
+
68
73
  def input_filters
69
74
  @input_filters ||= begin
70
75
  if Command == self.superclass
@@ -91,6 +96,8 @@ module Mutations
91
96
  raise ArgumentError.new("All arguments must be hashes") unless a.is_a?(Hash)
92
97
  @original_hash.merge!(a)
93
98
  end
99
+
100
+ @filtered_input, @errors = self.input_filters.filter(@original_hash)
94
101
  end
95
102
 
96
103
  def input_filters
@@ -98,7 +105,6 @@ module Mutations
98
105
  end
99
106
 
100
107
  def execute!
101
- @filtered_input, @errors = self.input_filters.filter(@original_hash)
102
108
  return Outcome.new(false, nil, @errors) if @errors
103
109
 
104
110
  # IDEA/TODO: run validate block
@@ -111,6 +117,15 @@ module Mutations
111
117
  end
112
118
  end
113
119
 
120
+ # Runs input thru the filter and sets @filtered_input and @errors
121
+ def validation_outcome
122
+ if @errors
123
+ Outcome.new(false, nil, @errors)
124
+ else
125
+ Outcome.new(true, nil, nil)
126
+ end
127
+ end
128
+
114
129
  # add_error("name", :too_short)
115
130
  # add_error("colors.foreground", :not_a_color) # => to create errors = {colors: {foreground: :not_a_color}}
116
131
  # or, supply a custom message:
@@ -1,3 +1,3 @@
1
1
  module Mutations
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
data/spec/command_spec.rb CHANGED
@@ -24,7 +24,7 @@ describe "Command" do
24
24
  outcome = SimpleCommand.run(name: "JohnTooLong", email: "john@gmail.com")
25
25
 
26
26
  assert !outcome.success?
27
- assert :length, outcome.errors.symbolic[:name]
27
+ assert_equal :max_length, outcome.errors.symbolic[:name]
28
28
  end
29
29
 
30
30
  it "shouldn't throw an exception with run!" do
@@ -38,6 +38,18 @@ describe "Command" do
38
38
  end
39
39
  end
40
40
 
41
+ it "should do standalone validation" do
42
+ outcome = SimpleCommand.validate(name: "JohnLong", email: "john@gmail.com")
43
+ assert outcome.success?
44
+ assert_nil outcome.result
45
+ assert_nil outcome.errors
46
+
47
+ outcome = SimpleCommand.validate(name: "JohnTooLong", email: "john@gmail.com")
48
+ assert !outcome.success?
49
+ assert_nil outcome.result
50
+ assert_equal :max_length, outcome.errors.symbolic[:name]
51
+ end
52
+
41
53
  it "should merge multiple hashes" do
42
54
  outcome = SimpleCommand.run({name: "John", email: "john@gmail.com"}, {email: "bob@jones.com", amount: 5})
43
55
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mutations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-26 00:00:00.000000000 Z
12
+ date: 2012-10-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport