From 16acc8d0dea845fa5fa96d97735b0facadf54b01 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Sat, 16 May 2015 12:49:38 -0400 Subject: [PATCH] Initial commit --- .gitignore | 14 ++++++ Gemfile | 3 ++ LICENSE.txt | 22 +++++++++ README.md | 32 +++++++++++++ Rakefile | 8 ++++ greatgazoo.gemspec | 26 +++++++++++ lib/great_gazoo.rb | 5 +++ lib/great_gazoo/time_warp.rb | 45 +++++++++++++++++++ lib/great_gazoo/version.rb | 3 ++ spec/lib/time_warp_spec.rb | 87 ++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 1 + 11 files changed, 246 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 greatgazoo.gemspec create mode 100644 lib/great_gazoo.rb create mode 100644 lib/great_gazoo/time_warp.rb create mode 100644 lib/great_gazoo/version.rb create mode 100644 spec/lib/time_warp_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3fdc2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +/.bundle/ +/.yardoc +/Gemfile.lock +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ +*.bundle +*.so +*.o +*.a +mkmf.log diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..fa75df1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..cc18553 --- /dev/null +++ b/LICENSE.txt @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..f64da81 --- /dev/null +++ b/README.md @@ -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 diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..713d2b7 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +require 'bundler/gem_tasks' +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) + +task default: :spec +task test: :spec + diff --git a/greatgazoo.gemspec b/greatgazoo.gemspec new file mode 100644 index 0000000..d00069d --- /dev/null +++ b/greatgazoo.gemspec @@ -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 diff --git a/lib/great_gazoo.rb b/lib/great_gazoo.rb new file mode 100644 index 0000000..f7787c1 --- /dev/null +++ b/lib/great_gazoo.rb @@ -0,0 +1,5 @@ +require 'great_gazoo/version' +require 'great_gazoo/time_warp' + +module GreatGazoo +end diff --git a/lib/great_gazoo/time_warp.rb b/lib/great_gazoo/time_warp.rb new file mode 100644 index 0000000..7e1203d --- /dev/null +++ b/lib/great_gazoo/time_warp.rb @@ -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 diff --git a/lib/great_gazoo/version.rb b/lib/great_gazoo/version.rb new file mode 100644 index 0000000..38a7f20 --- /dev/null +++ b/lib/great_gazoo/version.rb @@ -0,0 +1,3 @@ +module GreatGazoo + VERSION = "0.0.1" +end diff --git a/spec/lib/time_warp_spec.rb b/spec/lib/time_warp_spec.rb new file mode 100644 index 0000000..1ef4714 --- /dev/null +++ b/spec/lib/time_warp_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..d16a586 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1 @@ +require 'great_gazoo'