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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a083875b2fb61b1944e5704f33fd94a347bd523
4
- data.tar.gz: b1add1dbb49c659847f805f95c44ca7d8189fec9
3
+ metadata.gz: 988b21cec0c58acf861ccabe5a11d7ba8e0bfdb5
4
+ data.tar.gz: 4333f04bdd5654cf30e149b24fb4111ba22091c6
5
5
  SHA512:
6
- metadata.gz: a7648be8b693217a28315d652c0845bf80d8e3ba2cffef97a8a965ccd15e5a1bf3d4fd7fe2209ef6bff706445d92837c95c78ad5d7347afa8475f1bff1e130d7
7
- data.tar.gz: 9f80495f4305776e8d0009cff7a44730da078540531a65c1282fbd1e24a1054f8ce2979cbaf6640ccd251f4da277b2db802a475a73d2e2d219678769b0ae9c2e
6
+ metadata.gz: 52a815000393b6985df7faed9533775183ee01372605490189696585bd63d012104adecb08618cae4d73128c0414d1505c839c43daca8e10761f1240e673c0ab
7
+ data.tar.gz: 5bb22ce6053954669c76d0e28ba323d622fbfa1330b136089ecb3e8ba5d37974d8d7855a3330e5f4f07680537efebd13cf8cd03c9a9a51d5d1fec27e78c9d657
@@ -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
- # meta isn't used by default, but it is supported for use cases
8
- # where you need to attach metadata to parameters, such as
9
- # who or what set the attribute. For example, Paid currently
10
- # has a flow like:
11
- #
12
- # action = Action.new(request.params, via: :request)
13
- # action.set_params(account: api_key.account, via: :api_key)
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 invoke
26
- validate
27
- return self if errors.any?
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
- ensure
30
- cleanup
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 self.invoke(params = {}, meta = {}, attributes = {})
34
- new(params, meta, attributes).invoke
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 perform
38
- # Implement this for your action
62
+ def param_provided?(name)
63
+ _param(name).provided?
39
64
  end
40
65
 
41
- def validate
42
- errors.clear
43
- validate_attributes
44
- validate_params
45
- # if errors.any?
46
- # raise ArgumentError.new("Validations failed: #{errors.full_messages.join(',')}")
47
- # end
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 cleanup
51
- cleanup_types
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
@@ -0,0 +1,12 @@
1
+ module Reaction
2
+ class ParamError < StandardError
3
+ attr_accessor :param
4
+ attr_accessor :message
5
+
6
+ def initialize(param, message = nil)
7
+ @param = param
8
+ @message = message || "Invalid value for param: #{param}."
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
- errors.add(attribute, 'is not set.') unless send(attribute)
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
 
@@ -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
- module Reaction
1
+ amodule Reaction
2
2
  class Type
3
- include IsDocumented
4
-
5
- attr_reader :name
6
-
7
- def initialize(name = nil)
8
- @name = name.nil? ? nil : name.to_sym
9
- end
10
-
11
- # If you need to validate based on type you can. These
12
- # work identically to the validators, except type validations
13
- # are always called before other validators, and the +options+
14
- # hash isn't available. If you have a particularly good
15
- # use case for passing in options to a Type reach out; It
16
- # should be pretty easy to add in, we just haven't had a
17
- # need for it yet.
18
- #
19
- def validate_each(record, attribute, value)
20
- end
21
-
22
- # Convert is used to transform a value into whatever
23
- # format you expect it to be. For example, you might
24
- # have a convert method that casts a string into an
25
- # integer, or one that takes in various date formats
26
- # and converts them to a DateTime prior to the param
27
- # being used in the action.
28
- #
29
- def convert(value)
30
- value
31
- end
32
-
33
- # Cleanup is provided in case you need to create files that
34
- # require cleanup after the action has been performed. For
35
- # example, Paid creates Tempfiles with some types and uses
36
- # the cleanup phase to ensure these files get closed up
37
- # properly.
38
- #
39
- def cleanup
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
@@ -3,14 +3,10 @@
3
3
  #
4
4
  module Reaction
5
5
  class RawType < Type
6
- def validate_each(record, attribute, value)
7
- end
8
6
 
9
- def convert(value)
10
- value
7
+ def convert(raw_value)
8
+ success(raw_value)
11
9
  end
12
10
 
13
- def cleanup
14
- end
15
11
  end
16
12
  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 :HasDocs, 'reaction/has_docs'
10
- autoload :HasErrors, 'reaction/has_errors'
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.2.8'
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.2.8
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-04-21 00:00:00.000000000 Z
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/doc.rb
45
- - lib/reaction/each_validator.rb
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/has_types.rb
53
- - lib/reaction/has_validators.rb
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
@@ -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
@@ -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
@@ -1,15 +0,0 @@
1
- module Reaction
2
- module HasErrors
3
- def self.included(base)
4
- base.extend ClassMethods
5
- end
6
-
7
- module ClassMethods
8
- end
9
-
10
- def errors
11
- @errors ||= Errors.new
12
- end
13
-
14
- end
15
- end
@@ -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
@@ -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
@@ -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