Class: XapianDb::TypeCodec::IntegerCodec

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

Class Method Summary collapse

Class Method Details

.decode(encoded_integer) ⇒ Integer

Decode a string to an integer

Parameters:

  • integer_as_string (String)

    a string representing an integer

Returns:

  • (Integer)

    the decoded integer


188
189
190
191
192
193
194
195
# File 'lib/type_codec.rb', line 188

def self.decode(encoded_integer)
  begin
    return nil if encoded_integer.nil? || encoded_integer.to_s.strip == ""
    Xapian::sortable_unserialise(encoded_integer).to_i
  rescue TypeError
    raise ArgumentError.new "#{encoded_integer} cannot be unserialized"
  end
end

.encode(number) ⇒ String

Encode an integer to a sortable string

Parameters:

  • integer (Integer)

    an integer to encode

Returns:

  • (String)

    the encoded integer


175
176
177
178
179
180
181
182
183
# File 'lib/type_codec.rb', line 175

def self.encode(number)
  return nil if number.nil?
  case number.class.name
    when "Fixnum"
      Xapian::sortable_serialise number
    else
      raise ArgumentError.new "#{number} was expected to be an integer"
  end
end