Class: Timespan

Inherits:
Object show all
Defined in:
lib/timespan/mongoid/mongoid_3x.rb,
lib/timespan/extensions/time_lord.rb

Constant Summary collapse

Serializer =
Mongoid::Fields::Timespan

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.custom_serialization?(operator) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
# File 'lib/timespan/mongoid/mongoid_3x.rb', line 68

def custom_serialization?(operator)
  return false unless operator
  case operator
    when '$gte', '$gt', '$lt', '$lte', '$eq', '$between', '$btw'
      true
  else
    false
  end
end

.custom_specify(name, operator, value, options = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/timespan/mongoid/mongoid_3x.rb', line 78

def custom_specify(name, operator, value, options = {})
  # puts "custom_specify: #{name}"
  timespan = value.__evolve_to_timespan__
  case operator
    when '$gte', '$gt', '$lt', '$lte', '$eq', '$between', '$btw'
      specify_with_asap(name, operator, timespan, options)
  else
    raise RuntimeError, "Unsupported operator"
  end
end

.demongoize(object) ⇒ Timespan

Deserialize a Timespan given the hash stored by Mongodb

Parameters:

  • Timespan (Hash)

    as hash

Returns:



52
53
54
55
56
57
58
59
60
# File 'lib/timespan/mongoid/mongoid_3x.rb', line 52

def demongoize(object)
  return if !object
  case object
  when Hash
    object.__evolve_to_timespan__
  else
    ::Timespan.new object
  end        
end

.evolve(object) ⇒ Object

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



64
65
66
# File 'lib/timespan/mongoid/mongoid_3x.rb', line 64

def evolve(object)
  object.__evolve_to_timespan__.mongoize
end

.mongoize(object) ⇒ 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



38
39
40
41
42
43
44
45
46
# File 'lib/timespan/mongoid/mongoid_3x.rb', line 38

def mongoize object
  case object
  when Timespan then object.mongoize
  when Hash
    ::Timespan.new object
  else
    object
  end
end

.specify_with_asap(name, operator, timespan, options) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/timespan/mongoid/mongoid_3x.rb', line 89

def specify_with_asap(name, operator, timespan, options)
  query = { 'from' => {}, 'to' => {} }
  case operator      
  when '$gte', '$gt'
    query['from'][operator] = Serializer.serialize_time(timespan.min)
  when '$lte', '$lt', '$eq'
    query['to'][operator] = Serializer.serialize_time(timespan.min)
  when '$between', '$btw'
    query['from']['$gte'] = Serializer.serialize_time(timespan.min)
    query['to']['$lte'] = Serializer.serialize_time(timespan.max)
  end
  # puts "query: #{query}"
  query
end

Instance Method Details

#__evolve_to_timespan__Object



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

def __evolve_to_timespan__
  self
end

#mongoizeHash

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



19
20
21
22
23
24
25
26
27
28
# File 'lib/timespan/mongoid/mongoid_3x.rb', line 19

def mongoize
  hash = { 
    :from => Serializer.serialize_time(start_time), 
    :to => Serializer.serialize_time(end_time), 
    :duration => duration.total, 
    :asap => asap? 
  }
  # puts "serialize: #{hash}"
  hash
end