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, Virtus
Defined in:
lib/imperator/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



45
46
47
48
49
# File 'lib/imperator/command.rb', line 45

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

Class Method Details

.action(&block) ⇒ Object



21
22
23
# File 'lib/imperator/command.rb', line 21

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

.load(command_string) ⇒ Object



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

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

Instance Method Details

#actionObject

This method is abstract.

Raises:

  • (NoMethodError)


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

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

#as_json(*args) ⇒ Object



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

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

#commitObject



40
41
42
43
# File 'lib/imperator/command.rb', line 40

def commit
  #TODO: background code for this
  self.perform
end

#commit!Object



35
36
37
38
# File 'lib/imperator/command.rb', line 35

def commit!
  raise Imperator::InvalidCommandError.new "Command was invalid" unless valid?
  self.commit
end

#dumpObject



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

def dump
  attributes.to_json
end

#load(command_string) ⇒ Object



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

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

#performObject



73
74
75
# File 'lib/imperator/command.rb', line 73

def perform
  run_callbacks(:perform) { action }
end

#perform!Object



63
64
65
66
# File 'lib/imperator/command.rb', line 63

def perform!
  raise InvalidCommandError.new "Command was invalid" unless valid?
  self.perform
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  false
end