Class: Reaction::Type
- Inherits:
-
Object
- Object
- Reaction::Type
- Defined in:
- lib/reaction/type.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#error ⇒ Object
Returns the value of attribute error.
-
#options ⇒ Object
Returns the value of attribute options.
-
#result ⇒ Object
Returns the value of attribute result.
-
#successful ⇒ Object
Returns the value of attribute successful.
Class Method Summary collapse
-
.to_type_symbol ⇒ Object
This isn’t perfect but works well enough.
Instance Method Summary collapse
- #convert(raw_value) ⇒ Object
- #failed? ⇒ Boolean
- #failure(error) ⇒ Object
-
#initialize(action, options = {}) ⇒ Type
constructor
A new instance of Type.
- #process(raw_value) ⇒ Object
- #process_subtype(raw_value, type_sym, options = {}, error = nil) ⇒ Object
- #success(result) ⇒ Object
- #successful? ⇒ Boolean
- #validate_with(validator, options, value) ⇒ Object
Constructor Details
#initialize(action, options = {}) ⇒ Type
Returns a new instance of Type.
10 11 12 13 14 |
# File 'lib/reaction/type.rb', line 10 def initialize(action, = {}) @action = action @options = @successful = nil end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
3 4 5 |
# File 'lib/reaction/type.rb', line 3 def action @action end |
#error ⇒ Object
Returns the value of attribute error.
7 8 9 |
# File 'lib/reaction/type.rb', line 7 def error @error end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/reaction/type.rb', line 4 def @options end |
#result ⇒ Object
Returns the value of attribute result.
6 7 8 |
# File 'lib/reaction/type.rb', line 6 def result @result end |
#successful ⇒ Object
Returns the value of attribute successful.
8 9 10 |
# File 'lib/reaction/type.rb', line 8 def successful @successful end |
Class Method Details
.to_type_symbol ⇒ Object
This isn’t perfect but works well enough.
65 66 67 68 69 70 71 72 |
# File 'lib/reaction/type.rb', line 65 def self.to_type_symbol ret = self.to_s ret.gsub!(/([A-Z]+)([A-Z])/, '\1_\2') ret.gsub!(/([a-z])([A-Z])/, '\1_\2') ret.downcase! ret.gsub!(/_type$/, '') ret.to_sym end |
Instance Method Details
#convert(raw_value) ⇒ Object
22 23 24 |
# File 'lib/reaction/type.rb', line 22 def convert(raw_value) raise NotImplementedError end |
#failed? ⇒ Boolean
35 36 37 |
# File 'lib/reaction/type.rb', line 35 def failed? @successful == false end |
#failure(error) ⇒ Object
39 40 41 42 |
# File 'lib/reaction/type.rb', line 39 def failure(error) @error = error @successful = false end |
#process(raw_value) ⇒ Object
16 17 18 19 20 |
# File 'lib/reaction/type.rb', line 16 def process(raw_value) convert(raw_value) raise "You forgot to call success or failure!!!" if @successful.nil? @successful end |
#process_subtype(raw_value, type_sym, options = {}, error = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/reaction/type.rb', line 54 def process_subtype(raw_value, type_sym, = {}, error = nil) type = Reaction::TypeBuilder.new(type_sym, ).build(action) if !type.process(raw_value) failure(error || type.error) nil else type.result end end |
#success(result) ⇒ Object
30 31 32 33 |
# File 'lib/reaction/type.rb', line 30 def success(result) @result = result @successful = true end |
#successful? ⇒ Boolean
26 27 28 |
# File 'lib/reaction/type.rb', line 26 def successful? @successful == true end |
#validate_with(validator, options, value) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/reaction/type.rb', line 44 def validate_with(validator, , value) v = ValidatorBuilder.new(validator, ).build(action) v.process(value) if v.failed? failure(v.error) return false end true end |