Class: Mongoid::Fields::Timespan

Inherits:
Object
  • Object
show all
Extended by:
Methods
Includes:
Serializable, Methods
Defined in:
lib/timespan/mongoid.rb,
lib/timespan/mongoid/mongoid_2x.rb

Defined Under Namespace

Modules: Methods

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Methods

asap, deserialize_time, from, serialize_time, to

Class Attribute Details

.end_fieldObject



22
23
24
# File 'lib/timespan/mongoid.rb', line 22

def end_field
  @end_field || :to
end

.start_fieldObject



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

def start_field
  @start_field || :from
end

Class Method Details

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



7
8
9
# File 'lib/timespan/mongoid/mongoid_2x.rb', line 7

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:



15
16
17
18
# File 'lib/timespan/mongoid/mongoid_2x.rb', line 15

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:

Returns:

  • (Hash)

    Timespan in seconds



25
26
27
28
29
30
31
32
33
34
# File 'lib/timespan/mongoid/mongoid_2x.rb', line 25

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