Class: SuperProcess::Core

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, ActiveSupport::Callbacks
Defined in:
lib/super_process/core.rb

Constant Summary collapse

ValidError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.after_call(method_name) ⇒ Object



16
17
18
# File 'lib/super_process/core.rb', line 16

def self.after_call(method_name)
  set_callback :validations, :after, method_name
end

.after_task(method_name) ⇒ Object



24
25
26
# File 'lib/super_process/core.rb', line 24

def self.after_task(method_name)
  set_callback :task, :after, method_name
end

.before_call(method_name) ⇒ Object



12
13
14
# File 'lib/super_process/core.rb', line 12

def self.before_call(method_name)
  set_callback :validations, :before, method_name
end

.before_task(method_name) ⇒ Object



20
21
22
# File 'lib/super_process/core.rb', line 20

def self.before_task(method_name)
  set_callback :task, :before, method_name
end

.callable(&block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/super_process/core.rb', line 38

def self.callable(&block)
  define_method :call! do |params={}|
    params.each do |attr, value|
      public_send("#{attr}=", value) if respond_to?("#{attr}=")
    end
    run_callbacks :validations do
      raise ValidError, "Validation Error" if valid? == false

      run_callbacks :task do
        instance_eval(&block)
      end
    end
  end

  define_method :call do |params={}|
    begin
      @result = call!(params)
      true
    rescue ValidError
      false
    end
  end
end

.init(model_name, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/super_process/core.rb', line 28

def self.init(model_name, &block)
  attr_accessor model_name

  class_eval(&block) if block_given?

  define_method :initialize do |model|
    self.send("#{model_name}=", model)
  end
end

.method_missing(m, *args, &block) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/super_process/core.rb', line 74

def self.method_missing(m, *args, &block)
  if block_given?
    new.public_send(m, *args, &block)
  else
    new.public_send(m, *args)
  end
end

Instance Method Details

#error_messagesObject



66
67
68
69
70
71
72
# File 'lib/super_process/core.rb', line 66

def error_messages
  if errors.messages.values.present?
    errors.messages.values.first.first
  else
    ""
  end
end

#resultObject



62
63
64
# File 'lib/super_process/core.rb', line 62

def result
  @result
end