class Groonga::Command::Base

Attributes

arguments[R]
command_name[R]

@return [String] The command name.

@since 1.1.8

original_format[RW]
original_source[RW]
path_prefix[RW]

Public Class Methods

new(arg1=nil, arg2=nil, arg3=nil) click to toggle source

@overload initialize(pair_arguments, ordered_arguments={})

Initializes a new known command. The command class must implement
{.command_name} method.

@param pair_arguments [::Hash<String, String>] The list of
  pair arguments.

@param ordered_arguments [::Array<String>] The list of
  ordered arguments.

@since 1.2.3

@overload initialize(command_name, pair_arguments, ordered_arguments={})

Initializes a new unknown command.

@param command_name [String] The command name.

@param pair_arguments [::Hash<String, String>] The list of
  pair arguments.

@param ordered_arguments [::Array<String>] The list of
  ordered arguments.
# File lib/groonga/command/base.rb, line 85
def initialize(arg1=nil, arg2=nil, arg3=nil)
  case arg1
  when String, Symbol
    command_name = arg1.to_s
    pair_arguments = arg2
    ordered_arguments = arg3
  else
    command_name = self.class.command_name
    pair_arguments = arg1
    ordered_arguments = arg2
  end
  pair_arguments ||= {}
  ordered_arguments ||= []

  @command_name = command_name
  @arguments = construct_arguments(pair_arguments, ordered_arguments)
  @original_format = nil
  @original_source = nil
  @path_prefix = "/d/"
end
parameter_names() click to toggle source
# File lib/groonga/command/base.rb, line 51
def parameter_names
  []
end

Public Instance Methods

==(other) click to toggle source
# File lib/groonga/command/base.rb, line 124
def ==(other)
  other.is_a?(self.class) and
    @command_name == other.command_name and
    @arguments == other.arguments
end
[](name) click to toggle source
# File lib/groonga/command/base.rb, line 111
def [](name)
  @arguments[normalize_name(name)]
end
[]=(name, value) click to toggle source
# File lib/groonga/command/base.rb, line 115
def []=(name, value)
  @arguments[normalize_name(name)] = value
end
command_format?() click to toggle source
# File lib/groonga/command/base.rb, line 134
def command_format?
  @original_format == :command
end
has_key?(name)
Alias for: key?
key?(name) click to toggle source
# File lib/groonga/command/base.rb, line 119
def key?(name)
  @arguments.key?(normalize_name(name))
end
Also aliased as: has_key?
name() click to toggle source

@deprecated since 1.1.8. Use {#command_name} instead.

# File lib/groonga/command/base.rb, line 107
def name
  command_name
end
output_type() click to toggle source
# File lib/groonga/command/base.rb, line 138
def output_type
  (self[:output_type] || :json).to_sym
end
request_id() click to toggle source

@return [String, nil] `request_id` parameter value.

@since 1.1.8

# File lib/groonga/command/base.rb, line 145
def request_id
  self[:request_id]
end
to_command_format(options={}) click to toggle source
# File lib/groonga/command/base.rb, line 153
def to_command_format(options={})
  format = Format::Command.new(@command_name, normalized_arguments)
  format.command_line(options)
end
to_elasticsearch_format(options={}) click to toggle source
# File lib/groonga/command/base.rb, line 158
def to_elasticsearch_format(options={})
  format = Format::Elasticsearch.new(self)
  format.json(options)
end
to_s() click to toggle source
# File lib/groonga/command/base.rb, line 163
def to_s
  if uri_format?
    to_uri_format
  else
    to_command_format
  end
end
to_uri_format() click to toggle source
# File lib/groonga/command/base.rb, line 149
def to_uri_format
  Format::URI.new(@path_prefix, @command_name, normalized_arguments).path
end
uri_format?() click to toggle source
# File lib/groonga/command/base.rb, line 130
def uri_format?
  @original_format == :uri
end

Private Instance Methods

array_value(name) click to toggle source
# File lib/groonga/command/base.rb, line 217
def array_value(name)
  parse_array_value(self[name] || "")
end
boolean_value(name, **keyword_args) click to toggle source
# File lib/groonga/command/base.rb, line 235
def boolean_value(name, **keyword_args)
  parse_boolean_value(self[name], **keyword_args)
end
construct_arguments(pair_arguments, ordered_arguments) click to toggle source
# File lib/groonga/command/base.rb, line 172
def construct_arguments(pair_arguments, ordered_arguments)
  arguments = {}

  pair_arguments.each do |key, value|
    key = key.to_sym if key.is_a?(String)
    arguments[key] = value
  end

  names = self.class.parameter_names
  ordered_arguments.each_with_index do |argument, i|
    name = names[i]
    break if name.nil?
    name = name.to_sym if name.is_a?(String)
    arguments[name] = argument
  end

  arguments
end
flags_value(name) click to toggle source
# File lib/groonga/command/base.rb, line 226
def flags_value(name)
  parse_flags_value(self[name] || "")
end
integer_value(name) click to toggle source
# File lib/groonga/command/base.rb, line 202
def integer_value(name)
  parse_integer_value(self[name])
end
normalize_name(name) click to toggle source
# File lib/groonga/command/base.rb, line 191
def normalize_name(name)
  name = name.to_sym if name.is_a?(String)
  name
end
normalized_arguments() click to toggle source
# File lib/groonga/command/base.rb, line 196
def normalized_arguments
  @arguments.reject do |_, value|
    value.nil?
  end
end
parse_array_value(value) click to toggle source
# File lib/groonga/command/base.rb, line 221
def parse_array_value(value)
  return nil if value.nil?
  value.strip.split(/\s*,\s*/)
end
parse_boolean_value(value, default:, invalid:) click to toggle source
# File lib/groonga/command/base.rb, line 239
def parse_boolean_value(value, default:, invalid:)
  return default if value.nil?
  value = value.strip
  return default if value.empty?
  case value
  when "yes"
    true
  when "no"
    false
  else
    invalid
  end
end
parse_flags_value(value) click to toggle source
# File lib/groonga/command/base.rb, line 230
def parse_flags_value(value)
  return nil if value.nil?
  value.strip.split(/\s*[| ]\s*/)
end
parse_integer_value(value) click to toggle source
# File lib/groonga/command/base.rb, line 206
def parse_integer_value(value)
  return value if value.nil?
  return nil if value.empty?

  begin
    Integer(value)
  rescue ArgumentError
    value
  end
end