Class: Groonga::Command::Format::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga/command/format/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, arguments) ⇒ Command

Returns a new instance of Command.



38
39
40
41
# File 'lib/groonga/command/format/command.rb', line 38

def initialize(name, arguments)
  @name = name
  @arguments = arguments
end

Class Method Details

.escape_value(value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/groonga/command/format/command.rb', line 24

def escape_value(value)
  escaped_value = value.gsub(/[\n"\\]/) do
    special_character = $MATCH
    case special_character
    when "\n"
      "\\n"
    else
      "\\#{special_character}"
    end
  end
  "\"#{escaped_value}\""
end

Instance Method Details

#command_line(options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/groonga/command/format/command.rb', line 43

def command_line(options={})
  pretty_print = options[:pretty_print]
  components = [@name]
  sorted_arguments = @arguments.sort_by do |name, _|
    name.to_s
  end
  sorted_arguments.each do |name, value|
    components << "--#{name} #{self.class.escape_value(value)}"
  end
  if pretty_print
    components.join(" \\\n  ")
  else
    components.join(" ")
  end
end