Class: CultomePlayer::Objects::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/cultome_player/objects/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, parameters) ⇒ Command

Returns a new instance of Command.



7
8
9
10
11
# File 'lib/cultome_player/objects/command.rb', line 7

def initialize(action, parameters)
  @action = action[:value]
  @parameters = parameters.collect{|p| Parameter.new(p) }
  @no_history = params(:literal).any?{|p| p.value == 'no_history'}
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



4
5
6
# File 'lib/cultome_player/objects/command.rb', line 4

def action
  @action
end

#parametersObject (readonly)

Returns the value of attribute parameters.



5
6
7
# File 'lib/cultome_player/objects/command.rb', line 5

def parameters
  @parameters
end

Instance Method Details

#history?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/cultome_player/objects/command.rb', line 13

def history?
  !@no_history
end

#params(type = nil) ⇒ List<Parameter>

Returns the parameters, optionally filtered by type

Parameters:

  • type (Symbol) (defaults to: nil)

    Parameter type to filter the results

Returns:

  • (List<Parameter>)

    The parameters associated with the command, optionally filtered.



21
22
23
24
# File 'lib/cultome_player/objects/command.rb', line 21

def params(type=nil)
  return @parameters if type.nil?
  @parameters.select{|p| p.type == type}
end

#params_groupsHash<Symbol, List<Parameter>>

Returns a map that contains parameter type as key and a list of the parameters of that type as value.

Returns:

  • (Hash<Symbol, List<Parameter>>)

    Parameters grouped by type.



29
30
31
# File 'lib/cultome_player/objects/command.rb', line 29

def params_groups
  @parameters.collect{|p| p.type }.each_with_object({}){|type,acc| acc[type] = params(type) }
end

#params_values(type) ⇒ List<Object>

Returns a list with only the parameters values of certain type.

Parameters:

  • type (Symbol)

    The type of parameters.

Returns:

  • (List<Object>)

    The values of the parameters.



37
38
39
# File 'lib/cultome_player/objects/command.rb', line 37

def params_values(type)
  params(type).map{|p| p.value }
end

#to_sObject



41
42
43
# File 'lib/cultome_player/objects/command.rb', line 41

def to_s
  "#{action} #{@parameters.join(" ")}"
end