Class: Mongoid::Fields::Duration

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seconds) ⇒ Duration

Instantiates a new Duration object



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

def initialize(seconds)
  ::Duration.new(seconds)
end

Class Method Details

.demongoize(seconds) ⇒ Duration

Deserialize a Duration given the amount of seconds stored by Mongodb

Parameters:

  • duration (Integer, nil)

    in seconds

Returns:



25
26
27
28
# File 'lib/duration/mongoid.rb', line 25

def demongoize(seconds)
  return if !seconds
  ::Duration.new(seconds)
end

.evolve(object) ⇒ Object

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



48
49
50
51
52
53
# File 'lib/duration/mongoid.rb', line 48

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

.mongoize(value) ⇒ Integer

Serialize a Duration or a Hash (with duration units) or a amount of seconds to a BSON serializable type.

Parameters:

Returns:

  • (Integer)

    duration in seconds



35
36
37
38
39
40
41
42
43
44
# File 'lib/duration/mongoid.rb', line 35

def mongoize(value)
  return if value.blank?
  if value.is_a?(Hash)
    value.delete_if{|k, v| v.blank? || !::Duration::UNITS.include?(k.to_sym)}
    return if value.blank?
    ::Duration.new(value).to_i
  elsif value.respond_to?(:to_i)
    value.to_i
  end
end

Instance Method Details

#mongoizeObject

Converts the Duration object into a MongoDB friendly value.



16
17
18
# File 'lib/duration/mongoid.rb', line 16

def mongoize
  self.to_i
end