Class: Reaction::Action

Inherits:
Object
  • Object
show all
Includes:
HasAttributes
Defined in:
lib/reaction/action.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasAttributes

#attributes, included, #set_attributes, #validate_attributes

Constructor Details

#initialize(param_values = {}, attribute_values = {}) ⇒ Action

Returns a new instance of Action.



9
10
11
12
# File 'lib/reaction/action.rb', line 9

def initialize(param_values = {}, attribute_values = {})
  set_params(param_values)
  set_attributes(attribute_values)
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



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

def error
  @error
end

#resultObject

Returns the value of attribute result.



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

def result
  @result
end

#successfulObject

Returns the value of attribute successful.



5
6
7
# File 'lib/reaction/action.rb', line 5

def successful
  @successful
end

Class Method Details

.param(name, type = RawType, options = {}) ⇒ Object



95
96
97
# File 'lib/reaction/action.rb', line 95

def self.param(name, type = RawType, options = {})
  param_builder(name).type_builder = TypeBuilder.new(type, options)
end

.param_builder(name) ⇒ Object



90
91
92
93
# File 'lib/reaction/action.rb', line 90

def self.param_builder(name)
  name.to_sym
  param_builders[name] ||= ParamBuilder.new(name)
end

.param_buildersObject



86
87
88
# File 'lib/reaction/action.rb', line 86

def self.param_builders
  @param_builders ||= {}
end

.process(param_values = {}, attribute_values = {}) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/reaction/action.rb', line 77

def self.process(param_values = {}, attribute_values = {})
  action = new(param_values, attribute_values)
  if action.process
    return [action.result, nil]
  else
    return [nil, action.error]
  end
end

.validates(name, validators = {}) ⇒ Object



99
100
101
102
103
# File 'lib/reaction/action.rb', line 99

def self.validates(name, validators = {})
  validators.each do |validator, options|
    param_builder(name).validator_builders << ValidatorBuilder.new(validator, options)
  end
end

Instance Method Details

#failed?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/reaction/action.rb', line 36

def failed?
  @successful == false
end

#failure(error) ⇒ Object



31
32
33
34
# File 'lib/reaction/action.rb', line 31

def failure(error)
  @error = error
  @successful = false
end

#param(name, mode = :converted) ⇒ Object

This is how you retrieve a param. Pass in a mode of :raw in order to get the raw param if you happen to need that, but in general you shouldn’t.



44
45
46
47
48
49
50
# File 'lib/reaction/action.rb', line 44

def param(name, mode = :converted)
  if mode == :raw
    _param(name).raw_value
  else
    _param(name).result
  end
end

#param_provided?(name) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/reaction/action.rb', line 73

def param_provided?(name)
  _param(name).provided?
end

#params(mode = :converted) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/reaction/action.rb', line 52

def params(mode = :converted)
  ret = {}
  _params.each do |name, param|
    if mode == :raw
      ret[name] = param.raw_value if param.provided?
    else
      ret[name] = param.result if param.provided?
    end
  end
  ret
end

#processObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/reaction/action.rb', line 14

def process
  return false unless validate_attributes
  _params.each do |name, param|
    unless param.process
      failure(param.error)
      return false
    end
  end
  perform
  return !!@successful
end

#set_params(param_values = {}) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/reaction/action.rb', line 64

def set_params(param_values = {})
  provided_keys = param_values.keys.map(&:to_sym)

  self.class.param_builders.each do |name, pb|
    provided = provided_keys.include?(name)
    _params[name] = pb.build(self, param_values[name], provided)
  end
end

#success(result) ⇒ Object



26
27
28
29
# File 'lib/reaction/action.rb', line 26

def success(result)
  @result = result
  @successful = true
end