Class: Droonga::MessagePackPacker

Inherits:
Object
  • Object
show all
Defined in:
lib/droonga/message-pack-packer.rb,
lib/droonga/message-pack-packer/version.rb,
lib/droonga/message-pack-packer/time-formatter.rb,
lib/droonga/message-pack-packer/geo-point-formatter.rb

Defined Under Namespace

Classes: GeoPointFormatter, TimeFormatter

Constant Summary collapse

MICRO_SECONDS_DECIMAL_PLACE =
6
VERSION =
"1.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessagePackPacker

Returns a new instance of MessagePackPacker.



33
34
35
# File 'lib/droonga/message-pack-packer.rb', line 33

def initialize
  @packer = MessagePack::Packer.new
end

Class Method Details

.pack(object) ⇒ Object



24
25
26
27
28
# File 'lib/droonga/message-pack-packer.rb', line 24

def pack(object)
  packer = new
  packer.pack(object)
  packer.to_s
end

Instance Method Details

#clearObject



66
67
68
# File 'lib/droonga/message-pack-packer.rb', line 66

def clear
  @packer.clear
end

#pack(object) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/droonga/message-pack-packer.rb', line 37

def pack(object)
  case object
  when Array
    @packer.write_array_header(object.size)
    object.each do |element|
      pack(element)
    end
  when Hash
    @packer.write_map_header(object.size)
    object.each do |key, value|
      pack(key)
      pack(value)
    end
  when Time
    @packer.write(TimeFormatter.format(object))
  else
    case object.class.name
    when "Groonga::WGS84GeoPoint", "Groonga::TokyoGeoPoint"
      @packer.write(GeoPointFormatter.format(object))
    else
      @packer.write(object)
    end
  end
end

#to_sObject



62
63
64
# File 'lib/droonga/message-pack-packer.rb', line 62

def to_s
  @packer.to_s
end