Class: Gaq::CommandLanguage

Inherits:
Object
  • Object
show all
Defined in:
lib/gaq/command_language.rb

Defined Under Namespace

Classes: Command, CommandDescriptor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandLanguage

Returns a new instance of CommandLanguage.



5
6
7
# File 'lib/gaq/command_language.rb', line 5

def initialize
  @descriptors = {}
end

Instance Attribute Details

#value_coercer=(value) ⇒ Object (writeonly)

Sets the attribute value_coercer

Parameters:

  • value

    the value to set the attribute value_coercer to.



3
4
5
# File 'lib/gaq/command_language.rb', line 3

def value_coercer=(value)
  @value_coercer = value
end

Instance Method Details

#commands_from_flash_items(flash_items) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/gaq/command_language.rb', line 28

def commands_from_flash_items(flash_items)
  flash_items.map do |flash_item|
    descriptor, tracker_name = descriptor_and_tracker_name_from_first_segment(flash_item.first)
    params = flash_item.drop(1).take(descriptor.signature.length)
    Command.new(descriptor, descriptor.name, params, tracker_name)
  end
end

#commands_to_flash_items(commands) ⇒ Object Also known as: commands_to_segments_for_to_json



19
20
21
22
23
# File 'lib/gaq/command_language.rb', line 19

def commands_to_flash_items(commands)
  commands.map do |command|
    command_to_segments(command)
  end
end

#knows_command(identifier) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/gaq/command_language.rb', line 11

def knows_command(identifier)
  @descriptors[identifier] = CommandDescriptor.new.tap do |desc|
    desc.identifier = identifier
    yield desc
  end
  self
end

#new_command(identifier, *params) ⇒ Object



47
48
49
50
51
52
# File 'lib/gaq/command_language.rb', line 47

def new_command(identifier, *params)
  descriptor = @descriptors.fetch(identifier) { raise "no command with identifier #{identifier.inspect}" }
  params = coerce_params(params, descriptor.signature)

  Command.new(descriptor, descriptor.name, params)
end

#sort_commands(commands) ⇒ Object

modifies commands



37
38
39
40
41
42
43
44
45
# File 'lib/gaq/command_language.rb', line 37

def sort_commands(commands)
  sorted_pairs = commands.each_with_index.sort_by do |command, index|
    [
      command.descriptor.sort_slot || sort_slot_fallback,
      index
    ]
  end
  commands.replace sorted_pairs.map(&:first)
end