Class: Reaction::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/reaction/type.rb

Direct Known Subclasses

RawType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @action = action
  @options = options
  @successful = nil
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



3
4
5
# File 'lib/reaction/type.rb', line 3

def action
  @action
end

#errorObject

Returns the value of attribute error.



7
8
9
# File 'lib/reaction/type.rb', line 7

def error
  @error
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/reaction/type.rb', line 4

def options
  @options
end

#resultObject

Returns the value of attribute result.



6
7
8
# File 'lib/reaction/type.rb', line 6

def result
  @result
end

#successfulObject

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_symbolObject

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

Raises:

  • (NotImplementedError)


22
23
24
# File 'lib/reaction/type.rb', line 22

def convert(raw_value)
  raise NotImplementedError
end

#failed?Boolean

Returns:

  • (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, options = {}, error = nil)
  type = Reaction::TypeBuilder.new(type_sym, options).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

Returns:

  • (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, options, value)
  v = ValidatorBuilder.new(validator, options).build(action)
  v.process(value)
  if v.failed?
    failure(v.error)
    return false
  end
  true
end