Initial commit
This commit is contained in:
commit
16acc8d0de
11 changed files with 246 additions and 0 deletions
14
.gitignore
vendored
Normal file
14
.gitignore
vendored
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
/.bundle/
|
||||||
|
/.yardoc
|
||||||
|
/Gemfile.lock
|
||||||
|
/_yardoc/
|
||||||
|
/coverage/
|
||||||
|
/doc/
|
||||||
|
/pkg/
|
||||||
|
/spec/reports/
|
||||||
|
/tmp/
|
||||||
|
*.bundle
|
||||||
|
*.so
|
||||||
|
*.o
|
||||||
|
*.a
|
||||||
|
mkmf.log
|
3
Gemfile
Normal file
3
Gemfile
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
source 'https://rubygems.org'
|
||||||
|
|
||||||
|
gemspec
|
22
LICENSE.txt
Normal file
22
LICENSE.txt
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
Copyright (c) 2015 Andrew Tomaka
|
||||||
|
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
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.
|
32
README.md
Normal file
32
README.md
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
# GreatGazoo
|
||||||
|
|
||||||
|
Take of sequence of objects with a method representing time along a span of
|
||||||
|
hours and moves all times outside of that span while maintaining sequence.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Add this line to your application's Gemfile:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
gem 'great_gazoo'
|
||||||
|
```
|
||||||
|
|
||||||
|
And then execute:
|
||||||
|
|
||||||
|
$ bundle
|
||||||
|
|
||||||
|
Or install it yourself as:
|
||||||
|
|
||||||
|
$ gem install GreatGazoo
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
TODO: Write usage instructions here
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
1. Fork it ( https://github.com/[my-github-username]/GreatGazoo/fork )
|
||||||
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
||||||
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
||||||
|
4. Push to the branch (`git push origin my-new-feature`)
|
||||||
|
5. Create a new Pull Request
|
8
Rakefile
Normal file
8
Rakefile
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
require 'bundler/gem_tasks'
|
||||||
|
require 'rspec/core/rake_task'
|
||||||
|
|
||||||
|
RSpec::Core::RakeTask.new(:spec)
|
||||||
|
|
||||||
|
task default: :spec
|
||||||
|
task test: :spec
|
||||||
|
|
26
greatgazoo.gemspec
Normal file
26
greatgazoo.gemspec
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
# coding: utf-8
|
||||||
|
lib = File.expand_path('../lib', __FILE__)
|
||||||
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
||||||
|
require 'great_gazoo/version'
|
||||||
|
|
||||||
|
Gem::Specification.new do |spec|
|
||||||
|
spec.platform = Gem::Platform::RUBY
|
||||||
|
spec.name = 'greatgazoo'
|
||||||
|
spec.version = GreatGazoo::VERSION
|
||||||
|
spec.authors = ['Andrew Tomaka']
|
||||||
|
spec.email = ['atomaka@gmail.com']
|
||||||
|
spec.homepage = 'http://www.atomaka.com'
|
||||||
|
spec.summary = %q{Remap time, often causing you more even more trouble}
|
||||||
|
spec.description = %q{Takes a set of times and a black-out window to rewrite timestamps to be outside of the window while still being sequential}
|
||||||
|
spec.license = 'MIT'
|
||||||
|
|
||||||
|
spec.files = `git ls-files -z`.split("\x0")
|
||||||
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
||||||
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
||||||
|
spec.require_paths = ['lib']
|
||||||
|
|
||||||
|
spec.add_development_dependency 'bundler', '~> 1.7'
|
||||||
|
spec.add_development_dependency 'rake', '~> 10.0'
|
||||||
|
spec.add_development_dependency 'rspec', '~> 3.2.0'
|
||||||
|
spec.add_development_dependency 'pry'
|
||||||
|
end
|
5
lib/great_gazoo.rb
Normal file
5
lib/great_gazoo.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
require 'great_gazoo/version'
|
||||||
|
require 'great_gazoo/time_warp'
|
||||||
|
|
||||||
|
module GreatGazoo
|
||||||
|
end
|
45
lib/great_gazoo/time_warp.rb
Normal file
45
lib/great_gazoo/time_warp.rb
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
module GreatGazoo
|
||||||
|
class TimeWarp
|
||||||
|
def initialize(options = {})
|
||||||
|
@options = default_options.merge(options)
|
||||||
|
|
||||||
|
@start_hour, @start_minute = @options[:start_time].split(/:/).map { |i| i.to_i }
|
||||||
|
@end_hour, @end_minute = @options[:end_time].split(/:/).map { |i| i.to_i }
|
||||||
|
|
||||||
|
@time_method = @options[:time_method]
|
||||||
|
@time_set_method = "#{@options[:time_method].to_s}="
|
||||||
|
end
|
||||||
|
|
||||||
|
def bend(sequence)
|
||||||
|
sequence.each do |object|
|
||||||
|
current_time = object.send(@time_method)
|
||||||
|
if blacked_out?(current_time)
|
||||||
|
new_time = Time.new(current_time.year, current_time.month, current_time.day, current_time.hour + (@end_hour - @start_hour), current_time.min)
|
||||||
|
object.send(@time_set_method, new_time)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def blacked_out?(time)
|
||||||
|
after_blackout_start?(time) && before_blackout_end?(time)
|
||||||
|
end
|
||||||
|
|
||||||
|
def after_blackout_start?(time)
|
||||||
|
time.hour > @start_hour || (time.hour == @start_hour && time.min >= @start_minute)
|
||||||
|
end
|
||||||
|
|
||||||
|
def before_blackout_end?(time)
|
||||||
|
time.hour < @end_hour || (time.hour == @end_hour && time.min <= @end_minute)
|
||||||
|
end
|
||||||
|
|
||||||
|
def default_options
|
||||||
|
{
|
||||||
|
time_method: :time,
|
||||||
|
start_time: '8:00',
|
||||||
|
end_time: '17:00',
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
3
lib/great_gazoo/version.rb
Normal file
3
lib/great_gazoo/version.rb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
module GreatGazoo
|
||||||
|
VERSION = "0.0.1"
|
||||||
|
end
|
87
spec/lib/time_warp_spec.rb
Normal file
87
spec/lib/time_warp_spec.rb
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
Default = Struct.new(:sequence, :time)
|
||||||
|
Adjusted = Struct.new(:sequence, :start)
|
||||||
|
|
||||||
|
describe GreatGazoo do
|
||||||
|
it 'can be constructed' do
|
||||||
|
warp = GreatGazoo::TimeWarp.new
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'when no values are passed' do
|
||||||
|
let(:gg) { GreatGazoo::TimeWarp.new() }
|
||||||
|
it 'can bend' do
|
||||||
|
gg.bend(Array.new)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'can bend a sequence' do
|
||||||
|
sequence = Array.new
|
||||||
|
my_time = Time.new(2014, 1, 1, 12, 0, 0)
|
||||||
|
sequence << Default.new(1, my_time)
|
||||||
|
|
||||||
|
expect(gg.bend(sequence).first.time).to_not eq(my_time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not bend when it should not' do
|
||||||
|
sequence = Array.new
|
||||||
|
my_time = Time.new(2014, 1, 1, 6, 0, 0)
|
||||||
|
sequence << Default.new(1, my_time)
|
||||||
|
|
||||||
|
expect(gg.bend(sequence).first.time).to eq(my_time)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'when values are passed' do
|
||||||
|
describe 'with non-default time method' do
|
||||||
|
let(:gg) { GreatGazoo::TimeWarp.new({time_method: :start}) }
|
||||||
|
|
||||||
|
it 'can bend a sequence' do
|
||||||
|
sequence = Array.new
|
||||||
|
my_time = Time.new(2014, 1, 1, 12, 0, 0)
|
||||||
|
sequence << Adjusted.new(1, my_time)
|
||||||
|
|
||||||
|
expect(gg.bend(sequence).first.start).to_not eq(my_time)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'with non-default start time' do
|
||||||
|
let(:gg) { GreatGazoo::TimeWarp.new({start_time: '16:00'}) }
|
||||||
|
|
||||||
|
it 'can bend a sequence' do
|
||||||
|
sequence = Array.new
|
||||||
|
my_time = Time.new(2014, 1, 1, 16, 30, 0)
|
||||||
|
sequence << Default.new(1, my_time)
|
||||||
|
|
||||||
|
expect(gg.bend(sequence).first.time).to_not eq(my_time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not bend a sequence when it should not' do
|
||||||
|
sequence = Array.new
|
||||||
|
my_time = Time.new(2014, 1, 1, 15, 30, 0)
|
||||||
|
sequence << Default.new(1, my_time)
|
||||||
|
|
||||||
|
expect(gg.bend(sequence).first.time).to eq(my_time)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'with non-default end time' do
|
||||||
|
let(:gg) { GreatGazoo::TimeWarp.new({end_time: '10:00'}) }
|
||||||
|
|
||||||
|
it 'can bend a sequence' do
|
||||||
|
sequence = Array.new
|
||||||
|
my_time = Time.new(2014, 1, 1, 9, 30, 0)
|
||||||
|
sequence << Default.new(1, my_time)
|
||||||
|
|
||||||
|
expect(gg.bend(sequence).first.time).to_not eq(my_time)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not bend a sequence when it should not' do
|
||||||
|
sequence = Array.new
|
||||||
|
my_time = Time.new(2014, 1, 1, 10, 30, 0)
|
||||||
|
sequence << Default.new(1, my_time)
|
||||||
|
|
||||||
|
expect(gg.bend(sequence).first.time).to eq(my_time)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
1
spec/spec_helper.rb
Normal file
1
spec/spec_helper.rb
Normal file
|
@ -0,0 +1 @@
|
||||||
|
require 'great_gazoo'
|
Loading…
Reference in a new issue