Class: Mongoid::Snappy

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Snappy

Returns a new instance of Snappy.



10
11
12
# File 'lib/mongoid_snappy.rb', line 10

def initialize(data)
  @data = data.force_encoding('UTF-8')
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



46
47
48
# File 'lib/mongoid_snappy.rb', line 46

def method_missing(name, *args, &block)
  @data.send(name, *args, &block)
end

Class Method Details

.demongoize(object) ⇒ Object

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



54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object

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



78
79
80
81
82
83
84
# 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) ⇒ Object

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



68
69
70
71
72
73
74
# 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

Instance Method Details

#==(other) ⇒ Object



22
23
24
25
26
27
28
29
30
# 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) ⇒ Object



32
33
34
# File 'lib/mongoid_snappy.rb', line 32

def coerce(something)
  [self, something]
end

#inspectObject



18
19
20
# File 'lib/mongoid_snappy.rb', line 18

def inspect
  '"' + @data + '"'
end

#mongoizeObject



36
37
38
39
40
41
42
# 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_sObject



14
15
16
# File 'lib/mongoid_snappy.rb', line 14

def to_s
  @data
end