Class: Mongoid::Fields::Timespan
- Includes:
- Serializable
- Defined in:
- lib/timespan/mongoid.rb
Class Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#deserialize(hash) ⇒ Timespan
Deserialize a Timespan given the hash stored by Mongodb.
-
#serialize(value) ⇒ Hash
Serialize a Timespan or a Hash (with Timespan units) or a Duration in some form to a BSON serializable type.
Class Attribute Details
.end_field ⇒ Object
33 34 35 |
# File 'lib/timespan/mongoid.rb', line 33 def end_field @end_field || :to end |
.start_field ⇒ Object
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, = {}) super end |
Instance Method Details
#deserialize(hash) ⇒ Timespan
Deserialize a Timespan given the hash stored by Mongodb
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.
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 |