gadgeto 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.3
4
+ - ruby-head
5
+
6
+ notifications:
7
+ email:
8
+ - rgogolok@avarteq.de
9
+ - mzirnstein@avarteq.de
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "jeweler", "~> 1.6.4"
5
+
6
+ gem 'rake'
7
+ gem 'simplecov', :require => false
8
+ gem 'rspec'
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Robert Gogolok, Matthias Zirnstein
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.
data/README.rdoc ADDED
@@ -0,0 +1,69 @@
1
+ = Gadgeto
2
+
3
+ Collection of ruby code snippets.
4
+
5
+ {<img src="https://secure.travis-ci.org/avarteqgmbh/gadgeto.png" />}[http://travis-ci.org/avarteqgmbh/gadgeto]
6
+
7
+ == Gadgeto::Dslable
8
+
9
+ class Foo
10
+ include Gadgeto::Dslable
11
+ include Gadgeto::Dslable::Display
12
+
13
+ dslable_method :item, :key, '*arguments'
14
+
15
+
16
+ def inspect
17
+ attributes[:key]
18
+ end
19
+ end
20
+
21
+ f = Foo.new
22
+
23
+
24
+ f.draw do
25
+ item 'home', :baem => :bum do
26
+ item 'terms'
27
+ item 'imprint'
28
+ end
29
+
30
+ item 'products' do
31
+ item 'kitchen' do
32
+ item 'forks'
33
+ end
34
+ end
35
+ end
36
+
37
+ f.display :items
38
+ f.items[0].attributes[:key] #=> "home"
39
+
40
+ == Gadgeto::Email
41
+
42
+ Gadgeto::Email.valid?("user@example.com") #=> true
43
+ Gadgeto::Email.valid?("user+name@example.com") #=> true
44
+
45
+ == Gadgeto::TimeOfDay
46
+
47
+ t = Gadgeto::TimeOfDay.new("08:30") #=> 08:00
48
+ t.hour #=> 8
49
+ t.minute #=> 30
50
+ t.to_i #=> 480
51
+ t.add_minutes(30) #=> 09:00
52
+
53
+ t1 = Gadgeto::TimeOfDay.new("08:30") #=> 08:30
54
+ t2 = Gadgeto::TimeOfDay.new("09:30") #=> 09:30
55
+ t1 < t2 #=> true
56
+ t1 > t2 #=> false
57
+ t1.minutes_till(t2) #=> 60
58
+ t1 == t2 #=> false
59
+
60
+ == Gadgeto::SanitizeFilename (Module)
61
+
62
+ obj = Object.new
63
+ obj.extend(Gadgeto::SanitizeFilename)
64
+ obj.sanitize_filename("foo bar.zip") #=> "foo_bar.zip"
65
+
66
+ == Copyright
67
+
68
+ Copyright (c) 2012 Robert Gogolok, Matthias Zirnstein. See LICENSE.txt for
69
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,42 @@
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 'rspec/core/rake_task'
15
+ RSpec::Core::RakeTask.new(:spec)
16
+
17
+ require 'jeweler'
18
+ Jeweler::Tasks.new do |gem|
19
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
20
+ gem.name = "gadgeto"
21
+ gem.homepage = "http://github.com/avarteqgmbh/gadgeto"
22
+ gem.license = "MIT"
23
+ gem.summary = %Q{collection of useful code snippets}
24
+ gem.description = %Q{collection of useful code snippets}
25
+ gem.email = "rgogolok@avarteq.de"
26
+ gem.authors = ["Robert Gogolok", "Matthias Zirnstein"]
27
+ # dependencies defined in Gemfile
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+
32
+ task :default => :spec
33
+
34
+ require 'rdoc/task'
35
+ Rake::RDocTask.new do |rdoc|
36
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
37
+
38
+ rdoc.rdoc_dir = 'rdoc'
39
+ rdoc.title = "gadgeto #{version}"
40
+ rdoc.rdoc_files.include('README*')
41
+ rdoc.rdoc_files.include('lib/**/*.rb')
42
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/gadgeto.gemspec ADDED
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "gadgeto"
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Robert Gogolok", "Matthias Zirnstein"]
12
+ s.date = "2012-01-06"
13
+ s.description = "collection of useful code snippets"
14
+ s.email = "rgogolok@avarteq.de"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".travis.yml",
21
+ "Gemfile",
22
+ "LICENSE.txt",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "gadgeto.gemspec",
27
+ "lib/gadgeto.rb",
28
+ "lib/gadgeto/dslable.rb",
29
+ "lib/gadgeto/email.rb",
30
+ "lib/gadgeto/email/validators.rb",
31
+ "lib/gadgeto/sanitize_filename.rb",
32
+ "lib/gadgeto/time_of_day.rb",
33
+ "spec/lib/gadgeto/dslable_spec.rb",
34
+ "spec/lib/gadgeto/email_spec.rb",
35
+ "spec/lib/gadgeto/sanitize_filename_spec.rb",
36
+ "spec/lib/gadgeto/time_of_day_spec.rb",
37
+ "spec/spec_helper.rb",
38
+ "test/helper.rb",
39
+ "test/test_gadgeto.rb"
40
+ ]
41
+ s.homepage = "http://github.com/avarteqgmbh/gadgeto"
42
+ s.licenses = ["MIT"]
43
+ s.require_paths = ["lib"]
44
+ s.rubygems_version = "1.8.10"
45
+ s.summary = "collection of useful code snippets"
46
+
47
+ if s.respond_to? :specification_version then
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
52
+ s.add_development_dependency(%q<rake>, [">= 0"])
53
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
54
+ s.add_development_dependency(%q<rspec>, [">= 0"])
55
+ else
56
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
57
+ s.add_dependency(%q<rake>, [">= 0"])
58
+ s.add_dependency(%q<simplecov>, [">= 0"])
59
+ s.add_dependency(%q<rspec>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
63
+ s.add_dependency(%q<rake>, [">= 0"])
64
+ s.add_dependency(%q<simplecov>, [">= 0"])
65
+ s.add_dependency(%q<rspec>, [">= 0"])
66
+ end
67
+ end
68
+
@@ -0,0 +1,161 @@
1
+
2
+ module Gadgeto
3
+ #
4
+ # class Foo
5
+ # include Dslable
6
+ # include Dslable::Display
7
+ #
8
+ # dslable_method :item, :key, '*arguments'
9
+ #
10
+ #
11
+ # def inspect
12
+ # attributes[:key]
13
+ # end
14
+ # end
15
+ #
16
+ # f = Foo.new
17
+ #
18
+ #
19
+ # f.draw do
20
+ # item 'Startseite', :baem => :bum do
21
+ # item 'terms'
22
+ # item 'imprint'
23
+ # end
24
+ #
25
+ # item 'Praemien' do
26
+ # item 'Kueche' do
27
+ # item 'Toepfe'
28
+ # end
29
+ # end
30
+ # end
31
+ #
32
+ # f.display :items
33
+ module Dslable
34
+
35
+ def self.included(base)
36
+ base.extend ClassMethods
37
+ end
38
+
39
+ def self.extended(base)
40
+ class << base
41
+ self.extend ClassMethods
42
+ end
43
+ end
44
+
45
+ module Display
46
+ module InstanceMethods
47
+ self.instance_eval do
48
+ self.class_eval <<-RUBY
49
+
50
+ def display(storage, level = 0, &block)
51
+ send(storage).each do |i|
52
+ if block
53
+ block.call i, level
54
+ else
55
+ puts '%s%s' % [' ' * level, i.inspect]
56
+ end
57
+
58
+ i.display storage, level + 1, &block if i.items.size > 0
59
+ end
60
+
61
+ return nil
62
+ end
63
+ RUBY
64
+ end
65
+ end
66
+
67
+ include InstanceMethods
68
+ end
69
+
70
+ module ClassMethods
71
+ def dslable_method(method, *args)
72
+ self.instance_eval do
73
+
74
+ stored_at = method.to_s + 's' # pluralize with ActiveSupport
75
+
76
+ # method_params: key, *arguments, options = {}
77
+ # method_values_set: i.name = name
78
+ # method_setters_getters: def key, def arguments, def options
79
+
80
+ method_params, method_values_set, method_accessors = [], [], []
81
+
82
+ args.each do |arg|
83
+ unless [String, Symbol, Hash].include? arg.class
84
+ raise ArgumentError, 'allowed arguments are Symbols, Strings and Hashes'
85
+ end
86
+
87
+ method_params << (arg.is_a?(Hash) ? arg.to_a : [arg]).flatten.join(' = ')
88
+
89
+ arg_name = (arg.is_a?(Hash) ? arg.keys.first : arg).to_s.gsub('*', '')
90
+
91
+ method_values_set << arg_name
92
+ method_accessors << arg_name
93
+ end
94
+
95
+ method_params = method_params.join(', ')
96
+
97
+ method_values_set = method_values_set.map do |p|
98
+ 'i.%s = %s' % [p, p]
99
+ end.join(';')
100
+
101
+ method_accessors = method_accessors.map do |p|
102
+ "def #{p}; read_attribute(:#{p}); end;" + "def #{p}=(v); write_attribute(:#{p}, v); end;"
103
+ end.join
104
+
105
+ self.class_eval <<-RUBY
106
+
107
+ #{method_accessors}
108
+
109
+ def draw(code = nil, &block)
110
+ i = self.class.new
111
+
112
+ if code
113
+ i.instance_eval code
114
+ else
115
+ i.instance_eval &block
116
+ end
117
+
118
+ @#{stored_at} = i.#{stored_at}
119
+ return nil
120
+ end
121
+
122
+ def #{method.to_s}(#{method_params}, &block)
123
+ i = self.class.new
124
+ #{method_values_set}
125
+
126
+ i.instance_eval &block if block
127
+
128
+ self.#{stored_at} << i
129
+ end
130
+
131
+ def #{stored_at}
132
+ @#{stored_at} ||= []
133
+ end
134
+
135
+ def #{stored_at}=(v)
136
+ @#{stored_at} = v
137
+ end
138
+
139
+ def attributes
140
+ @attributes ||= {}
141
+ end
142
+
143
+ def attributes=(a)
144
+ @attributes = a
145
+ end
146
+
147
+ def read_attribute(n)
148
+ attributes[n]
149
+ end
150
+
151
+ def write_attribute(n, v)
152
+ attributes[n] = v
153
+ end
154
+
155
+ RUBY
156
+
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,9 @@
1
+ module Gadgeto
2
+ class Email
3
+ module Validators
4
+ def email?(email)
5
+ !!(email =~ /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ require File.join(File.dirname(__FILE__), '/email/validators')
2
+
3
+ module Gadgeto
4
+ class Email
5
+ include Email::Validators
6
+
7
+ class << self
8
+ def valid?(object)
9
+ o = new object
10
+ o.valid?
11
+ end
12
+ end
13
+
14
+ def initialize(email)
15
+ @email = email
16
+ end
17
+
18
+ def valid?
19
+ email? @email
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ module Gadgeto
2
+ module SanitizeFilename
3
+
4
+ def sanitize_filename(filename)
5
+ return filename unless filename
6
+
7
+ filename.strip.tap do |fn|
8
+ # get only the filename
9
+ fn.gsub! /^.*(\\|\/)/, ''
10
+
11
+ fn.gsub! /[^A-Za-z0-9\.\-]/, '_'
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,142 @@
1
+ module Gadgeto
2
+ # A +TimeOfDay+ represents the time of day.
3
+ #
4
+ # Limitation: 24 hours only.
5
+ class TimeOfDay
6
+
7
+ class InvalidTimeOfDayFormat < ::Exception # :nodoc:
8
+ end
9
+
10
+ class TimeOfDayOutOfRange < ::Exception # :nodoc:
11
+ end
12
+
13
+ ONE_HOUR_IN_MINUTES = 60 # :nodoc:
14
+ HOURS_ONE_DAY = 24 # :nodoc:
15
+
16
+ attr_reader :hour, :minute
17
+
18
+ # Creates a new TimeOfDay object.
19
+ #
20
+ # TimeOfDay.new("08:00") #=> 08:00
21
+ # TimeOfDay.new("x8:00") #=> raises InvalidTimeOfDayFormat
22
+ #
23
+ def initialize(time_of_day)
24
+ match = /^([0-2]?\d):([0-5]\d)$/.match(time_of_day)
25
+ raise InvalidTimeOfDayFormat if match.nil?
26
+ @hour = match[1].to_i
27
+ @minute = match[2].to_i
28
+ raise InvalidTimeOfDayFormat if (@hour > HOURS_ONE_DAY) || (@hour == HOURS_ONE_DAY && @minute > 0)
29
+ end
30
+
31
+ # Returns the hour component.
32
+ #
33
+ # t = TimeOfDay.new("08:00") #=> 08:00
34
+ # t.hour #=> 8
35
+ def hour
36
+ @hour
37
+ end
38
+
39
+ # Returns the hour component.
40
+ #
41
+ # t = TimeOfDay.new("08:03") #=> 08:00
42
+ # t.minute #=> 3
43
+ def minute
44
+ @minute
45
+ end
46
+
47
+ # Adds the given +minutes+ to +self+.
48
+ #
49
+ # t = TimeOfDay.new("08:00") #=> 08:00
50
+ # t.add_minutes(60) #=> 09:00
51
+ #
52
+ # t = TimeOfDay.new("23:50") #=> 08:00
53
+ # t.add_minutes(20) #=> 00:10
54
+ def add_minutes(minutes)
55
+ new_minutes = @minute + minutes
56
+ hours_to_add = (@minute + minutes) / 60
57
+ new_hour = @hour + hours_to_add
58
+
59
+ @hour = new_hour % HOURS_ONE_DAY
60
+ @minute = new_minutes % ONE_HOUR_IN_MINUTES
61
+
62
+ self
63
+ end
64
+
65
+ # Returns string representing time of day.
66
+ #
67
+ # TimeOfDay.new("08:00").to_s #=> "08:00"
68
+ def to_s
69
+ "#{hour.to_s.rjust(2, '0')}:#{minute.to_s.rjust(2, '0')}"
70
+ end
71
+
72
+ # Returns time of day in minutes.
73
+ #
74
+ # TimeOfDay.new("02:07") #=> 127
75
+ def to_i
76
+ hour * 60 + minute
77
+ end
78
+
79
+ # Returns +true+ if +self+ precedes +other+.
80
+ #
81
+ # TimeOfDay.new("08:00") < TimeOfDay.new("09:00:) #=> true
82
+ def <(other)
83
+ return true if self.hour < other.hour
84
+ return false if self.hour > other.hour
85
+
86
+ self.minute < other.minute
87
+ end
88
+
89
+ # Returns +true+ if +self+ follows +other+.
90
+ #
91
+ # TimeOfDay.new("09:00") < TimeOfDay.new("08:00:) #=> true
92
+ def >(other)
93
+ return true if self.hour > other.hour
94
+ return false if self.hour < other.hour
95
+
96
+ self.minute > other.minute
97
+ end
98
+
99
+ # Returns true if +self+ is equal to +other+.
100
+ #
101
+ # TimeOfDay.new("08:00") == TimeOfDay.new("08:00") #=> true
102
+ def ==(other)
103
+ self.hour == other.hour && self.minute == other.minute
104
+ end
105
+
106
+ # Returns difference in minutes between +self+ and +other+.
107
+ #
108
+ # TimeOfDay.new("08:00").minutes_till(TimeOfDay.new("09:00")) #=> 60
109
+ # TimeOfDay.new("23:00").minutes_till(TimeOfDay.new("01:00")) #=> 120
110
+ #
111
+ # TimeOfDay.new("23:00").minutes_till(TimeOfDay.new("01:00"), :overflow => false) #=> raises TimeOfDayOutOfRange
112
+ def minutes_till(other, options = {})
113
+ options[:overflow] = true if options[:overflow].nil?
114
+
115
+ if options[:overflow]
116
+ if self > other
117
+ (self.minutes_till(TimeOfDay.new("24:00"))) + other.to_i
118
+ else
119
+ other.to_i - self.to_i
120
+ end
121
+ else
122
+ raise TimeOfDayOutOfRange if self > other
123
+ other.to_i - self.to_i
124
+ end
125
+ end
126
+
127
+ # Returns +true+ if +time_of_day+ represents time of day.
128
+ #
129
+ # TimeOfDay.valid?("08:00") #=> true
130
+ def self.valid?(time_of_day)
131
+ begin
132
+ val = TimeOfDay.new(time_of_day)
133
+ true
134
+ rescue InvalidTimeOfDayFormat, TimeOfDayOutOfRange
135
+ false
136
+ end
137
+ end
138
+
139
+ alias :inspect :to_s
140
+
141
+ end
142
+ end
data/lib/gadgeto.rb ADDED
File without changes
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ require File.join(File.dirname(__FILE__), '../../../lib/gadgeto/dslable')
3
+
4
+ class DslableDummy
5
+ include Gadgeto::Dslable
6
+
7
+ dslable_method :item, :key, "*args"
8
+ end
9
+
10
+ describe Gadgeto::Dslable do
11
+
12
+ it "should define a class method dslable_method if extended" do
13
+ o = Object.new
14
+ o.extend(Gadgeto::Dslable)
15
+ o_metaclass = class << o; self; end
16
+
17
+ o_metaclass.respond_to?(:dslable_method).should be_true
18
+ end
19
+
20
+ it "should define a class method dslable_method if included" do
21
+ DslableDummy.respond_to?(:dslable_method).should be_true
22
+ end
23
+
24
+ it "should define an instance method draw" do
25
+ o = DslableDummy.new
26
+ o.respond_to?(:draw).should be_true
27
+ end
28
+
29
+ context "" do
30
+
31
+ let (:o) {DslableDummy.new}
32
+
33
+ before(:each) do
34
+ o.draw do
35
+ item "item1" do
36
+
37
+ item "item1.1" do
38
+ item "item1.1.1", 3, "hello world"
39
+ item "item1.1.2"
40
+ end
41
+
42
+ end
43
+
44
+ item "item2"
45
+ end
46
+ end
47
+
48
+ it { o.items.size.should == 2 }
49
+ it { o.items[0].class.should == DslableDummy }
50
+
51
+
52
+ it { o.items[0].items[0].items[0].attributes[:args].size.should == 2 }
53
+ it { o.items[0].items[0].items[0].attributes[:args][1].should == "hello world" }
54
+
55
+ it { o.items[0].items[0].items[1].attributes[:key].should == "item1.1.2" }
56
+ end
57
+
58
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+ require File.join(File.dirname(__FILE__), '../../../lib/gadgeto/email')
3
+
4
+ describe Gadgeto::Email do
5
+
6
+ it { Gadgeto::Email.valid?("user@example.com").should be_true }
7
+ it { Gadgeto::Email.valid?("user+name@example.com").should be_true }
8
+
9
+ it { Gadgeto::Email.valid?("userexample.com").should be_false }
10
+
11
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require File.join(File.dirname(__FILE__), '../../../lib/gadgeto/sanitize_filename')
3
+
4
+
5
+ describe Gadgeto::SanitizeFilename do
6
+
7
+ class DummyClass
8
+ include Gadgeto::SanitizeFilename
9
+ end
10
+
11
+ describe 'filename' do
12
+
13
+ subject { DummyClass.new }
14
+
15
+ it { subject.sanitize_filename('/home/foo/bar').should == 'bar'}
16
+ it { subject.sanitize_filename('\\bar').should == 'bar'}
17
+
18
+ context "stripping" do
19
+ it { subject.sanitize_filename('foo bar').should == 'foo_bar'}
20
+ it { subject.sanitize_filename(' foobar ').should == 'foobar'}
21
+ end
22
+
23
+ context "remove special characters" do
24
+ it { subject.sanitize_filename(',bar').should == '_bar'}
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,105 @@
1
+ require 'spec_helper'
2
+ require File.join(File.dirname(__FILE__), '../../../lib/gadgeto/time_of_day')
3
+
4
+ describe Gadgeto::TimeOfDay do
5
+
6
+ describe "new" do
7
+
8
+ it "should require an argument" do
9
+ expect { Gadgeto::TimeOfDay.new }.to raise_exception
10
+ end
11
+
12
+ it { expect{ Gadgeto::TimeOfDay.new("0730") }.to raise_error(Gadgeto::TimeOfDay::InvalidTimeOfDayFormat) }
13
+ it { expect{ Gadgeto::TimeOfDay.new("a:30") }.to raise_error(Gadgeto::TimeOfDay::InvalidTimeOfDayFormat) }
14
+ it { expect{ Gadgeto::TimeOfDay.new("-1:30") }.to raise_error(Gadgeto::TimeOfDay::InvalidTimeOfDayFormat) }
15
+ it { expect{ Gadgeto::TimeOfDay.new("+1:30") }.to raise_error(Gadgeto::TimeOfDay::InvalidTimeOfDayFormat) }
16
+ it { expect{ Gadgeto::TimeOfDay.new("a1:30") }.to raise_error(Gadgeto::TimeOfDay::InvalidTimeOfDayFormat) }
17
+ it { expect{ Gadgeto::TimeOfDay.new("01:x") }.to raise_error(Gadgeto::TimeOfDay::InvalidTimeOfDayFormat) }
18
+ it { expect{ Gadgeto::TimeOfDay.new("01:3") }.to raise_error(Gadgeto::TimeOfDay::InvalidTimeOfDayFormat) }
19
+ it { expect{ Gadgeto::TimeOfDay.new("30:00") }.to raise_error(Gadgeto::TimeOfDay::InvalidTimeOfDayFormat) }
20
+ it { expect{ Gadgeto::TimeOfDay.new("01:70") }.to raise_error(Gadgeto::TimeOfDay::InvalidTimeOfDayFormat) }
21
+ it { expect{ Gadgeto::TimeOfDay.new("01:61") }.to raise_error(Gadgeto::TimeOfDay::InvalidTimeOfDayFormat) }
22
+ it { expect{ Gadgeto::TimeOfDay.new("25:00") }.to raise_error(Gadgeto::TimeOfDay::InvalidTimeOfDayFormat) }
23
+ it { expect{ Gadgeto::TimeOfDay.new("24:01") }.to raise_error(Gadgeto::TimeOfDay::InvalidTimeOfDayFormat) }
24
+
25
+ it { expect{ Gadgeto::TimeOfDay.new("24:00") }.not_to raise_error }
26
+ it { expect{ Gadgeto::TimeOfDay.new("08:13") }.not_to raise_error }
27
+ end
28
+
29
+
30
+ describe "to_s" do
31
+ it "should return string representation by default" do
32
+ Gadgeto::TimeOfDay.new("08:30").to_s.should == "08:30"
33
+ end
34
+ end
35
+
36
+ describe "to_i" do
37
+ context "should return time of day converted in minutes" do
38
+ it { Gadgeto::TimeOfDay.new("00:00").to_i.should == 0 }
39
+ it { Gadgeto::TimeOfDay.new("08:00").to_i.should == (8 * 60) }
40
+ it { Gadgeto::TimeOfDay.new("08:13").to_i.should == (8 * 60 + 13) }
41
+ end
42
+ end
43
+
44
+ describe "hour" do
45
+ it { Gadgeto::TimeOfDay.new("08:30").hour.should == 8 }
46
+ it { Gadgeto::TimeOfDay.new("8:30").hour.should == 8 }
47
+ end
48
+
49
+ describe "minute" do
50
+ it { Gadgeto::TimeOfDay.new("08:37").minute.should == 37 }
51
+ it { Gadgeto::TimeOfDay.new("08:07").minute.should == 7 }
52
+ end
53
+
54
+ describe "add_minutes" do
55
+ it { Gadgeto::TimeOfDay.new("08:30").add_minutes(7).to_s.should == "08:37" }
56
+ it { Gadgeto::TimeOfDay.new("08:59").add_minutes(1).to_s.should == "09:00" }
57
+ it { Gadgeto::TimeOfDay.new("08:00").add_minutes(135).to_s.should == "10:15" }
58
+
59
+ it { Gadgeto::TimeOfDay.new("23:59").add_minutes(1).to_s.should == "00:00" }
60
+ it { Gadgeto::TimeOfDay.new("23:59").add_minutes(2).to_s.should == "00:01" }
61
+ it { Gadgeto::TimeOfDay.new("23:59").add_minutes(120).to_s.should == "01:59" }
62
+
63
+ end
64
+
65
+ describe "self.valid?" do
66
+ it { expect { Gadgeto::TimeOfDay.valid? }.to raise_error }
67
+ it { Gadgeto::TimeOfDay.valid?("08:07").should be_true }
68
+ it { Gadgeto::TimeOfDay.valid?("08:70").should be_false }
69
+ it { Gadgeto::TimeOfDay.valid?("x8:30").should be_false }
70
+ end
71
+
72
+ describe "operator <" do
73
+ it { (Gadgeto::TimeOfDay.new("08:00") < Gadgeto::TimeOfDay.new("08:01")).should be_true }
74
+ it { (Gadgeto::TimeOfDay.new("08:01") < Gadgeto::TimeOfDay.new("08:00")).should be_false }
75
+ it { (Gadgeto::TimeOfDay.new("08:00") < Gadgeto::TimeOfDay.new("08:00")).should be_false }
76
+ end
77
+
78
+ describe "operator >" do
79
+ it { (Gadgeto::TimeOfDay.new("08:01") > Gadgeto::TimeOfDay.new("08:00")).should be_true }
80
+ it { (Gadgeto::TimeOfDay.new("08:00") > Gadgeto::TimeOfDay.new("08:01")).should be_false }
81
+ it { (Gadgeto::TimeOfDay.new("08:11") > Gadgeto::TimeOfDay.new("08:11")).should be_false }
82
+ end
83
+
84
+ describe "operator ==" do
85
+ it { (Gadgeto::TimeOfDay.new("08:13") == Gadgeto::TimeOfDay.new("08:13")).should be_true }
86
+ end
87
+
88
+ describe "till" do
89
+
90
+ it { Gadgeto::TimeOfDay.new("00:00").minutes_till(Gadgeto::TimeOfDay.new("00:00")).should == 0 }
91
+ it { Gadgeto::TimeOfDay.new("00:00").minutes_till(Gadgeto::TimeOfDay.new("00:13")).should == 13 }
92
+ it { Gadgeto::TimeOfDay.new("08:15").minutes_till(Gadgeto::TimeOfDay.new("10:00")).should == 105 }
93
+
94
+ context "allow overflow" do
95
+ it { Gadgeto::TimeOfDay.new("23:00").minutes_till(Gadgeto::TimeOfDay.new("01:00")).should == 120 }
96
+ end
97
+
98
+ context "do not allow overflow" do
99
+ it { expect { Gadgeto::TimeOfDay.new("23:00").minutes_till(Gadgeto::TimeOfDay.new("01:00"), :overflow => false)}.to raise_error }
100
+ it { Gadgeto::TimeOfDay.new("23:00").minutes_till(Gadgeto::TimeOfDay.new("23:15"), :overflow => false).should == 15 }
101
+ end
102
+
103
+ end
104
+
105
+ end
@@ -0,0 +1,4 @@
1
+ if ENV["COVERAGE"]
2
+ require 'simplecov'
3
+ SimpleCov.start 'rails'
4
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'gadgeto'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestGadgeto < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gadgeto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Robert Gogolok
9
+ - Matthias Zirnstein
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-01-06 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: jeweler
17
+ requirement: &70348239824400 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 1.6.4
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *70348239824400
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: &70348239823560 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70348239823560
37
+ - !ruby/object:Gem::Dependency
38
+ name: simplecov
39
+ requirement: &70348239822400 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70348239822400
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ requirement: &70348239821180 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *70348239821180
59
+ description: collection of useful code snippets
60
+ email: rgogolok@avarteq.de
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files:
64
+ - LICENSE.txt
65
+ - README.rdoc
66
+ files:
67
+ - .travis.yml
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.rdoc
71
+ - Rakefile
72
+ - VERSION
73
+ - gadgeto.gemspec
74
+ - lib/gadgeto.rb
75
+ - lib/gadgeto/dslable.rb
76
+ - lib/gadgeto/email.rb
77
+ - lib/gadgeto/email/validators.rb
78
+ - lib/gadgeto/sanitize_filename.rb
79
+ - lib/gadgeto/time_of_day.rb
80
+ - spec/lib/gadgeto/dslable_spec.rb
81
+ - spec/lib/gadgeto/email_spec.rb
82
+ - spec/lib/gadgeto/sanitize_filename_spec.rb
83
+ - spec/lib/gadgeto/time_of_day_spec.rb
84
+ - spec/spec_helper.rb
85
+ - test/helper.rb
86
+ - test/test_gadgeto.rb
87
+ homepage: http://github.com/avarteqgmbh/gadgeto
88
+ licenses:
89
+ - MIT
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ segments:
101
+ - 0
102
+ hash: 1775889944675506510
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 1.8.10
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: collection of useful code snippets
115
+ test_files: []