Class: Imperator::Command

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Serializable::JSON, ActiveModel::Serializable::XML, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, ActiveModel::Validations
Defined in:
lib/imperator/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



42
43
44
45
46
# File 'lib/imperator/command.rb', line 42

def initialize(*)
  run_callbacks :initialize do
    super
  end
end

Class Method Details

.action(&block) ⇒ Object



18
19
20
# File 'lib/imperator/command.rb', line 18

def self.action(&block)
  define_method(:action, &block)
end

.load(command_string) ⇒ Object



52
53
54
# File 'lib/imperator/command.rb', line 52

def self.load(command_string)
  new(JSON.parse(command_string))
end

Instance Method Details

#actionObject

This method is abstract.

Raises:

  • (NoMethodError)


66
67
68
# File 'lib/imperator/command.rb', line 66

def action
  raise NoMethodError, "Please define #action for #{self.class.name}"
end

#as_json(*args) ⇒ Object



24
25
26
# File 'lib/imperator/command.rb', line 24

def as_json(*args)
  attributes.as_json(*args)
end

#commitObject



37
38
39
40
# File 'lib/imperator/command.rb', line 37

def commit
  # TODO: background code for this
  perform
end

#commit!Object



32
33
34
35
# File 'lib/imperator/command.rb', line 32

def commit!
  raise Imperator::InvalidCommandError, 'Command was invalid' unless valid?
  commit
end

#dumpObject



48
49
50
# File 'lib/imperator/command.rb', line 48

def dump
  attributes.to_json
end

#load(command_string) ⇒ Object



56
57
58
# File 'lib/imperator/command.rb', line 56

def load(command_string)
  self.attributes = HashWithIndifferentAccess.new(JSON.parse(command_string))
end

#performObject



70
71
72
# File 'lib/imperator/command.rb', line 70

def perform
  run_callbacks(:perform) { action }
end

#perform!Object



60
61
62
63
# File 'lib/imperator/command.rb', line 60

def perform!
  raise InvalidCommandError, 'Command was invalid' unless valid?
  perform
end

#persisted?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/imperator/command.rb', line 28

def persisted?
  false
end