class Mongoid::Snappy

Public Class Methods

new(data) click to toggle source
# File lib/mongoid_snappy.rb, line 10
def initialize(data)
  @data = data.force_encoding('UTF-8')
end

Protected Class Methods

demongoize(object) click to toggle source

Get the object as it was stored in the database, and instantiate this custom class from it.

# File lib/mongoid_snappy.rb, line 54
def demongoize(object)
  if defined?(Moped::BSON) && object.is_a?(Moped::BSON::Binary)
    Mongoid::Snappy.new(::Snappy.inflate(object.data))
  elsif defined?(BSON) && object.is_a?(BSON::Binary)
    Mongoid::Snappy.new(::Snappy.inflate(object.data))
  elsif object.is_a?(String)
    Mongoid::Snappy.new(object)
  else
    object
  end
end
evolve(object) click to toggle source

Converts the object that was supplied to a criteria and converts it into a database friendly form.

# File lib/mongoid_snappy.rb, line 78
def evolve(object)
  case object
    when Mongoid::Snappy then object.mongoize
    when String then Mongoid::Snappy.new(object).mongoize
    else object
  end
end
mongoize(object) click to toggle source

Takes any possible object and converts it to how it would be stored in the database.

# File lib/mongoid_snappy.rb, line 68
def mongoize(object)
  case
    when object.is_a?(Mongoid::Snappy) then object.mongoize
    when object.is_a?(String) then Mongoid::Snappy.new(object).mongoize
    else object
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/mongoid_snappy.rb, line 22
def ==(other)
  if other.class == self.class
    other.to_s == to_s
  elsif other.class == String
    @data == other
  else
    false
  end
end
coerce(something) click to toggle source
# File lib/mongoid_snappy.rb, line 32
def coerce(something)
  [self, something]
end
inspect() click to toggle source
# File lib/mongoid_snappy.rb, line 18
def inspect
  '"' + @data + '"'
end
mongoize() click to toggle source
# File lib/mongoid_snappy.rb, line 36
def mongoize
  if defined?(Moped::BSON) && !defined?(BSON)
    Moped::BSON::Binary.new(:generic, ::Snappy.deflate(@data))
  else
    BSON::Binary.new(::Snappy.deflate(@data), :generic)
  end
end
to_s() click to toggle source
# File lib/mongoid_snappy.rb, line 14
def to_s
  @data
end

Protected Instance Methods

method_missing(name, *args, &block) click to toggle source
# File lib/mongoid_snappy.rb, line 46
def method_missing(name, *args, &block)
  @data.send(name, *args, &block)
end