Class: TimeField
- Inherits:
-
Object
- Object
- TimeField
- Defined in:
- lib/mongoid_time_field/time_field.rb
Instance Method Summary collapse
- #build_regexp(format) ⇒ Object
-
#demongoize(object) ⇒ Object
Get the object as it was stored in the database, and instantiate this custom class from it.
-
#evolve(object) ⇒ Object
Converts the object that was supplied to a criteria and converts it into a database friendly form.
-
#initialize(options = {}) ⇒ TimeField
constructor
A new instance of TimeField.
-
#mongoize(object) ⇒ Object
Takes any possible object and converts it to how it would be stored in the database.
- #parse(value) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ TimeField
Returns a new instance of TimeField.
2 3 4 5 6 7 |
# File 'lib/mongoid_time_field/time_field.rb', line 2 def initialize( = {}) @options = @options[:format] = 'hh?:mm:SS' unless [:format] @options[:regexp] = build_regexp([:format]) unless [:regexp] end |
Instance Method Details
#build_regexp(format) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/mongoid_time_field/time_field.rb', line 9 def build_regexp(format) s = '^' + Regexp.escape(format) + '$' s.gsub!('hh\?', '(?<h>\d*?)') s.gsub!(':', ':?') s.gsub!('-', '-?') s.gsub!('_', '_?') s.gsub!('HH', '(?<h>\d*?)') s.gsub!('hh', '(?<h>\d*?)') s.gsub!('MM', '(?<m>\d*)') s.gsub!('mm', '(?<m>\d*)') s.gsub!('SS', '(?<s>\d*)') Regexp.new(s) end |
#demongoize(object) ⇒ Object
Get the object as it was stored in the database, and instantiate this custom class from it.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mongoid_time_field/time_field.rb', line 52 def demongoize(object) if object.nil? nil else case object when String then parse(object) when Integer then Mongoid::TimeField::Value.new(object, @options) when Fixnum then Mongoid::TimeField::Value.new(object, @options) else object end end end |
#evolve(object) ⇒ Object
Converts the object that was supplied to a criteria and converts it into a database friendly form.
77 78 79 80 81 82 83 |
# File 'lib/mongoid_time_field/time_field.rb', line 77 def evolve(object) case object when String then parse(object).mongoize when Mongoid::TimeField::Value then object.mongoize else object end end |
#mongoize(object) ⇒ Object
Takes any possible object and converts it to how it would be stored in the database.
67 68 69 70 71 72 73 |
# File 'lib/mongoid_time_field/time_field.rb', line 67 def mongoize(object) case object when Mongoid::TimeField::Value then object.mongoize when String then parse(object).mongoize else object end end |
#parse(value) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/mongoid_time_field/time_field.rb', line 24 def parse(value) if value.blank? nil elsif value.to_i.to_s == value if @options[:format].index('SS').nil? Mongoid::TimeField::Value.new(value.to_i * 60, @options) else Mongoid::TimeField::Value.new(value.to_i, @options) end else match = @options[:regexp].match(value.strip) if match.nil? nil else seconds = 0 names = match.names seconds += match['s'].to_i if names.include?('s') seconds += match['m'].to_i * 60 if names.include?('m') seconds += match['h'].to_i * 3600 if names.include?('h') Mongoid::TimeField::Value.new(seconds, @options) end end end |