Class: XapianDb::TypeCodec::NumberCodec

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

Class Method Summary collapse

Class Method Details

.decode(encoded_number) ⇒ BigDecimal

Decode a string to a BigDecimal



161
162
163
164
165
166
167
# File 'lib/type_codec.rb', line 161

def self.decode(encoded_number)
  begin
    BigDecimal.new(Xapian::sortable_unserialise(encoded_number).to_s)
  rescue TypeError
    raise ArgumentError.new "#{encoded_number} cannot be unserialized"
  end
end

.encode(number) ⇒ String

Encode a number to a sortable string



147
148
149
150
151
152
153
154
155
156
# File 'lib/type_codec.rb', line 147

def self.encode(number)
  case number.class.name
    when "Fixnum", "Float", "Bignum"
      Xapian::sortable_serialise number
    when "BigDecimal"
      Xapian::sortable_serialise number.to_f
    else
      raise ArgumentError.new "#{number} was expected to be a number"
  end
end