Class: Timespan

Inherits:
Object show all
Defined in:
lib/timespan/mongoid/mongoid_3x.rb

Constant Summary collapse

Serializer =
Mongoid::Fields::Timespan

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.demongoize(object) ⇒ Timespan

Deserialize a Timespan given the hash stored by Mongodb

Parameters:

  • Timespan (Hash)

    as hash

Returns:



37
38
39
40
41
42
43
44
45
# File 'lib/timespan/mongoid/mongoid_3x.rb', line 37

def demongoize(object)
  return if !object
  case object
  when Hash
    ::Timespan.new :from => Serializer.from(object), :to => Serializer.to(object)
  else
    ::Timespan.new object
  end        
end

.evolve(object) ⇒ Object

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



49
50
51
52
53
54
# File 'lib/timespan/mongoid/mongoid_3x.rb', line 49

def evolve(object)
  case object
  when Timespan then object.mongoize
  else object
  end
end

.mongoize(object) ⇒ Hash

Serialize a Timespan or a Hash (with Timespan units) or a Duration in some form to a BSON serializable type.

Parameters:

  • value (Timespan, Hash, Integer, String)

Returns:

  • (Hash)

    Timespan in seconds



23
24
25
26
27
28
29
30
31
# File 'lib/timespan/mongoid/mongoid_3x.rb', line 23

def mongoize object
  case object
  when Timespan then object.mongoize
  when Hash
    ::Timespan.new object
  else
    object
  end
end

Instance Method Details

#mongoizeHash

Serialize a Timespan or a Hash (with Timespan units) or a Duration in some form to a BSON serializable type.

Parameters:

  • value (Timespan, Hash, Integer, String)

Returns:

  • (Hash)

    Timespan in seconds



11
12
13
# File 'lib/timespan/mongoid/mongoid_3x.rb', line 11

def mongoize
  {:from => Serializer.serialize_time(start_time), :to => Serializer.serialize_time(end_time), :duration => duration.total }
end