Class: Groonga::QueryLog::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga/query-log.rb

Direct Known Subclasses

SelectCommand

Constant Summary collapse

@@registered_commands =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parameters) ⇒ Command

Returns a new instance of Command.



73
74
75
76
77
# File 'lib/groonga/query-log.rb', line 73

def initialize(name, parameters)
  @name = name
  @parameters = parameters
  @original_format = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



71
72
73
# File 'lib/groonga/query-log.rb', line 71

def name
  @name
end

#original_formatObject

Returns the value of attribute original_format.



72
73
74
# File 'lib/groonga/query-log.rb', line 72

def original_format
  @original_format
end

#parametersObject (readonly)

Returns the value of attribute parameters.



71
72
73
# File 'lib/groonga/query-log.rb', line 71

def parameters
  @parameters
end

Class Method Details

.parse(input) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/groonga/query-log.rb', line 31

def parse(input)
  if input.start_with?("/d/")
    parse_uri_path(input)
  else
    parse_command_line(input)
  end
end

.register(name, klass) ⇒ Object



27
28
29
# File 'lib/groonga/query-log.rb', line 27

def register(name, klass)
  @@registered_commands[name] = klass
end

Instance Method Details

#==(other) ⇒ Object



79
80
81
82
83
# File 'lib/groonga/query-log.rb', line 79

def ==(other)
  other.is_a?(self.class) and
    @name == other.name and
    @parameters == other.parameters
end

#command_format?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/groonga/query-log.rb', line 89

def command_format?
  @original_format == :command
end

#to_command_formatObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/groonga/query-log.rb', line 111

def to_command_format
  command_line = [@name]
  sorted_parameters = @parameters.sort_by do |name, _|
    name.to_s
  end
  sorted_parameters.each do |name, value|
    escaped_value = value.gsub(/[\n"\\]/) do
      special_character = $MATCH
      case special_character
      when "\n"
        "\\n"
      else
        "\\#{special_character}"
      end
    end
    command_line << "--#{name}"
    command_line << "\"#{escaped_value}\""
  end
  command_line.join(" ")
end

#to_uri_formatObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/groonga/query-log.rb', line 93

def to_uri_format
  path = "/d/#{@name}"
  parameters = @parameters.dup
  output_type = parameters.delete("output_type")
  path << ".#{output_type}" if output_type
  unless parameters.empty?
    sorted_parameters = parameters.sort_by do |name, _|
      name.to_s
    end
    uri_parameters = sorted_parameters.collect do |name, value|
      "#{CGI.escape(name)}=#{CGI.escape(value)}"
    end
    path << "?"
    path << uri_parameters.join("&")
  end
  path
end

#uri_format?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/groonga/query-log.rb', line 85

def uri_format?
  @original_format == :uri
end