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

- (Duration) initialize(seconds)

Instantiates a new Duration object



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

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

Class Method Details

+ (Duration) demongoize(seconds)

Deserialize a Duration given the amount of seconds stored by Mongodb

Parameters:

  • duration (Integer, nil)

    in seconds

Returns:



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

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

+ (Object) evolve(object)

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



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

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

+ (Integer) mongoize(value)

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



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

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

- (Object) mongoize

Converts the Duration object into a MongoDB friendly value.



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

def mongoize
  self.to_i
end