Class: Imperator::Command

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveAttr::Model
Defined in:
lib/imperator/command.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/imperator/command.rb', line 71

def method_missing(method, *args)
  method_root = method.to_s.gsub(/=$/, "")
  if method.to_s[/=$/]
    self.attributes[method_root] = args.first
  elsif attributes.has_key?(method_root)
    self.attributes[method]
  else
    super
  end
end

Class Attribute Details

.perform_blockObject

Returns the value of attribute perform_block.



14
15
16
# File 'lib/imperator/command.rb', line 14

def perform_block
  @perform_block
end

Class Method Details

.action(&block) ⇒ Object



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

def self.action(&block)
  @perform_block = block
end

.load(command_string) ⇒ Object



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

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

Instance Method Details

#as_jsonObject



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

def as_json
  attributes.as_json
end

#commitObject



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

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

#commit!Object



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

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

#dumpObject



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

def dump
  attributes.to_json
end

#load(command_string) ⇒ Object



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

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

#performObject



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

def perform
  raise "You need to define the perform block for #{self.class.name}" unless self.class.perform_block
  run_callbacks :perform do 
    self.instance_exec(&self.class.perform_block)
  end
end

#perform!Object



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

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

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  false
end