Class: Mongoid::Fields::Timespan

Inherits:
Object
  • Object
show all
Includes:
Serializable
Defined in:
lib/timespan/mongoid.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.end_fieldObject



33
34
35
# File 'lib/timespan/mongoid.rb', line 33

def end_field
  @end_field || :to
end

.start_fieldObject



29
30
31
# File 'lib/timespan/mongoid.rb', line 29

def start_field
  @start_field || :from
end

Class Method Details

.instantiate(name, options = {}) ⇒ Object



38
39
40
# File 'lib/timespan/mongoid.rb', line 38

def self.instantiate(name, options = {})
  super
end

Instance Method Details

#deserialize(hash) ⇒ Timespan

Deserialize a Timespan given the hash stored by Mongodb

Parameters:

  • Timespan (Hash)

    as hash

Returns:



46
47
48
49
# File 'lib/timespan/mongoid.rb', line 46

def deserialize(hash)
  return if !hash
  ::Timespan.new :from => from(hash), :to => to(hash)
end

#serialize(value) ⇒ 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



56
57
58
59
60
61
62
63
64
65
# File 'lib/timespan/mongoid.rb', line 56

def serialize(value)
  return if value.blank?
  timespan = case value
  when ::Timespan
    value
  else
    ::Timespan.new(value)
  end
  {:from => serialize_time(timespan.start_time), :to => serialize_time(timespan.end_time.to_i), :duration => timespan.duration.total }
end