reaction 0.2.0 → 0.2.4

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: aca4aad215915e667e9c711483c2cfc9ff476868
4
- data.tar.gz: d54fd1353ef63abad3c25aae23e09031f111beae
3
+ metadata.gz: 9d44ed5a4d16563ce2b661c870d73950cd4bb81f
4
+ data.tar.gz: 22851e1ee79194a12e563da426aacc1b7c3ecf91
5
5
  SHA512:
6
- metadata.gz: 69b6a817db4c326691a171514987a0d4bd98b12a568d51c771db34f7eec84eaa917d47f8a653252c2191cc514d15b3fea5d9fff075099c14d546f3298056a744
7
- data.tar.gz: 26262301db8e8898ce28e399019cb11b6b083b8ebb7ee51ed6c97a7e68b1a06e358cd77e7df3fc3e1dfc688e59ab3d514cc21b43b98cd32e22b1fe1e0c8e6475
6
+ metadata.gz: eba75f96b595f8c5844a92aa6ffebb0eaff6d9045690570dd41d2b904a2af124ecd5fcd29c6593b573c2f2f654b9cfa8edb3af47b26229fb3ddb1c7354ceee4c
7
+ data.tar.gz: 32d58d640be2c370fc5b6f9b37711e397ec2594311ea557fa374e3d79377d9e595d75861299dd280b4767d3ad5e5f8f48cb8307a5e3bf88225affc22f7a49054
@@ -1,5 +1,6 @@
1
1
  module Reaction
2
2
  class Action
3
+ include HasAttributes
3
4
  include HasErrors
4
5
  include HasParams
5
6
 
@@ -32,29 +33,17 @@ module Reaction
32
33
  end
33
34
 
34
35
  def validate!
35
- params.each do |name, value|
36
- unless self.class.class_for_type(name)
37
- errors.add(name, 'is not a valid parameter.')
38
- end
39
- end
40
- self.class.types.each do |name, type|
41
- type.validate_each(self, name, raw_param(name))
42
- end
43
- self.class.validators.each do |name, validator|
44
- validator.validate_each(self, name, raw_param(name))
45
- end
36
+ errors.clear
37
+ validate_attributes
38
+ validate_params
46
39
  if errors.any?
47
40
  raise ArgumentError.new("Validations failed: #{errors.full_messages.join(',')}")
48
41
  end
49
42
  end
50
43
 
51
44
  def cleanup
52
- self.class.types.each do |name, type|
53
- type.cleanup
54
- end
55
- self.class.validators.each do |name, validator|
56
- validator.cleanup
57
- end
45
+ cleanup_types
46
+ cleanup_validators
58
47
  end
59
48
  end
60
49
  end
@@ -0,0 +1,33 @@
1
+ module Reaction
2
+ module HasAttributes
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def attribute(name)
9
+ attr_accessor name
10
+ attributes << name
11
+ end
12
+
13
+ def attributes
14
+ @attributes ||= []
15
+ end
16
+ end
17
+
18
+ def set_attributes(attributes = {})
19
+ attributes.each do |key, value|
20
+ if self.class.attributes.include?(key)
21
+ send("#{key}=", value)
22
+ end
23
+ end
24
+ end
25
+
26
+ def validate_attributes
27
+ self.class.attributes.each do |attribute|
28
+ errors.add(attribute, 'is not set.') unless send(attribute)
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -13,17 +13,25 @@ module Reaction
13
13
  set_type(name, options.delete(:type) || RawType)
14
14
  set_validators(name, options)
15
15
  end
16
+
17
+ def param_settable?(name)
18
+ !type(name).nil?
19
+ end
16
20
  end
17
21
 
18
22
  def param(name)
19
23
  typed_params[name.to_sym] ||= begin
20
24
  type = self.class.types[name.to_sym]
21
- type ? type.convert(raw_param(name)) : raw_param(name)
25
+ type.convert(raw_param(name))
22
26
  end
23
27
  end
24
28
 
25
29
  def params
26
- @params ||= {}
30
+ ret = {}
31
+ raw_params.keys.each do |name|
32
+ ret[name.to_sym] = param(name)
33
+ end
34
+ ret
27
35
  end
