elapsed_watch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 92693d56268a653f9f72c0d238452927ade8faac
4
+ data.tar.gz: 6c7c17e24ea8e3160e4d0bda8b4a8a7e72e5e51b
5
+ SHA512:
6
+ metadata.gz: 64e7b02b859c2aa01bb6df58b92bc8c1dbb523a29cf0fde3bccccc1287f41462a280eb08828fcb412162a78f68b3f87241fdd15136d1f72c19cb3e0f9ab1515b
7
+ data.tar.gz: e4d44182f1ee0f3b50184f938e616db80404535a69d15702b0f71815d26cc66f8435ce57ca2144ab63fe24302f59da29e36d4d6e1dea5c777e22c4139a59c685
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ .DS_Store
2
+ results.html
3
+ pkg
4
+ html
5
+ tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in elapsed_watch.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'guard'
8
+ gem 'guard-bundler'
9
+ gem 'guard-rspec'
10
+ gem 'guard-cucumber'
11
+ gem 'timecop'
12
+ end
13
+
data/Gemfile.lock ADDED
@@ -0,0 +1,97 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ elapsed_watch (0.0.1)
5
+ chronic_duration
6
+ methadone (~> 1.3.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ aruba (0.5.3)
12
+ childprocess (>= 0.3.6)
13
+ cucumber (>= 1.1.1)
14
+ rspec-expectations (>= 2.7.0)
15
+ builder (3.2.2)
16
+ childprocess (0.3.9)
17
+ ffi (~> 1.0, >= 1.0.11)
18
+ chronic_duration (0.10.2)
19
+ numerizer (~> 0.1.1)
20
+ coderay (1.0.9)
21
+ cucumber (1.3.8)
22
+ builder (>= 2.1.2)
23
+ diff-lcs (>= 1.1.3)
24
+ gherkin (~> 2.12.1)
25
+ multi_json (>= 1.7.5, < 2.0)
26
+ multi_test (>= 0.0.2)
27
+ diff-lcs (1.2.4)
28
+ ffi (1.9.0)
29
+ formatador (0.2.4)
30
+ gherkin (2.12.1)
31
+ multi_json (~> 1.3)
32
+ guard (1.8.2)
33
+ formatador (>= 0.2.4)
34
+ listen (>= 1.0.0)
35
+ lumberjack (>= 1.0.2)
36
+ pry (>= 0.9.10)
37
+ thor (>= 0.14.6)
38
+ guard-bundler (1.0.0)
39
+ bundler (~> 1.0)
40
+ guard (~> 1.1)
41
+ guard-cucumber (1.4.0)
42
+ cucumber (>= 1.2.0)
43
+ guard (>= 1.1.0)
44
+ guard-rspec (3.0.2)
45
+ guard (>= 1.8)
46
+ rspec (~> 2.13)
47
+ json (1.8.0)
48
+ listen (1.3.1)
49
+ rb-fsevent (>= 0.9.3)
50
+ rb-inotify (>= 0.9)
51
+ rb-kqueue (>= 0.2)
52
+ lumberjack (1.0.4)
53
+ methadone (1.3.0)
54
+ bundler
55
+ method_source (0.8.2)
56
+ multi_json (1.8.0)
57
+ multi_test (0.0.2)
58
+ numerizer (0.1.1)
59
+ pry (0.9.12.2)
60
+ coderay (~> 1.0.5)
61
+ method_source (~> 0.8)
62
+ slop (~> 3.4)
63
+ rake (0.9.6)
64
+ rb-fsevent (0.9.3)
65
+ rb-inotify (0.9.2)
66
+ ffi (>= 0.5.0)
67
+ rb-kqueue (0.2.0)
68
+ ffi (>= 0.5.0)
69
+ rdoc (4.0.1)
70
+ json (~> 1.4)
71
+ rspec (2.14.1)
72
+ rspec-core (~> 2.14.0)
73
+ rspec-expectations (~> 2.14.0)
74
+ rspec-mocks (~> 2.14.0)
75
+ rspec-core (2.14.5)
76
+ rspec-expectations (2.14.2)
77
+ diff-lcs (>= 1.1.3, < 2.0)
78
+ rspec-mocks (2.14.3)
79
+ slop (3.4.6)
80
+ thor (0.18.1)
81
+ timecop (0.6.3)
82
+
83
+ PLATFORMS
84
+ ruby
85
+
86
+ DEPENDENCIES
87
+ aruba
88
+ bundler (~> 1.3)
89
+ elapsed_watch!
90
+ guard
91
+ guard-bundler
92
+ guard-cucumber
93
+ guard-rspec
94
+ rake (~> 0.9.2)
95
+ rdoc
96
+ rspec
97
+ timecop
data/Guardfile ADDED
@@ -0,0 +1,19 @@
1
+ # -*- mode: ruby -*-
2
+ guard 'cucumber', :cli => '--color --format pretty' do
3
+ watch(%r{^features/.+\.feature$})
4
+ watch(%r{^features/support/.+$}) { 'features' }
5
+ watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
6
+ watch('bin/elapsed_watch') { 'features' }
7
+ end
8
+
9
+ guard :rspec, :cli => '--color --format documentation' do
10
+ watch(%r{^spec/.+_spec\.rb$})
11
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
12
+ watch('spec/spec_helper.rb') { "spec" }
13
+ end
14
+
15
+
16
+ guard 'bundler' do
17
+ watch('Gemfile')
18
+ watch(/^.+\.gemspec/)
19
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2013 YOUR NAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # ElapsedWatch
2
+
3
+ ElapsedWatch is a tiny command line script that will read an events
4
+ file and print out a nice, human-readable duration from or to the event.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'elapsed_watch'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install elapsed_watch
19
+
20
+ ## Usage
21
+
22
+ Prints a short help message:
23
+
24
+ $ elapsed_watch --help
25
+
26
+ Prints the elapsed time for events in `$HOME/.eventsrc`:
27
+
28
+ $ elapsed_watch
29
+
30
+ Prints the elapsed time for events in `my_events`:
31
+
32
+ $ elapsed_watch my_events
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
data/README.rdoc ADDED
@@ -0,0 +1,45 @@
1
+ = elapsed_watch - Human-readable elapsed times for events
2
+
3
+ Author:: Tamara Temple (tamouse@gmail.com)
4
+ Copyright:: Copyright (c) 2013 Tamara Temple
5
+ License:: mit, see LICENSE.txt
6
+
7
+ Reading a file of events, containing an event name (a string) and a
8
+ date-time string (yyyy-mm-dd hh:mm(:ss)), print a nice duration since
9
+ the event (if the event occurs in the past) or until the event (if the
10
+ event occurs in the future).
11
+
12
+ The impetous for this little program is my wanting to know how long it
13
+ has been since my last cigarette.
14
+
15
+ == Links
16
+
17
+ * {Source on Github}[http://github.com/tamouse/elapsed_watch]
18
+
19
+ == Install
20
+
21
+ gem install elapsed_watch
22
+
23
+ == Examples
24
+
25
+ The default events file is `$HOME/.eventsrc`. Other event files can be
26
+ given on the command line:
27
+
28
+ elapsed_watch
29
+
30
+ elapsed_watch my_events
31
+
32
+ == Events File
33
+
34
+ The events file is a regular text file with one event per line:
35
+
36
+ Event name: YYYY-MM-DD
37
+
38
+ The date spec can be anything that Time.parse can handle.
39
+
40
+ The events file is ERB-able, so you can program things if you wish.
41
+
42
+ == Contributing
43
+
44
+ See github repository for information on contributing.
45
+
data/Rakefile ADDED
@@ -0,0 +1,66 @@
1
+ def dump_load_path
2
+ puts $LOAD_PATH.join("\n")
3
+ found = nil
4
+ $LOAD_PATH.each do |path|
5
+ if File.exists?(File.join(path,"rspec"))
6
+ puts "Found rspec in #{path}"
7
+ if File.exists?(File.join(path,"rspec","core"))
8
+ puts "Found core"
9
+ if File.exists?(File.join(path,"rspec","core","rake_task"))
10
+ puts "Found rake_task"
11
+ found = path
12
+ else
13
+ puts "!! no rake_task"
14
+ end
15
+ else
16
+ puts "!!! no core"
17
+ end
18
+ end
19
+ end
20
+ if found.nil?
21
+ puts "Didn't find rspec/core/rake_task anywhere"
22
+ else
23
+ puts "Found in #{path}"
24
+ end
25
+ end
26
+ require 'bundler'
27
+ require 'rake/clean'
28
+
29
+ begin
30
+ require 'rspec/core/rake_task'
31
+ rescue LoadError
32
+ dump_load_path
33
+ raise
34
+ end
35
+
36
+ require 'cucumber'
37
+ require 'cucumber/rake/task'
38
+ gem 'rdoc' # we need the installed RDoc gem, not the system one
39
+ require 'rdoc/task'
40
+
41
+ include Rake::DSL
42
+
43
+ Bundler::GemHelper.install_tasks
44
+
45
+
46
+ RSpec::Core::RakeTask.new do |t|
47
+ # Put spec opts in a file named .rspec in root
48
+ end
49
+
50
+
51
+ CUKE_RESULTS = 'results.html'
52
+ CLEAN << CUKE_RESULTS
53
+ Cucumber::Rake::Task.new(:features) do |t|
54
+ t.cucumber_opts = "features --format html -o #{CUKE_RESULTS} --format pretty --no-source -x"
55
+ t.fork = false
56
+ end
57
+
58
+ Rake::RDocTask.new do |rd|
59
+
60
+ rd.main = "README.rdoc"
61
+
62
+ rd.rdoc_files.include("README.rdoc","lib/**/*.rb","bin/**/*")
63
+ end
64
+
65
+ task :default => [:spec,:features]
66
+
data/bin/elapsed_watch ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'methadone'
5
+ require 'elapsed_watch.rb'
6
+
7
+ class App
8
+ include Methadone::Main
9
+ include Methadone::CLILogging
10
+
11
+ main do |event_file|
12
+ puts ElapsedWatch.run(event_file, options).join("\n")
13
+ end
14
+
15
+ # supplemental methods here
16
+
17
+ # Declare command-line interface here
18
+
19
+ description ElapsedWatch::SUMMARY
20
+
21
+ # Accept flags via:
22
+ # on("--flag VAL","Some flag")
23
+ # options[flag] will contain VAL
24
+ #
25
+ # Specify switches via:
26
+ # on("--[no-]switch","Some switch")
27
+ #
28
+ # Or, just call OptionParser methods on opts
29
+ #
30
+ # Require an argument
31
+ # arg :some_arg
32
+ #
33
+ # # Make an argument optional
34
+ # arg :optional_arg, :optional
35
+
36
+ arg :event_file, :optional, "File name of the events file. Defaults to #{ElapsedWatch::DEFAULT_EVENT_FILE} if not given"
37
+
38
+ version ElapsedWatch::VERSION
39
+
40
+ use_log_level_option
41
+
42
+ go!
43
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'elapsed_watch/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "elapsed_watch"
8
+ spec.version = ElapsedWatch::VERSION
9
+ spec.authors = ["Tamara Temple"]
10
+ spec.email = ["tamouse@gmail.com"]
11
+ spec.description = ElapsedWatch::DESCRIPTION
12
+ spec.summary = ElapsedWatch::SUMMARY
13
+ spec.homepage = "http://github.com/tamouse/elapsed_watch"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency('rdoc')
24
+ spec.add_development_dependency('aruba')
25
+ spec.add_development_dependency('rake', '~> 0.9.2')
26
+ spec.add_development_dependency('rspec')
27
+ spec.add_development_dependency('timecop')
28
+
29
+ spec.add_dependency('methadone', '~> 1.3.0')
30
+ spec.add_dependency('chronic_duration')
31
+
32
+ end
@@ -0,0 +1,28 @@
1
+ Feature: My bootstrapped app kinda works
2
+ In order to get going on coding my awesome app
3
+ I want to have aruba and cucumber setup
4
+ So I don't have to do it myself
5
+
6
+ @announce
7
+ Scenario: App just runs
8
+ When I get help for "elapsed_watch"
9
+ Then the exit status should be 0
10
+ And the banner should be present
11
+ And the banner should document that this app takes options
12
+ And the following options should be documented:
13
+ |--version|
14
+ And the banner should document that this app's arguments are:
15
+ |event_file|
16
+
17
+ @announce
18
+ Scenario: Events are shown
19
+ Given a file named "my_events" with:
20
+ """
21
+ Event One: <%= (Time.now-(10*24*60*60)).strftime "%Y-%m-%d %H:%M:%S" %>
22
+ Event Two: <%= (Time.now+(10*24*60*60)).strftime "%Y-%m-%d %H:%M:%S" %>
23
+ """
24
+ When I run `elapsed_watch my_events`
25
+ Then the exit status should be 0
26
+ And the output should contain "Event One"
27
+ And the output should contain "Event Two in "
28
+
@@ -0,0 +1 @@
1
+ # Put your step definitions here
@@ -0,0 +1,16 @@
1
+ require 'aruba/cucumber'
2
+ require 'methadone/cucumber'
3
+
4
+ ENV['PATH'] = "#{File.expand_path(File.dirname(__FILE__) + '/../../bin')}#{File::PATH_SEPARATOR}#{ENV['PATH']}"
5
+ LIB_DIR = File.join(File.expand_path(File.dirname(__FILE__)),'..','..','lib')
6
+
7
+ Before do
8
+ # Using "announce" causes massive warnings on 1.9.2
9
+ @puts = true
10
+ @original_rubylib = ENV['RUBYLIB']
11
+ ENV['RUBYLIB'] = LIB_DIR + File::PATH_SEPARATOR + ENV['RUBYLIB'].to_s
12
+ end
13
+
14
+ After do
15
+ ENV['RUBYLIB'] = @original_rubylib
16
+ end
@@ -0,0 +1,30 @@
1
+ =begin rdoc
2
+
3
+ elapsed_watch.rb -- show human readable time since or until a set of events
4
+
5
+ * Time-stamp: <2013-09-13 02:14:05 tamara>
6
+ * Copyright (C) 2013 Tamara Temple Web Development
7
+ * Author: Tamara Temple <tamouse@gmail.com>
8
+ * License: MIT
9
+
10
+ =end
11
+
12
+ require "elapsed_watch/version"
13
+ require "elapsed_watch/event"
14
+ require "elapsed_watch/event_collection"
15
+
16
+ module ElapsedWatch
17
+
18
+ # Module method that runs the elapsed watcher
19
+ #
20
+ # == Inputs
21
+ # * event_file (string) - name of the event file, nil to use default
22
+ # * options (hash) - options from command line
23
+ #
24
+ # == Returns
25
+ # Array of elapsed times for events
26
+ def self.run(event_file,options={})
27
+ EventCollection.new(event_file).map(&:to_s)
28
+ end
29
+
30
+ end
@@ -0,0 +1,103 @@
1
+ require 'chronic_duration'
2
+ require 'yaml'
3
+ require 'erb'
4
+ require 'time'
5
+
6
+ module ElapsedWatch
7
+
8
+ # The format for events occuring in the past
9
+ PAST_EVENT_FORMAT = "%s %s ago"
10
+
11
+ # The format for events occuring in the future
12
+ FUTURE_EVENT_FORMAT = "%s in %s"
13
+
14
+ =begin rdoc
15
+
16
+ event.rb -- Class for holding events
17
+
18
+ * Time-stamp: <2013-09-13 01:42:22 tamara>
19
+ * Copyright (C) 2013 Tamara Temple Web Development
20
+ * Author: Tamara Temple <tamouse@gmail.com>
21
+ * License: MIT
22
+
23
+ == Description
24
+
25
+ Defines an object class that deals with individual events.
26
+
27
+ Events have a name and a time.
28
+
29
+ =end
30
+
31
+ class Event
32
+
33
+ # Instantiates a new Event object.
34
+ #
35
+ # The event_string consists of a tiny piece of YAML that defines
36
+ # the event. The format is:
37
+ #
38
+ # Event Name: YYYY-MM-DD HH:MM (anything parsable by Time.parse)
39
+ def initialize(event_string)
40
+ raise "event_string is empty" if event_string.nil? or event_string.empty?
41
+ ev_yaml = YAML.load(ERB.new(event_string).result)
42
+ @event_name = ev_yaml.keys.first
43
+ @event_time = ev_yaml.values.first
44
+ raise "No event time spec: #{event_string}" if @event_time.nil?
45
+ case @event_time
46
+ when String
47
+ raise "No event time spec: #{event_string}" if @event_time.empty?
48
+ @event_time = Time.parse(@event_time)
49
+ when Date
50
+ @event_time = @event_time.to_time
51
+ when DateTime
52
+ @event_time = @event_time.to_time
53
+ when Integer
54
+ @event_time = Time.new @event_time
55
+ when nil
56
+
57
+ end
58
+ end
59
+
60
+ # Returns a formatted string for the event
61
+ def to_s()
62
+ format() % [@event_name, duration_string()]
63
+ end
64
+
65
+ alias :duration :to_s
66
+
67
+ # Returns a nicely formatted string of the duration between the
68
+ # event and now
69
+ def duration_string()
70
+ ChronicDuration.output(time_diff())
71
+ end
72
+
73
+ # Calculates the absolute value of the difference between the
74
+ # event and now
75
+ def time_diff()
76
+ (now() - @event_time).to_i.abs
77
+ end
78
+
79
+ # Returns the format string to use based on whether the event is
80
+ # in the past or the future.
81
+ def format()
82
+ if now() >= @event_time
83
+ PAST_EVENT_FORMAT
84
+ else
85
+ FUTURE_EVENT_FORMAT
86
+ end
87
+ end
88
+
89
+ # Parse an event sting and return an event object
90
+ def self.parse(event_string)
91
+ self.new(event_string)
92
+ end
93
+
94
+ private
95
+
96
+ # Memoizes now
97
+ def now(use_time=nil)
98
+ @now ||= (use_time.nil? ? Time.now : use_time)
99
+ end
100
+
101
+ end
102
+
103
+ end
@@ -0,0 +1,46 @@
1
+ module ElapsedWatch
2
+
3
+ DEFAULT_EVENT_FILE = File.join(ENV['HOME'],'.eventsrc')
4
+
5
+ =begin rdoc
6
+
7
+ event_collection.rb -- a collection of events to process
8
+
9
+ * Time-stamp: <2013-09-13 01:44:46 tamara>
10
+ * Copyright (C) 2013 Tamara Temple Web Development
11
+ * Author: Tamara Temple <tamouse@gmail.com>
12
+ * License: MIT
13
+
14
+ =end
15
+
16
+ # The event collection holds all the events to watch.
17
+ class EventCollection < Array
18
+
19
+ # Set up the EventCollection by passing in a file name that
20
+ # contains the events. If omitted or nil, the default file name at
21
+ # $HOME/.eventsrc will be used.
22
+ def initialize(event_file_name=nil)
23
+ self.event_file = event_file_name ||= DEFAULT_EVENT_FILE
24
+ end
25
+
26
+ # Get the event file name
27
+ def event_file()
28
+ @event_file
29
+ end
30
+
31
+ # Assign the event file name and reload the contents of the file
32
+ def event_file=(fn)
33
+ @event_file=fn
34
+ self.reload()
35
+ end
36
+
37
+ # Reload the events from the event file. Existing events are
38
+ # deleted first.
39
+ def reload()
40
+ self.clear
41
+ self.concat File.read(event_file).split(/\r?\n/).map{|e| Event.new(e)}
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,16 @@
1
+ module ElapsedWatch
2
+ VERSION = "0.0.1"
3
+
4
+ DESCRIPTION = "
5
+ Reading a file of events, containing an event name (a string) and a
6
+ date-time string (yyyy-mm-dd hh:mm(:ss)), print a nice duration since
7
+ the event (if the event occurs in the past) or until the event (if the
8
+ event occurs in the future).
9
+
10
+ The impetous for this little program is my wanting to know how long it
11
+ has been since my last cigarette.
12
+ "
13
+
14
+ SUMMARY = "Print elapsed time since or until an event in nice human-readable form"
15
+
16
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe ElapsedWatch do
4
+ it "should have a version number" do
5
+ ElapsedWatch::VERSION.should_not be_nil
6
+ end
7
+ it {should respond_to :run}
8
+ context "running the ElapsedWatch" do
9
+ before(:each) do
10
+ File.stub(:read => "Event One: 2001-01-01\nEvent Two: 2013-12-31")
11
+ end
12
+ let(:results) {ElapsedWatch.run(nil)}
13
+ it {results.should be_an Array}
14
+ it {results.size.should eq 2}
15
+ it {results[0].should be_a String}
16
+ it {results[0].should match /^Event One/}
17
+ it {results[1].should be_a String}
18
+ it {results[1].should match /^Event Two/}
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe ElapsedWatch do
4
+
5
+ context "file handling" do
6
+ context "should use the default file" do
7
+ before(:each) do
8
+ File.should_receive(:read).with(ElapsedWatch::DEFAULT_EVENT_FILE).and_return("Event: 2013-09-01")
9
+ end
10
+ let(:events) {ElapsedWatch::EventCollection.new()}
11
+ it{events.should_not be_nil}
12
+ it{events.size.should be 1}
13
+ it{events.first.instance_variable_get("@event_name").should eq "Event"}
14
+ it{events.first.instance_variable_get("@event_time").should eq Time.new(2013,9,1)}
15
+ end
16
+ context "should use the given file" do
17
+ before(:each) do
18
+ File.should_receive(:read).with("events").and_return("Alt Event: 2013-09-01")
19
+ end
20
+ let(:events) { ElapsedWatch::EventCollection.new("events") }
21
+ it {events.should_not be_nil}
22
+ it {events.size.should eq 1}
23
+ it {events.first.instance_variable_get("@event_name").should eq "Alt Event"}
24
+ end
25
+
26
+ end
27
+
28
+ context "processing events" do
29
+ before(:each) do
30
+ File.stub(:read).and_return("Past Event: 2013-01-25\nFuture Event: 2015-04-15")
31
+ end
32
+
33
+ let(:events) {ElapsedWatch::EventCollection.new()}
34
+ let(:output) do
35
+ Timecop.freeze(Time.new(2013,6,1)) do
36
+ events.map(&:to_s)
37
+ end
38
+ end
39
+
40
+ it {events.size.should eq 2}
41
+
42
+ it {events.first.should be_an ElapsedWatch::Event}
43
+ it {events.first.instance_variable_get("@event_name").should eq "Past Event"}
44
+ it {events.first.instance_variable_get("@event_time").should eq Time.new(2013,1,25)}
45
+
46
+ it {events.last.should be_an ElapsedWatch::Event}
47
+ it {events.last.instance_variable_get("@event_name").should eq "Future Event"}
48
+ it {events.last.instance_variable_get("@event_time").should eq Time.new(2015,4,15)}
49
+
50
+ it {output.size.should eq 2}
51
+ it "should have correct output" do
52
+ output.should eq ["Past Event 4 mos 6 days 23 hrs ago", "Future Event in 1 yr 10 mos 18 days"]
53
+ end
54
+
55
+ end
56
+
57
+ context "reloading events" do
58
+ it "should reload events when a file name is assigned" do
59
+ File.stub(:read => "Event: 2001-01-01")
60
+ events = ElapsedWatch::EventCollection.new()
61
+ events.clear
62
+ events.size.should be_zero
63
+ events.event_file="dummy"
64
+ events.size.should eq 1
65
+ events.first.instance_variable_get("@event_name").should eq "Event"
66
+ events.first.instance_variable_get("@event_time").should eq Time.new 2001, 1, 1
67
+ end
68
+
69
+ end
70
+
71
+
72
+ end
73
+
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe ElapsedWatch do
4
+ let(:event1) {ElapsedWatch::Event.new("First of September: 2013-09-01")}
5
+ it{event1.instance_variable_get("@event_name").should eq "First of September"}
6
+ it{event1.instance_variable_get("@event_time").should eq Time.new(2013,9,1)}
7
+
8
+ it "should test elapsed time from event" do
9
+ Timecop.freeze(Time.new(2013,9,2)) do
10
+ puts "debug in #{__FILE__} at #{__LINE__} : Time.now == #{Time.now}"
11
+ event1.to_s.should eq "First of September 1 day ago"
12
+ end
13
+ end
14
+
15
+ it "should test elapsed time until event" do
16
+ Timecop.freeze(Time.new(2013,8,31)) do
17
+ puts "debug in #{__FILE__} at #{__LINE__} : Time.now == #{Time.now}"
18
+ event1.duration.should eq "First of September in 1 day"
19
+ end
20
+ end
21
+
22
+ context "events with times" do
23
+ let(:event2) {ElapsedWatch::Event.new("Quit Smoking: 2013-09-07 17:00")}
24
+ it{event2.instance_variable_get("@event_name").should eq "Quit Smoking"}
25
+ it{event2.instance_variable_get("@event_time").should eq Time.new(2013,9,7,17)}
26
+ it "should show elapsed time since event" do
27
+ Timecop.freeze(Time.new(2013,9,16,20,22,3)) do
28
+ event2.to_s.should eq "Quit Smoking 9 days 3 hrs 22 mins 3 secs ago"
29
+ end
30
+ end
31
+ it "should show elapsed time for one year from event" do
32
+ Timecop.freeze(Time.new(2014,9,7,17)) do
33
+ event2.to_s.should eq "Quit Smoking 1 yr ago"
34
+ end
35
+ end
36
+ end
37
+
38
+ context "events with only year given" do
39
+ let(:event3) {ElapsedWatch::Event.new("Birth Year: 1957")}
40
+ it{event3.instance_variable_get("@event_name").should eq "Birth Year"}
41
+ it{event3.instance_variable_get("@event_time").should eq Time.new(1957)}
42
+ end
43
+
44
+ context "sad paths" do
45
+ it "event without a day should fail" do
46
+ expect {ElapsedWatch::Event.new("No day: 2013-09")}.to raise_error ArgumentError
47
+ end
48
+ it "event without a date should fail" do
49
+ expect {ElapsedWatch::Event.new("No date:")}.to raise_error RuntimeError
50
+ end
51
+ it "event with empty string for date spec should fail" do
52
+ expect {ElapsedWatch::Event.new("Empty string: ''")}.to raise_error RuntimeError
53
+ end
54
+ it "event with empty name should fail" do
55
+ expect {ElapsedWatch::Event.new(": 2013-09-01")}.to raise_error Psych::SyntaxError
56
+ end
57
+ it "event with unparsable time spec should fail" do
58
+ expect {ElapsedWatch::Event.new("Bad spec: another fine mess")}.to raise_error ArgumentError
59
+ end
60
+
61
+
62
+ end
63
+
64
+ context "class method parse" do
65
+ it{ElapsedWatch::Event.should respond_to :parse}
66
+ context "should return an ElapsedWatch::Event object" do
67
+ let(:event) {ElapsedWatch::Event.parse "Parsed event: 2026-10-12" }
68
+ it {event.should be_a ElapsedWatch::Event}
69
+ it {event.instance_variable_get("@event_name").should eq "Parsed event"}
70
+ it {event.instance_variable_get("@event_time").should eq Time.new 2026,10,12}
71
+ end
72
+
73
+ end
74
+
75
+
76
+
77
+ end
@@ -0,0 +1,20 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ # config.order = 'random'
17
+ end
18
+
19
+ require 'elapsed_watch'
20
+ require 'timecop'
metadata ADDED
@@ -0,0 +1,208 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elapsed_watch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tamara Temple
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: aruba
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: timecop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: methadone
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 1.3.0
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: 1.3.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: chronic_duration
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: |2
140
+
141
+ Reading a file of events, containing an event name (a string) and a
142
+ date-time string (yyyy-mm-dd hh:mm(:ss)), print a nice duration since
143
+ the event (if the event occurs in the past) or until the event (if the
144
+ event occurs in the future).
145
+
146
+ The impetous for this little program is my wanting to know how long it
147
+ has been since my last cigarette.
148
+ email:
149
+ - tamouse@gmail.com
150
+ executables:
151
+ - elapsed_watch
152
+ extensions: []
153
+ extra_rdoc_files: []
154
+ files:
155
+ - .gitignore
156
+ - .rspec
157
+ - Gemfile
158
+ - Gemfile.lock
159
+ - Guardfile
160
+ - LICENSE.txt
161
+ - README.md
162
+ - README.rdoc
163
+ - Rakefile
164
+ - bin/elapsed_watch
165
+ - elapsed_watch.gemspec
166
+ - features/elapsed_watch.feature
167
+ - features/step_definitions/elapsed_watch_steps.rb
168
+ - features/support/env.rb
169
+ - lib/elapsed_watch.rb
170
+ - lib/elapsed_watch/event.rb
171
+ - lib/elapsed_watch/event_collection.rb
172
+ - lib/elapsed_watch/version.rb
173
+ - spec/lib/elapse_watch_spec.rb
174
+ - spec/lib/elapsed_watch/event_collection_spec.rb
175
+ - spec/lib/elapsed_watch/event_spec.rb
176
+ - spec/spec_helper.rb
177
+ homepage: http://github.com/tamouse/elapsed_watch
178
+ licenses:
179
+ - MIT
180
+ metadata: {}
181
+ post_install_message:
182
+ rdoc_options: []
183
+ require_paths:
184
+ - lib
185
+ required_ruby_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ required_rubygems_version: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - '>='
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ requirements: []
196
+ rubyforge_project:
197
+ rubygems_version: 2.0.3
198
+ signing_key:
199
+ specification_version: 4
200
+ summary: Print elapsed time since or until an event in nice human-readable form
201
+ test_files:
202
+ - features/elapsed_watch.feature
203
+ - features/step_definitions/elapsed_watch_steps.rb
204
+ - features/support/env.rb
205
+ - spec/lib/elapse_watch_spec.rb
206
+ - spec/lib/elapsed_watch/event_collection_spec.rb
207
+ - spec/lib/elapsed_watch/event_spec.rb
208
+ - spec/spec_helper.rb