time_keeper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ gem "activemodel", ">= 3.0.0", "< 3.1.0"
9
+ gem "activesupport", ">=3.0.0", "< 3.1.0"
10
+
11
+ group :development do
12
+ gem "rspec", "~> 2.0.0"
13
+ gem "mocha"
14
+ gem "bundler", "~> 1.0.0"
15
+ gem "jeweler", "~> 1.6.4"
16
+ gem "rcov", ">= 0"
17
+ gem "ruby-debug"
18
+ end
@@ -0,0 +1,53 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.11)
5
+ activesupport (= 3.0.11)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.5.0)
8
+ activesupport (3.0.11)
9
+ builder (2.1.2)
10
+ columnize (0.3.6)
11
+ diff-lcs (1.1.3)
12
+ git (1.2.5)
13
+ i18n (0.5.0)
14
+ jeweler (1.6.4)
15
+ bundler (~> 1.0)
16
+ git (>= 1.2.5)
17
+ rake
18
+ linecache (0.46)
19
+ rbx-require-relative (> 0.0.4)
20
+ metaclass (0.0.1)
21
+ mocha (0.10.0)
22
+ metaclass (~> 0.0.1)
23
+ rake (0.9.2.2)
24
+ rbx-require-relative (0.0.5)
25
+ rcov (0.9.11)
26
+ rspec (2.0.1)
27
+ rspec-core (~> 2.0.1)
28
+ rspec-expectations (~> 2.0.1)
29
+ rspec-mocks (~> 2.0.1)
30
+ rspec-core (2.0.1)
31
+ rspec-expectations (2.0.1)
32
+ diff-lcs (>= 1.1.2)
33
+ rspec-mocks (2.0.1)
34
+ rspec-core (~> 2.0.1)
35
+ rspec-expectations (~> 2.0.1)
36
+ ruby-debug (0.10.4)
37
+ columnize (>= 0.1)
38
+ ruby-debug-base (~> 0.10.4.0)
39
+ ruby-debug-base (0.10.4)
40
+ linecache (>= 0.3)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ activemodel (>= 3.0.0, < 3.1.0)
47
+ activesupport (>= 3.0.0, < 3.1.0)
48
+ bundler (~> 1.0.0)
49
+ jeweler (~> 1.6.4)
50
+ mocha
51
+ rcov
52
+ rspec (~> 2.0.0)
53
+ ruby-debug
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Suman Mukherjee
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = time_keeper
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to time_keeper
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Suman Mukherjee. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "time_keeper"
18
+ gem.homepage = "http://github.com/sumanmukherjee03/time_keeper"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Define time without dates as strings, perform operations on time spans and collection of time spans.}
21
+ gem.description = %Q{A simple gem to pass time in string format without dates and perform various useful operations on it. This has been derived from an app.}
22
+ gem.email = "sumanmukherjee03@gmail.com"
23
+ gem.authors = ["Suman Mukherjee"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "time_keeper #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'active_model'
3
+ module TimeKeeper
4
+ end
5
+ Dir["#{File.dirname(__FILE__)}/time_keeper/**/*.rb"].each do |name|
6
+ require name
7
+ end
@@ -0,0 +1,39 @@
1
+ class TimeKeeper::CustomTime
2
+ attr_accessor :time_in_str
3
+
4
+ TIME_FORMAT_REGEX = /^((\d){1}|(\d){2})((:(\d){2}){1})?([ ])*(am|pm)?$/i
5
+
6
+ include ActiveModel::Validations
7
+
8
+ validates :time_in_str, :presence => true, :format => {:with => TimeKeeper::CustomTime::TIME_FORMAT_REGEX}
9
+
10
+ def initialize(time_in_str)
11
+ @time_in_str = time_in_str.downcase.strip
12
+ end
13
+
14
+ def parse
15
+ @parsed_time = TimeKeeper::Parser::Base.build(@time_in_str).parse
16
+ self
17
+ end
18
+
19
+ def in_seconds
20
+ raise "Please parse the time before extracting in seconds" unless @parsed_time
21
+ @parsed_time.hour * 3600 + @parsed_time.min * 60
22
+ end
23
+
24
+ def in_minutes
25
+ raise "Please parse the time before extracting in minutes" unless @parsed_time
26
+ @parsed_time.hour * 60 + @parsed_time.min
27
+ end
28
+
29
+ def in_hours
30
+ raise "Please parse the time before extracting in hours" unless @parsed_time
31
+ @parsed_time.hour + ((@parsed_time.min.to_f / 60) * 100).round.to_f/100
32
+ end
33
+
34
+ def to_s
35
+ h = (self.in_minutes.to_i / 60)
36
+ m = (self.in_minutes.to_i % 60)
37
+ "%.2d:%.2d" % [h,m]
38
+ end
39
+ end
@@ -0,0 +1,7 @@
1
+ module TimeKeeper
2
+ module Parser
3
+ end
4
+ end
5
+ Dir["#{File.dirname(__FILE__)}/time_keeper/**/*.rb"].each do |name|
6
+ require name
7
+ end
@@ -0,0 +1,50 @@
1
+ module TimeKeeper
2
+ module Parser
3
+ class Base
4
+ attr_accessor :time_str, :hour, :min
5
+
6
+ TIME_FORMAT_REGEX = /^((\d){1}|(\d){2})((:(\d){2}){1})?([ ])*(am|pm)?$/i
7
+
8
+ include ActiveModel::Validations
9
+
10
+ validates :time_str, :presence => true, :format => {:with => TimeKeeper::Parser::Base::TIME_FORMAT_REGEX}
11
+
12
+ validate :time_is_valid
13
+
14
+ def initialize(time_str)
15
+ @time_str = time_str
16
+ raise "#{time_str} is not a valid time" unless self.valid?
17
+ end
18
+
19
+ def self.build(time_in_str)
20
+ if self.meridian_format?(time_in_str)
21
+ TimeKeeper::Parser::Meridian.new(time_in_str)
22
+ else
23
+ TimeKeeper::Parser::Military.new(time_in_str)
24
+ end
25
+ end
26
+
27
+ def parse
28
+ raise NotImplementedError
29
+ end
30
+
31
+ def self.meridian_format?(time_in_str)
32
+ time_in_str.downcase =~ /(am|pm)/
33
+ end
34
+
35
+ private
36
+
37
+ def time_is_valid
38
+ raise NotImplementedError
39
+ end
40
+
41
+ def extract_hour
42
+ raise NotImplementedError
43
+ end
44
+
45
+ def extract_min(min_of_day)
46
+ @min = min_of_day.blank? ? 0 : min_of_day[1..-1].strip.to_i
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,40 @@
1
+ module TimeKeeper
2
+ module Parser
3
+ class Meridian < Base
4
+ TIME_FORMAT_REGEX = /^((\d){1}|(\d){2})((:(\d){2}){1})?([ ])*(am|pm)$/i
5
+
6
+ validates :time_str, :presence => true, :format => {:with => TimeKeeper::Parser::Meridian::TIME_FORMAT_REGEX}
7
+
8
+ def parse
9
+ matched_data = @time_str.match(TimeKeeper::Parser::Meridian::TIME_FORMAT_REGEX)
10
+ extract_hour(matched_data[1], matched_data[8])
11
+ extract_min(matched_data[4])
12
+ self
13
+ end
14
+
15
+ private
16
+
17
+ def time_is_valid
18
+ matched_data = @time_str.match(TimeKeeper::Parser::Meridian::TIME_FORMAT_REGEX)
19
+ valid = ((1..12) === matched_data[1].to_i && (0...60) === matched_data[4].gsub(':', '').to_i)
20
+ self.errors.add(:base, "Either hour or min is not valid") unless valid
21
+ valid
22
+ end
23
+
24
+ def extract_hour(hour_of_day, meridian_status)
25
+ hour_of_day = hour_of_day.strip.to_i
26
+ @hour = 0
27
+ if hour_of_day == 12 && meridian_status == "am"
28
+ @hour = 0
29
+ elsif hour_of_day == 12 && meridian_status == "pm"
30
+ @hour = 12
31
+ elsif hour_of_day > 0 && hour_of_day <= 12 && meridian_status == "am"
32
+ @hour = hour_of_day
33
+ elsif hour_of_day > 0 && hour_of_day <= 12 && meridian_status == "pm"
34
+ @hour = hour_of_day + 12
35
+ end
36
+ @hour
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,29 @@
1
+ module TimeKeeper
2
+ module Parser
3
+ class Military < Base
4
+ TIME_FORMAT_REGEX = /^((\d){1}|(\d){2})((:(\d){2}){1})?$/
5
+
6
+ validates :time_str, :presence => true, :format => {:with => TimeKeeper::Parser::Military::TIME_FORMAT_REGEX}
7
+
8
+ def parse
9
+ matched_data = @time_str.match(TimeKeeper::Parser::Military::TIME_FORMAT_REGEX)
10
+ extract_hour(matched_data[1])
11
+ extract_min(matched_data[4])
12
+ self
13
+ end
14
+
15
+ private
16
+
17
+ def time_is_valid
18
+ matched_data = @time_str.match(TimeKeeper::Parser::Military::TIME_FORMAT_REGEX)
19
+ valid = ((0...24) === matched_data[1].to_i && (0...60) === matched_data[4].gsub(':', '').to_i)
20
+ self.errors.add(:base, "Either hour or min is not valid") unless valid
21
+ valid
22
+ end
23
+
24
+ def extract_hour(hour_of_day)
25
+ @hour = (hour_of_day.strip.to_i || 0)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ module TimeKeeper
2
+ module StringMinutesSupport
3
+ def to_s_helper(minutes)
4
+ return nil if minutes.nil?
5
+ h = minutes.to_i / 60
6
+ m = minutes.to_i % 60
7
+ "%.2d:%.2d" % [h,m]
8
+ end
9
+
10
+ def to_i_helper(time)
11
+ return nil if time.blank?
12
+ time = time.to_s
13
+ h,m = time.split(":")
14
+ m ||= "0"
15
+ raise ArgumentError, "#{time.inspect} is invalid" unless h =~ /^\d+$/ && m =~ /^\d+$/
16
+ (h.to_i*60) + m.to_i
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ class TimeKeeper::TimeRange
2
+ TIME_FORMAT_REGEX = /^([0-9]){1,2}(:[0-9]{2})?([ ])*(am|pm)?([ ])*(to|-)([ ])*([0-9]){1,2}(:[0-9]{2})?([ ])*(am|pm)?$/i
3
+
4
+ attr_accessor :range, :start_time, :end_time, :start_day, :end_day
5
+
6
+ include ActiveModel::Validations
7
+
8
+ validates :range, :presence => true, :format => {:with => TimeKeeper::TimeRange::TIME_FORMAT_REGEX}
9
+
10
+ def initialize(range)
11
+ @range = range.strip.downcase.gsub(/\s*(-|to)\s*/, '-')
12
+ end
13
+
14
+ def parse
15
+ raise "The range data (#{range.inspect}) you have provided is invalid" unless self.valid?
16
+ parsed_range = @range.gsub('to', '-').split('-').map(&:strip)
17
+ @start_time = TimeKeeper::CustomTime.new(parsed_range.first).parse
18
+ @end_time = TimeKeeper::CustomTime.new(parsed_range.last).parse
19
+ @start_day = 0
20
+ @end_day = days_elapsed
21
+ self
22
+ end
23
+
24
+ def days_elapsed
25
+ @start_time.in_seconds > @end_time.in_seconds ? 1 : 0
26
+ end
27
+
28
+ def duration_in_seconds
29
+ (days_elapsed * 24 * 60 * 60) + (@end_time.in_seconds - @start_time.in_seconds)
30
+ end
31
+
32
+ def duration_in_minutes
33
+ ((duration_in_seconds.to_f/60)*100).round.to_f/100
34
+ end
35
+
36
+ def duration_in_hours
37
+ ((duration_in_seconds.to_f/3600)*100).round.to_f/100
38
+ end
39
+ end
@@ -0,0 +1,29 @@
1
+ class TimeKeeper::TimeRangeCollection
2
+ TIME_FORMAT_REGEX = /^(\s*(to|-)\s*)+$/
3
+
4
+ attr_accessor :time_ranges_str, :time_ranges
5
+
6
+ include ActiveModel::Validations
7
+
8
+ validates :time_ranges_str, :presence => true, :format => {:with => TimeKeeper::TimeRangeCollection::TIME_FORMAT_REGEX}
9
+
10
+ def initialize(time_ranges_str)
11
+ @time_ranges_str = time_ranges_str.strip.gsub(/\s*(-|to)\s*/, "-")
12
+ end
13
+
14
+ def parse
15
+ spans = @time_ranges_str.split(/\s*\n\s*/).map(&:strip)
16
+ previous_time_range = nil
17
+ @time_ranges = spans.inject([]) do |result, span|
18
+ time_range = TimeKeeper::TimeRange.new(span.strip).parse
19
+ if previous_time_range
20
+ time_range.start_day = (time_range.start_time.in_seconds < previous_time_range.end_time.in_seconds ? previous_time_range.end_day + 1 : previous_time_range.end_day)
21
+ time_range.end_day = time_range.start_day + time_range.days_elapsed
22
+ end
23
+ result << time_range
24
+ previous_time_range = time_range
25
+ result
26
+ end
27
+ self
28
+ end
29
+ end