28
36
 
29
37
  def typed_params
@@ -31,8 +39,9 @@ module Reaction
31
39
  end
32
40
 
33
41
  def set_param(name, value, meta = {})
42
+ return unless self.class.param_settable?(name)
34
43
  set_meta(name, meta)
35
- params[name.to_sym] = value
44
+ raw_params[name.to_sym] = value
36
45
  end
37
46
 
38
47
  def set_params(params = {}, meta = {})
@@ -42,7 +51,20 @@ module Reaction
42
51
  end
43
52
 
44
53
  def raw_param(name)
45
- params[name.to_sym]
54
+ raw_params[name.to_sym]
55
+ end
56
+
57
+ def raw_params
58
+ @raw_params ||= {}
59
+ end
60
+
61
+ def validate_params
62
+ raw_params.each do |name, value|
63
+ self.class.type(name).validate_each(self, name, value)
64
+ self.class.validators(name).each do |validator|
65
+ validator.validate_each(self, name, value)
66
+ end
67
+ end
46
68
  end
47
69
 
48
70
  end
@@ -5,13 +5,17 @@ module Reaction
5
5
  end
6
6
 
7
7
  module ClassMethods
8
+ def type(name)
9
+ types[name.to_sym]
10
+ end
11
+
8
12
  def types
9
13
  @types ||= {}
10
14
  end
11
15
 
12
16
  def set_type(name, type)
13
17
  type ||= Type
14
- klass = class_for_type(type)
18
+ klass = class_for_type!(type)
15
19
  types[name.to_sym] = klass.new(name)
16
20
  end
17
21
 
@@ -22,6 +26,20 @@ module Reaction
22
26
  rescue NameError
23
27
  nil
24
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
25
43
  end
26
44
 
27
45
  end
@@ -5,18 +5,24 @@ module Reaction
5
5
  end
6
6
 
7
7
  module ClassMethods
8
- def validators
9
- @validators ||= {}
8
+ def all_validators
9
+ @all_validators ||= {}
10
10
  end
11
11
 
12
- def set_validator(name, validator, options = {})
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 = {})
13
18
  klass = class_for_validator(validator)
14
- validators[name.to_sym] = klass.new(options)
19
+ validators(name) << klass.new(options)
15
20
  end
16
21
 
17
- def set_validators(name, validators = {})
18
- validators.each do |validator, options|
19
- set_validator(name, validator, options)
22
+ def set_validators(name, new_validators = {})
23
+ validators(name).clear
24
+ new_validators.each do |validator, options|
25
+ add_validator(name, validator, options)
20
26
  end
21
27
  end
22
28
 
@@ -27,6 +33,18 @@ module Reaction
27
33
  rescue NameError
28
34
  raise ArgumentError.new("Unknown param validator: #{validator}")
29
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
30
48
  end
31
49
 
32
50
  end
data/lib/reaction.rb CHANGED
@@ -3,6 +3,7 @@ module Reaction
3
3
  autoload :Doc, 'reaction/doc'
4
4
  autoload :EachValidator, 'reaction/each_validator'
5
5
  autoload :Errors, 'reaction/errors'
6
+ autoload :HasAttributes, 'reaction/has_attributes'
6
7
  autoload :HasDocs, 'reaction/has_docs'
7
8
  autoload :HasErrors, 'reaction/has_errors'
8
9
  autoload :HasMetas, 'reaction/has_metas'
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.0'
7
+ gem.version = '0.2.4'
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.0
4
+ version: 0.2.4
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-03-02 00:00:00.000000000 Z
13
+ date: 2016-03-07 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Reaction makes it easy to build reusable controller actions along with
16
16
  reusable validators and param type converters.
@@ -30,6 +30,7 @@ files:
30
30
  - lib/reaction/doc.rb
31
31
  - lib/reaction/each_validator.rb
32
32
  - lib/reaction/errors.rb
33
+ - lib/reaction/has_attributes.rb
33
34
  - lib/reaction/has_docs.rb
34
35
  - lib/reaction/has_errors.rb
35
36
  - lib/reaction/has_metas.rb