reaction 0.2.8 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/reaction/action.rb +91 -37
- data/lib/reaction/errors/attribute_error.rb +12 -0
- data/lib/reaction/errors/param_error.rb +12 -0
- data/lib/reaction/has_attributes.rb +5 -1
- data/lib/reaction/has_params.rb +26 -3
- data/lib/reaction/param.rb +50 -0
- data/lib/reaction/param_builder.rb +20 -0
- data/lib/reaction/type.rb +40 -38
- data/lib/reaction/type_builder.rb +28 -0
- data/lib/reaction/types/raw_type.rb +2 -6
- data/lib/reaction/validator.rb +29 -0
- data/lib/reaction/validator_builder.rb +28 -0
- data/lib/reaction.rb +8 -12
- data/reaction.gemspec +1 -1
- metadata +9 -12
- data/lib/reaction/doc.rb +0 -19
- data/lib/reaction/each_validator.rb +0 -43
- data/lib/reaction/errors.rb +0 -72
- data/lib/reaction/has_docs.rb +0 -22
- data/lib/reaction/has_errors.rb +0 -15
- data/lib/reaction/has_metas.rb +0 -27
- data/lib/reaction/has_types.rb +0 -46
- data/lib/reaction/has_validators.rb +0 -51
- data/lib/reaction/is_documented.rb +0 -18
- data/lib/reaction/markdown.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 988b21cec0c58acf861ccabe5a11d7ba8e0bfdb5
|
4
|
+
data.tar.gz: 4333f04bdd5654cf30e149b24fb4111ba22091c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52a815000393b6985df7faed9533775183ee01372605490189696585bd63d012104adecb08618cae4d73128c0414d1505c839c43daca8e10761f1240e673c0ab
|
7
|
+
data.tar.gz: 5bb22ce6053954669c76d0e28ba323d622fbfa1330b136089ecb3e8ba5d37974d8d7855a3330e5f4f07680537efebd13cf8cd03c9a9a51d5d1fec27e78c9d657
|
data/lib/reaction/action.rb
CHANGED
@@ -1,55 +1,109 @@
|
|
1
1
|
module Reaction
|
2
2
|
class Action
|
3
3
|
include HasAttributes
|
4
|
-
include HasErrors
|
5
|
-
include HasParams
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
#
|
15
|
-
# Which allows us to use a validator to define who can set
|
16
|
-
# various attributes. Specifically, we limit certain attributes
|
17
|
-
# from being set by the request so end users can't set params
|
18
|
-
# that we don't expect them to set.
|
19
|
-
#
|
20
|
-
def initialize(params = {}, meta = {}, attributes = {})
|
21
|
-
set_params(params, meta)
|
22
|
-
set_attributes(attributes)
|
5
|
+
attr_accessor :successful
|
6
|
+
attr_accessor :result
|
7
|
+
attr_accessor :error
|
8
|
+
|
9
|
+
def initialize(param_values = {}, attribute_values = {})
|
10
|
+
set_params(param_values)
|
11
|
+
set_attributes(attribute_values)
|
23
12
|
end
|
24
13
|
|
25
|
-
def
|
26
|
-
|
27
|
-
|
14
|
+
def process
|
15
|
+
return false unless validate_attributes
|
16
|
+
_params.each do |name, param|
|
17
|
+
unless param.process
|
18
|
+
failure(param.error) and return false
|
19
|
+
end
|
20
|
+
end
|
28
21
|
perform
|
29
|
-
|
30
|
-
|
22
|
+
return !!@successful
|
23
|
+
end
|
24
|
+
|
25
|
+
def success(result)
|
26
|
+
@successful = true
|
27
|
+
@result = result
|
28
|
+
end
|
29
|
+
|
30
|
+
def failure(error)
|
31
|
+
@successful = false
|
32
|
+
@error = error
|
33
|
+
end
|
34
|
+
|
35
|
+
# This is how you retrieve a param. Pass in a mode of :raw in
|
36
|
+
# order to get the raw param if you happen to need that, but
|
37
|
+
# in general you shouldn't.
|
38
|
+
#
|
39
|
+
def param(name, mode = :converted)
|
40
|
+
if mode == :raw
|
41
|
+
_param(name).raw_value
|
42
|
+
else
|
43
|
+
_param(name).result
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def params
|
48
|
+
ret = {}
|
49
|
+
_params.each do |name, param|
|
50
|
+
ret[name] = param.result
|
51
|
+
end
|
52
|
+
ret
|
31
53
|
end
|
32
54
|
|
33
|
-
def
|
34
|
-
|
55
|
+
def set_params(param_values = {})
|
56
|
+
self.class.param_builders.each do |name, pb|
|
57
|
+
provided = param_values.keys.include?(name)
|
58
|
+
_params[name] = pb.build(self, param_values[name], provided)
|
59
|
+
end
|
35
60
|
end
|
36
61
|
|
37
|
-
def
|
38
|
-
|
62
|
+
def param_provided?(name)
|
63
|
+
_param(name).provided?
|
39
64
|
end
|
40
65
|
|
41
|
-
def
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
66
|
+
def self.process(param_values = {}, attribute_values = {})
|
67
|
+
action = new(param_values, attribute_values)
|
68
|
+
if action.process
|
69
|
+
return [action.result, nil]
|
70
|
+
else
|
71
|
+
return [nil, action.error]
|
72
|
+
end
|
48
73
|
end
|
49
74
|
|
50
|
-
def
|
51
|
-
|
52
|
-
cleanup_validators
|
75
|
+
def self.param_builders
|
76
|
+
@param_builders ||= {}
|
53
77
|
end
|
78
|
+
|
79
|
+
def self.param_builder(name)
|
80
|
+
name.to_sym
|
81
|
+
param_builders[name] ||= ParamBuilder.new(name)
|
82
|
+
end
|
83
|
+
|
84
|
+
def self.param(name, type = RawType, options = {})
|
85
|
+
param_builder(name).type_builder = TypeBuilder.new(type, options)
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.validates(name, validators = {})
|
89
|
+
validators.each do |validator, options|
|
90
|
+
param_builder(name).validator_builders << ValidatorBuilder.new(validator, options)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
def _param(name)
|
97
|
+
name = name.to_sym
|
98
|
+
unless self.class.param_builders[name]
|
99
|
+
raise ArgumentError, "Invalid param: #{name}"
|
100
|
+
end
|
101
|
+
_params[name]
|
102
|
+
end
|
103
|
+
|
104
|
+
def _params
|
105
|
+
@params ||= {}
|
106
|
+
end
|
107
|
+
|
54
108
|
end
|
55
109
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Reaction
|
2
|
+
class AttributeError < StandardError
|
3
|
+
attr_accessor :attribute
|
4
|
+
attr_accessor :message
|
5
|
+
|
6
|
+
def initialize(attribute, message = nil)
|
7
|
+
@attribute = attribute
|
8
|
+
@message = message || "Missing value for attribute: #{attribute}."
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
@@ -42,7 +42,11 @@ CODE
|
|
42
42
|
|
43
43
|
def validate_attributes
|
44
44
|
self.class.attributes.each do |attribute|
|
45
|
-
|
45
|
+
unless send(attribute)
|
46
|
+
# This is a server side error - we didn't set a valid attribute
|
47
|
+
# for some reason, and attributes are things the server sets.
|
48
|
+
failure(AttributeError.new(attribute)) and return false
|
49
|
+
end
|
46
50
|
end
|
47
51
|
end
|
48
52
|
|
data/lib/reaction/has_params.rb
CHANGED
@@ -1,5 +1,22 @@
|
|
1
1
|
module Reaction
|
2
2
|
module HasParams
|
3
|
+
|
4
|
+
def set_params(param_values)
|
5
|
+
param_values.each do |name, raw_value|
|
6
|
+
builder = self.class.param_builder(name)
|
7
|
+
builder.build(self, name, raw_value)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def process_params
|
12
|
+
params.each do |param|
|
13
|
+
return false unless param.process
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
3
20
|
def self.included(base)
|
4
21
|
base.include HasDocs
|
5
22
|
base.include HasMetas
|
@@ -10,13 +27,16 @@ module Reaction
|
|
10
27
|
|
11
28
|
module ClassMethods
|
12
29
|
def param(name, options = {})
|
13
|
-
set_type(name, options.delete(:type) || RawType)
|
14
|
-
set_validators(name, options)
|
30
|
+
set_type(name, options.delete(:type) || RawType, options)
|
15
31
|
end
|
16
32
|
|
17
33
|
def param_settable?(name)
|
18
34
|
!type(name).nil?
|
19
35
|
end
|
36
|
+
|
37
|
+
def validates(name, options = {})
|
38
|
+
set_validators(name, options)
|
39
|
+
end
|
20
40
|
end
|
21
41
|
|
22
42
|
def param_provided?(name)
|
@@ -26,7 +46,7 @@ module Reaction
|
|
26
46
|
def param(name)
|
27
47
|
typed_params[name.to_sym] ||= begin
|
28
48
|
type = self.class.types[name.to_sym]
|
29
|
-
type.convert(raw_param(name))
|
49
|
+
type.convert(self, name, raw_param(name))
|
30
50
|
end
|
31
51
|
end
|
32
52
|
|
@@ -72,6 +92,9 @@ module Reaction
|
|
72
92
|
def validate_params
|
73
93
|
raw_params.each do |name, value|
|
74
94
|
self.class.type(name).validate_each(self, name, value)
|
95
|
+
end
|
96
|
+
return if errors.any?
|
97
|
+
raw_params.each do |name, value|
|
75
98
|
converted = param(name)
|
76
99
|
self.class.validators(name).each do |validator|
|
77
100
|
validator.validate_each(self, name, converted)
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Reaction
|
2
|
+
class Param
|
3
|
+
attr_accessor :action
|
4
|
+
attr_accessor :type
|
5
|
+
attr_accessor :validators
|
6
|
+
attr_accessor :name
|
7
|
+
attr_accessor :raw_value
|
8
|
+
|
9
|
+
attr_accessor :successful
|
10
|
+
attr_accessor :result
|
11
|
+
attr_accessor :error
|
12
|
+
|
13
|
+
def initialize(action, type, validators, name, raw_value, provided = true)
|
14
|
+
@action = action
|
15
|
+
@type = type
|
16
|
+
@validators = validators
|
17
|
+
@name = name
|
18
|
+
@raw_value = raw_value
|
19
|
+
@provided = provided
|
20
|
+
end
|
21
|
+
|
22
|
+
def process
|
23
|
+
if type.process(raw_value)
|
24
|
+
validators.each do |validator|
|
25
|
+
unless validator.process(type.result)
|
26
|
+
failure(validator.error) and return false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
else
|
30
|
+
failure(type.error) and return false
|
31
|
+
end
|
32
|
+
success(type.result) and return true
|
33
|
+
end
|
34
|
+
|
35
|
+
def success(result)
|
36
|
+
@successful = true
|
37
|
+
@result = result
|
38
|
+
end
|
39
|
+
|
40
|
+
def failure(error)
|
41
|
+
@successful = false
|
42
|
+
@error = ParamError.new(name, error)
|
43
|
+
end
|
44
|
+
|
45
|
+
def provided?
|
46
|
+
!!@provided
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Reaction
|
2
|
+
class ParamBuilder
|
3
|
+
attr_accessor :name
|
4
|
+
attr_accessor :type_builder
|
5
|
+
attr_accessor :validator_builders
|
6
|
+
|
7
|
+
def initialize(name, type_builder = nil, validator_builders = [])
|
8
|
+
@name = name
|
9
|
+
@type_builder = type_builder
|
10
|
+
@validator_builders = validator_builders
|
11
|
+
end
|
12
|
+
|
13
|
+
def build(action, raw_value, provided = true)
|
14
|
+
type = type_builder.build(action)
|
15
|
+
validators = validator_builders.map{ |vb| vb.build(action) }
|
16
|
+
Param.new(action, type, validators, name, raw_value, provided)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/lib/reaction/type.rb
CHANGED
@@ -1,42 +1,44 @@
|
|
1
|
-
|
1
|
+
amodule Reaction
|
2
2
|
class Type
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
def
|
3
|
+
attr_accessor :action
|
4
|
+
attr_accessor :options
|
5
|
+
|
6
|
+
attr_accessor :result
|
7
|
+
attr_accessor :error
|
8
|
+
attr_accessor :successful
|
9
|
+
|
10
|
+
def initialize(action, options = {})
|
11
|
+
@action = action
|
12
|
+
@options = options
|
13
|
+
@successful = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def process(raw_value)
|
17
|
+
convert(raw_value)
|
18
|
+
raise "You forgot to call success or failure!!!" if @successful.nil?
|
19
|
+
@successful
|
20
|
+
end
|
21
|
+
|
22
|
+
def convert(raw_value)
|
23
|
+
raise NotImplementedError
|
24
|
+
end
|
25
|
+
|
26
|
+
def successful?
|
27
|
+
@successful == true
|
28
|
+
end
|
29
|
+
|
30
|
+
def success(result)
|
31
|
+
@successful = true
|
32
|
+
@result = result
|
33
|
+
end
|
34
|
+
|
35
|
+
def failed?
|
36
|
+
@successful == false
|
37
|
+
end
|
38
|
+
|
39
|
+
def failure(error)
|
40
|
+
@successful = false
|
41
|
+
@error = error
|
40
42
|
end
|
41
43
|
|
42
44
|
# This isn't perfect but works well enough.
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Reaction
|
2
|
+
class TypeBuilder
|
3
|
+
attr_accessor :type
|
4
|
+
attr_accessor :options
|
5
|
+
|
6
|
+
def initialize(type = RawType, options = {})
|
7
|
+
@type = class_for_type!(type)
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def build(action)
|
12
|
+
type.new(action, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def class_for_type(type)
|
16
|
+
return type if type.is_a?(Class)
|
17
|
+
name = type.to_s.split('_').map(&:capitalize).join
|
18
|
+
Module.const_get("#{name}Type")
|
19
|
+
rescue NameError
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def class_for_type!(type)
|
24
|
+
class_for_type(type) or raise ArgumentError.new("Unknown type: #{type}")
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Reaction
|
2
|
+
class Validator
|
3
|
+
attr_accessor :action
|
4
|
+
attr_accessor :options
|
5
|
+
|
6
|
+
attr_accessor :error
|
7
|
+
attr_accessor :successful
|
8
|
+
|
9
|
+
|
10
|
+
def initialize(action, options = {})
|
11
|
+
@action = action
|
12
|
+
@options = options
|
13
|
+
end
|
14
|
+
|
15
|
+
def process(value)
|
16
|
+
# Assume successful unless a failure for validators
|
17
|
+
@successful = true
|
18
|
+
|
19
|
+
validate(value)
|
20
|
+
!!@successful
|
21
|
+
end
|
22
|
+
|
23
|
+
def failure(error)
|
24
|
+
@successful = false
|
25
|
+
@error = error
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Reaction
|
2
|
+
class ValidatorBuilder
|
3
|
+
attr_accessor :validator
|
4
|
+
attr_accessor :options
|
5
|
+
|
6
|
+
def initialize(validator, options = {})
|
7
|
+
@validator = class_for_validator!(validator)
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def build(action)
|
12
|
+
validator.new(action, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def class_for_validator(validator)
|
16
|
+
return validator if validator.is_a?(Class)
|
17
|
+
name = validator.to_s.split('_').map(&:capitalize).join
|
18
|
+
Module.const_get("#{name}Validator")
|
19
|
+
rescue NameError
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def class_for_validator!(validator)
|
24
|
+
class_for_validator(validator) or raise ArgumentError.new("Unknown validator: #{validator}")
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
data/lib/reaction.rb
CHANGED
@@ -2,18 +2,14 @@ require 'redcarpet'
|
|
2
2
|
|
3
3
|
module Reaction
|
4
4
|
autoload :Action, 'reaction/action'
|
5
|
-
autoload :Doc, 'reaction/doc'
|
6
|
-
autoload :EachValidator, 'reaction/each_validator'
|
7
|
-
autoload :Errors, 'reaction/errors'
|
8
5
|
autoload :HasAttributes, 'reaction/has_attributes'
|
9
|
-
autoload :
|
10
|
-
autoload :
|
11
|
-
autoload :HasMetas, 'reaction/has_metas'
|
12
|
-
autoload :HasParams, 'reaction/has_params'
|
13
|
-
autoload :HasTypes, 'reaction/has_types'
|
14
|
-
autoload :HasValidators, 'reaction/has_validators'
|
15
|
-
autoload :IsDocumented, 'reaction/is_documented'
|
16
|
-
autoload :Markdown, 'reaction/markdown'
|
17
|
-
autoload :RawType, 'reaction/types/raw_type'
|
6
|
+
autoload :Param, 'reaction/param'
|
7
|
+
autoload :ParamBuilder, 'reaction/param_builder'
|
18
8
|
autoload :Type, 'reaction/type'
|
9
|
+
autoload :TypeBuilder, 'reaction/type_builder'
|
10
|
+
autoload :Validator, 'reaction/validator'
|
11
|
+
autoload :ValidatorBuilder, 'reaction/validator_builder'
|
12
|
+
autoload :AttributeError, 'reaction/errors/attribute_error'
|
13
|
+
autoload :ParamError, 'reaction/errors/param_error'
|
14
|
+
autoload :RawType, 'reaction/types/raw_type'
|
19
15
|
end
|
data/reaction.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |gem|
|
6
6
|
gem.name = 'reaction'
|
7
|
-
gem.version = '0.
|
7
|
+
gem.version = '1.0.0'
|
8
8
|
gem.authors = ["Jon Calhoun", "Jon Calhoun", "Ryan Jackson"]
|
9
9
|
gem.email = ["joncalhoun@gmail.com", "jon@paidlabs.com", "ryan@paidlabs.com"]
|
10
10
|
gem.description = 'Reaction makes it easy to build reusable controller actions along with reusable validators and param type converters.'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reaction
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Calhoun
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2016-
|
13
|
+
date: 2016-05-10 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: redcarpet
|
@@ -41,20 +41,17 @@ files:
|
|
41
41
|
- LICENSE.txt
|
42
42
|
- lib/reaction.rb
|
43
43
|
- lib/reaction/action.rb
|
44
|
-
- lib/reaction/
|
45
|
-
- lib/reaction/
|
46
|
-
- lib/reaction/errors.rb
|
44
|
+
- lib/reaction/errors/attribute_error.rb
|
45
|
+
- lib/reaction/errors/param_error.rb
|
47
46
|
- lib/reaction/has_attributes.rb
|
48
|
-
- lib/reaction/has_docs.rb
|
49
|
-
- lib/reaction/has_errors.rb
|
50
|
-
- lib/reaction/has_metas.rb
|
51
47
|
- lib/reaction/has_params.rb
|
52
|
-
- lib/reaction/
|
53
|
-
- lib/reaction/
|
54
|
-
- lib/reaction/is_documented.rb
|
55
|
-
- lib/reaction/markdown.rb
|
48
|
+
- lib/reaction/param.rb
|
49
|
+
- lib/reaction/param_builder.rb
|
56
50
|
- lib/reaction/type.rb
|
51
|
+
- lib/reaction/type_builder.rb
|
57
52
|
- lib/reaction/types/raw_type.rb
|
53
|
+
- lib/reaction/validator.rb
|
54
|
+
- lib/reaction/validator_builder.rb
|
58
55
|
- reaction.gemspec
|
59
56
|
homepage: https://github.com/paidlabs/reaction
|
60
57
|
licenses: []
|
data/lib/reaction/doc.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
# This class serves no real purpose in this gem, but is here so that
|
2
|
-
# developers can use it to create more dynamic documentation by
|
3
|
-
# documenting attributes used for actions directly in the actions,
|
4
|
-
# and then access these when generating their actual documenation.
|
5
|
-
#
|
6
|
-
module Reaction
|
7
|
-
class Doc
|
8
|
-
attr_reader :text, :options
|
9
|
-
|
10
|
-
def initialize(text, options = {})
|
11
|
-
@text = text
|
12
|
-
@options = options
|
13
|
-
end
|
14
|
-
|
15
|
-
def to_html(formatter = Markdown)
|
16
|
-
formatter.render(text)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
module Reaction
|
2
|
-
class EachValidator
|
3
|
-
attr_reader :options
|
4
|
-
|
5
|
-
def initialize(options = {})
|
6
|
-
@options = options
|
7
|
-
end
|
8
|
-
|
9
|
-
# If you need to validate a parameter outside of the basic
|
10
|
-
# type then validators are your best bet. They are generally
|
11
|
-
# very similar to Rails' EachValidator. You provide a
|
12
|
-
# validate_each method that takes in a record, attribute, and
|
13
|
-
# value and then if the attribute isn't valid adds an error
|
14
|
-
# to record.errors. You can also access the data provided
|
15
|
-
# for the validator via the +options+ method.
|
16
|
-
#
|
17
|
-
# Example validate_each method for a RequiredValidator which
|
18
|
-
# would be used like:
|
19
|
-
#
|
20
|
-
# param :cat, required: true
|
21
|
-
# param :dog, required: false
|
22
|
-
#
|
23
|
-
#
|
24
|
-
# def validate_each(record, attribute, value)
|
25
|
-
# if options && value.nil?
|
26
|
-
# record.errors.add(attribute, 'is required.')
|
27
|
-
# end
|
28
|
-
# end
|
29
|
-
#
|
30
|
-
def validate_each(record, attribute, value)
|
31
|
-
raise NotImplementedError.new('Subclasses must implement a validate_each(record, attribute, value) method')
|
32
|
-
end
|
33
|
-
|
34
|
-
# Cleanup is provided in case you need to create files that
|
35
|
-
# require cleanup after the action has been performed. For
|
36
|
-
# example, Paid creates Tempfiles with some types and uses
|
37
|
-
# the cleanup phase to ensure these files get closed up
|
38
|
-
# properly.
|
39
|
-
#
|
40
|
-
def cleanup
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
data/lib/reaction/errors.rb
DELETED
@@ -1,72 +0,0 @@
|
|
1
|
-
module Reaction
|
2
|
-
class Errors
|
3
|
-
|
4
|
-
attr_accessor :messages
|
5
|
-
|
6
|
-
def initialize
|
7
|
-
@messages = {}
|
8
|
-
end
|
9
|
-
|
10
|
-
def clear
|
11
|
-
messages.clear
|
12
|
-
end
|
13
|
-
|
14
|
-
def include?(key)
|
15
|
-
!messages[key].nil?
|
16
|
-
end
|
17
|
-
|
18
|
-
def get(key)
|
19
|
-
messages[key]
|
20
|
-
end
|
21
|
-
|
22
|
-
def add(key, message, options = {})
|
23
|
-
messages[key] ||= []
|
24
|
-
messages[key] << message
|
25
|
-
end
|
26
|
-
|
27
|
-
def any?
|
28
|
-
messages.any?
|
29
|
-
end
|
30
|
-
|
31
|
-
def empty?
|
32
|
-
messages.empty?
|
33
|
-
end
|
34
|
-
|
35
|
-
def size
|
36
|
-
messages.size
|
37
|
-
end
|
38
|
-
|
39
|
-
def length
|
40
|
-
messages.length
|
41
|
-
end
|
42
|
-
|
43
|
-
def first
|
44
|
-
messages.first
|
45
|
-
end
|
46
|
-
|
47
|
-
def each
|
48
|
-
messages
|
49
|
-
end
|
50
|
-
|
51
|
-
def full_messages
|
52
|
-
ret = []
|
53
|
-
messages.each do |key, values|
|
54
|
-
ret |= full_messages_for(key, values)
|
55
|
-
end
|
56
|
-
ret
|
57
|
-
end
|
58
|
-
|
59
|
-
def full_messages_for(key, values = nil)
|
60
|
-
ret = []
|
61
|
-
values ||= messages[key]
|
62
|
-
values.each do |value|
|
63
|
-
if key != :base
|
64
|
-
ret << "Invalid value for param: #{key}. #{value}"
|
65
|
-
else
|
66
|
-
ret << value
|
67
|
-
end
|
68
|
-
end
|
69
|
-
ret
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
data/lib/reaction/has_docs.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
module Reaction
|
2
|
-
module HasDocs
|
3
|
-
def self.included(base)
|
4
|
-
base.extend ClassMethods
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
def doc(name, message, options = {})
|
9
|
-
docs[name.to_sym] = Doc.new(message, options)
|
10
|
-
end
|
11
|
-
|
12
|
-
def docs
|
13
|
-
@docs ||= {}
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def doc(name)
|
18
|
-
self.class.docs[name.to_sym]
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
data/lib/reaction/has_errors.rb
DELETED
data/lib/reaction/has_metas.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
module Reaction
|
2
|
-
module HasMetas
|
3
|
-
def self.included(base)
|
4
|
-
base.extend ClassMethods
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
end
|
9
|
-
|
10
|
-
def meta(name)
|
11
|
-
metas[name.to_sym]
|
12
|
-
end
|
13
|
-
|
14
|
-
def metas
|
15
|
-
@metas ||= {}
|
16
|
-
end
|
17
|
-
|
18
|
-
def rm_meta(name)
|
19
|
-
metas.delete(name.to_sym)
|
20
|
-
end
|
21
|
-
|
22
|
-
def set_meta(name, meta)
|
23
|
-
metas[name.to_sym] = meta
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
end
|
data/lib/reaction/has_types.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
module Reaction
|
2
|
-
module HasTypes
|
3
|
-
def self.included(base)
|
4
|
-
base.extend ClassMethods
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
def type(name)
|
9
|
-
types[name.to_sym]
|
10
|
-
end
|
11
|
-
|
12
|
-
def types
|
13
|
-
@types ||= {}
|
14
|
-
end
|
15
|
-
|
16
|
-
def set_type(name, type)
|
17
|
-
type ||= Type
|
18
|
-
klass = class_for_type!(type)
|
19
|
-
types[name.to_sym] = klass.new(name)
|
20
|
-
end
|
21
|
-
|
22
|
-
def class_for_type(type)
|
23
|
-
return type if type.is_a?(Class)
|
24
|
-
name = type.to_s.split('_').map(&:capitalize).join
|
25
|
-
const_get("#{name}Type")
|
26
|
-
rescue NameError
|
27
|
-
nil
|
28
|
-
end
|
29
|
-
|
30
|
-
def class_for_type!(type)
|
31
|
-
class_for_type(type) or raise ArgumentError.new("Unknown type: #{type}")
|
32
|
-
end
|
33
|
-
|
34
|
-
def cleanup_types
|
35
|
-
types.each do |name, type|
|
36
|
-
type.cleanup
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def cleanup_types
|
42
|
-
self.class.cleanup_types
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
module Reaction
|
2
|
-
module HasValidators
|
3
|
-
def self.included(base)
|
4
|
-
base.extend ClassMethods
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
def all_validators
|
9
|
-
@all_validators ||= {}
|
10
|
-
end
|
11
|
-
|
12
|
-
def validators(name)
|
13
|
-
all_validators[name.to_sym] ||= []
|
14
|
-
all_validators[name.to_sym]
|
15
|
-
end
|
16
|
-
|
17
|
-
def add_validator(name, validator, options = {})
|
18
|
-
klass = class_for_validator(validator)
|
19
|
-
validators(name) << klass.new(options)
|
20
|
-
end
|
21
|
-
|
22
|
-
def set_validators(name, new_validators = {})
|
23
|
-
validators(name).clear
|
24
|
-
new_validators.each do |validator, options|
|
25
|
-
add_validator(name, validator, options)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def class_for_validator(validator)
|
30
|
-
return validator if validator.is_a?(Class)
|
31
|
-
name = validator.to_s.split('_').map(&:capitalize).join
|
32
|
-
const_get("#{name}Validator")
|
33
|
-
rescue NameError
|
34
|
-
raise ArgumentError.new("Unknown param validator: #{validator}")
|
35
|
-
end
|
36
|
-
|
37
|
-
def cleanup_validators
|
38
|
-
all_validators.each do |name, validators|
|
39
|
-
validators.each do |validator|
|
40
|
-
validator.cleanup
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def cleanup_validators
|
47
|
-
self.class.cleanup_validators
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
51
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Reaction
|
2
|
-
module IsDocumented
|
3
|
-
def self.included(base)
|
4
|
-
base.extend ClassMethods
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
def doc(message = nil, options = {})
|
9
|
-
if message
|
10
|
-
@doc = Doc.new(message, options)
|
11
|
-
else
|
12
|
-
@doc
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
data/lib/reaction/markdown.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
module Reaction
|
2
|
-
class Markdown
|
3
|
-
def self.render(md_str)
|
4
|
-
markdown.render(md_str)
|
5
|
-
end
|
6
|
-
|
7
|
-
def self.renderer
|
8
|
-
@renderer ||= Redcarpet::Render::HTML.new
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.markdown
|
12
|
-
@markdown ||= Redcarpet::Markdown.new(renderer, autolink: true, tables: true)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|