Class: Andromeda::Cmd::Cmd

Inherits:
Object show all
Defined in:
lib/andromeda/cmd.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, data = {}, cmd_time = nil) ⇒ Cmd

Returns a new instance of Cmd.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
# File 'lib/andromeda/cmd.rb', line 10

def initialize(cmd, data = {}, cmd_time = nil)
  raise ArgumentError unless cmd.kind_of?(Symbol)
  @cmd     = cmd
  @data    = data
  @time    = if cmd_time then cmd_time else Time.now.to_i end
  @comment = nil
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



6
7
8
# File 'lib/andromeda/cmd.rb', line 6

def cmd
  @cmd
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/andromeda/cmd.rb', line 7

def data
  @data
end

#timeObject (readonly)

Returns the value of attribute time.



8
9
10
# File 'lib/andromeda/cmd.rb', line 8

def time
  @time
end

Class Method Details

.from_json(json) ⇒ Object



42
43
44
# File 'lib/andromeda/cmd.rb', line 42

def self.from_json(json)
  Cmd.new json['cmd'].to_sym, json['data'], (json['time'] rescue nil)
end

.new_input(cmd, data = {}, cmd_time = nil) ⇒ Object



37
38
39
40
# File 'lib/andromeda/cmd.rb', line 37

def self.new_input(cmd, data = {}, cmd_time = nil)
  inner = Cmd.new cmd, data, cmd_time
  Cmd.new :input, inner, cmd_time
end

Instance Method Details

#as_jsonObject



23
24
25
26
27
28
# File 'lib/andromeda/cmd.rb', line 23

def as_json
  h = { cmd: cmd, data: (data.as_json rescue data), time: time }
  c = @comment
  h[:comment] = c if c
  h
end

#commentObject



18
# File 'lib/andromeda/cmd.rb', line 18

def comment ; @comment || ''  end

#comment=(str = nil) ⇒ Object



19
# File 'lib/andromeda/cmd.rb', line 19

def comment=(str = nil) ; @comment = str end

#if_cmd(sym) ⇒ Object



21
# File 'lib/andromeda/cmd.rb', line 21

def if_cmd(sym) ; if sym == cmd then yield data else seld end end

#to_sObject



30
# File 'lib/andromeda/cmd.rb', line 30

def to_s ; as_json.to_json end

#with_comment(str = nil) ⇒ Object



32
33
34
35
# File 'lib/andromeda/cmd.rb', line 32

def with_comment(str = nil)
  @comment = str
  self
end