Class: ActiveCommand::Command

Inherits:
Object
  • Object
show all
Includes:
Core, ActiveModel::Serializers::JSON, ActiveModel::Validations, Sidekiq::Worker
Defined in:
lib/active_command/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_json(json) ⇒ Object



11
12
13
14
15
# File 'lib/active_command/command.rb', line 11

def self.from_json(json)
  klass = self.new
  klass.from_json(json)
  klass
end

Instance Method Details

#executeObject

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/active_command/command.rb', line 39

def execute
  raise NotImplementedError, "#{self.class.name} does not implement an execute method"
end

#perform(job_data) ⇒ Object



26
27
28
29
# File 'lib/active_command/command.rb', line 26

def perform(job_data)
  self.from_json(job_data)
  self.perform_now
end

#perform_nowObject



31
32
33
34
35
36
37
# File 'lib/active_command/command.rb', line 31

def perform_now
  if self.valid?
    self.execute
  else
    raise ActiveCommand::CommandNotValidError, self.errors.full_messages
  end
end

#runObject



22
23
24
# File 'lib/active_command/command.rb', line 22

def run
  self.class.perform_async self.to_params.to_json
end

#to_paramsObject Also known as: to_hash



17
18
19
# File 'lib/active_command/command.rb', line 17

def to_params
  ActiveSupport::HashWithIndifferentAccess.new(attributes).delete_if { |k, v| v.nil? }
end