Class: Groonga::Command::Builder

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, arguments = {}) ⇒ Builder

Returns a new instance of Builder.



31
32
33
34
# File 'lib/groonga/command.rb', line 31

def initialize(command, arguments={})
  @command = command
  @arguments = arguments
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



30
31
32
# File 'lib/groonga/command.rb', line 30

def arguments
  @arguments
end

#commandObject (readonly)

Returns the value of attribute command.



30
31
32
# File 'lib/groonga/command.rb', line 30

def command
  @command
end

Class Method Details

.escape_value(value) ⇒ Object



22
23
24
25
26
27
# File 'lib/groonga/command.rb', line 22

def escape_value(value)
  escaped_value = value.to_s.gsub(/[\\"]/) do |matched|
    "\\#{matched}"
  end
  "\"#{escaped_value}\""
end

Instance Method Details

#[](key) ⇒ Object



36
37
38
# File 'lib/groonga/command.rb', line 36

def [](key)
  @arguments[key]
end

#[]=(key, value) ⇒ Object



40
41
42
# File 'lib/groonga/command.rb', line 40

def []=(key, value)
  @arguments[key] = value
end

#buildObject



44
45
46
47
48
49
50
51
52
# File 'lib/groonga/command.rb', line 44

def build
  query = "#{@command} "
  @arguments.each do |key, value|
    value = value.join(", ") if value.is_a?(::Array)
    escaped_value = self.class.escape_value(value)
    query << " --#{key} #{escaped_value}"
  end
  query
end