Module: Qreport::TimeParser::TimeUnit
- Includes:
- Comparable
- Included in:
- TimeInterval, TimeRelative, TimeWithUnit
- Defined in:
- lib/qreport/time_parser/time_unit.rb
Constant Summary collapse
- UNIT_REGEXP =
(@@unit_interval.keys + @@unit_alias.keys + @@unit_alias.values). uniq. map(&:to_s). reject(&:empty?). sort_by(&:size) * '|'
- @@unit_alias =
{ :second => :sec, :s => :sec, :minute => :min, :min => :min, :m => :min, :hr => :hour, :h => :hour, :d => :day, :w => :week, :mo => :mon, :month => :mon, :mth => :mon, :yr => :year, :y => :year, }
- @@unit_interval =
in seconds
{ nil => 1, :sec => 1, :min => 60, :hour => 60 * 60, :day => 60 * 60 * 24, # + 1 leap second :week => 60 * 60 * 24 * 7, :mon => (60 * 60 * 24) * 31, :year => (60 * 60 * 24) * 365, # + 1 leap day :decade => (60 * 60 * 24) * 3650, :century => (60 * 60 * 24) * 36500, :millenium => (60 * 60 * 24) * 365000, }
Instance Attribute Summary collapse
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(x) ⇒ Object
- #new(*args) ⇒ Object
- #normalize! ⇒ Object
-
#unit_interval ⇒ Object
$stderr.puts “UNIT_REGEXP = #UNIT_REGEXP”.
- #unit_multiplier(unit = @unit) ⇒ Object
Instance Attribute Details
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
7 8 9 |
# File 'lib/qreport/time_parser/time_unit.rb', line 7 def unit @unit end |
Class Method Details
.pluralize(str) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/qreport/time_parser/time_unit.rb', line 61 def self.pluralize str str = str.to_s case str when "day" "days" when /y\Z/ str.sub(/y\Z/, 'ies') else str + "s" end end |
Instance Method Details
#<=>(x) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/qreport/time_parser/time_unit.rb', line 99 def <=> x case x when TimeInterval unit_interval <=> x.unit_interval else unit_interval <=> x end end |
#new(*args) ⇒ Object
9 10 11 |
# File 'lib/qreport/time_parser/time_unit.rb', line 9 def new *args self.class.new *args end |
#normalize! ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/qreport/time_parser/time_unit.rb', line 13 def normalize! case @unit when nil when Symbol @unit = @@unit_alias[@unit] || @unit when String @unit = @unit.downcase.to_sym @unit = @@unit_alias[@unit] || @unit when TimeInterval, TimeWithUnit @unit = @unit.unit else raise ArgumentError, @unit.inspect end self end |
#unit_interval ⇒ Object
$stderr.puts “UNIT_REGEXP = #UNIT_REGEXP”
94 95 96 97 |
# File 'lib/qreport/time_parser/time_unit.rb', line 94 def unit_interval @unit_interval ||= TimeInterval.new(1, @unit) end |
#unit_multiplier(unit = @unit) ⇒ Object
80 81 82 |
# File 'lib/qreport/time_parser/time_unit.rb', line 80 def unit_multiplier unit = @unit @@unit_interval[unit] || 1 end |