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



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

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

.param_builder(name) ⇒ Object



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

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

.param_buildersObject



81
82
83
# File 'lib/reaction/action.rb', line 81

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

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



72
73
74
75
76
77
78
79
# File 'lib/reaction/action.rb', line 72

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



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

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

Instance Method Details

#failure(error) ⇒ Object



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

def failure(error)
  @successful = false
  @error = error
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.



39
40
41
42
43
44
45
# File 'lib/reaction/action.rb', line 39

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

#param_provided?(name) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/reaction/action.rb', line 68

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

#params(mode = :converted) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/reaction/action.rb', line 47

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
# 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) and return false
    end
  end
  perform
  return !!@successful
end

#set_params(param_values = {}) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/reaction/action.rb', line 59

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



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

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