rmoriz-smartmonth 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +30 -0
- data/Rakefile +22 -0
- data/init.rb +5 -0
- data/lib/month.rb +19 -0
- data/lib/smart_month/calculations.rb +80 -0
- data/lib/smart_month/collection.rb +67 -0
- data/lib/smart_month/magic.rb +35 -0
- data/lib/smart_month/math.rb +39 -0
- data/lib/smart_month/util.rb +29 -0
- data/smartmonth.gemspec +60 -0
- data/tasks/smart_month_tasks.rake +0 -0
- data/test/test_helper.rb +26 -0
- data/test/unit/smart_month_test.rb +189 -0
- metadata +70 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Panoctagon, inc. (Derek Perez | www.derekperez.com)
|
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
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
SmartMonth is a "bodysnatcher" plugin that takes boring Date month fixnums and replaces them with a rich toolkit of functionality. You can use SmartMonth to:
|
2
|
+
|
3
|
+
- Determine the first tuesday of any month in any year.
|
4
|
+
- Determine all of the fridays of any month in any year.
|
5
|
+
- Iterate through all the days of a month.
|
6
|
+
- Determine how many days of the month there are.
|
7
|
+
- Determine the first and last days of the month.
|
8
|
+
- And other fun date/month related things!
|
9
|
+
|
10
|
+
This is designed to be an extension of not only Time.now.month, but adds a new Month class to the ruby object model.
|
11
|
+
|
12
|
+
Keep in mind, this is 1.0, and may break things that rely on Time.now.month being a Fixnum. I've tried to fix this by making sure #to_i works as expected, but it may be an issue, handle with care.
|
13
|
+
|
14
|
+
Examples:
|
15
|
+
|
16
|
+
Time.now.month.every_tuesday #=> [array of tuesdays for the current month]
|
17
|
+
Month.april.first_wednesday #=> Date object corresponding to the first wednesday of april.
|
18
|
+
Month.may.every_monday_and_friday #=> {:monday=>[...],:friday=>[...]}
|
19
|
+
Month.june(2012).last_monday #=> Date object corresponding to the last monday in june 2012.
|
20
|
+
|
21
|
+
# iterator:
|
22
|
+
Month.june(2004).each do |day|
|
23
|
+
day.to_s #=> name of day (Saturday, Sunday, etc.)
|
24
|
+
day.to_i #=> value between 1 and the last day of the month corresponding.
|
25
|
+
end
|
26
|
+
|
27
|
+
Check the included documentation (rdoc) or our documentation site (http://code.panoctagon.com/projects/smartmonth)
|
28
|
+
for more info on usage!
|
29
|
+
|
30
|
+
Copyright (c) 2008 Panoctagon, inc (Derek Perez | www.derekperez.com), released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the smart_month plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the smart_month plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'SmartMonth'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/init.rb
ADDED
data/lib/month.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
class Month
|
4
|
+
# gather the modules
|
5
|
+
include SmartMonth::Calculations
|
6
|
+
include SmartMonth::Collection
|
7
|
+
include SmartMonth::Math
|
8
|
+
include SmartMonth::Util
|
9
|
+
include SmartMonth::Magic
|
10
|
+
# abstraction that returns an array of months.
|
11
|
+
NAMES = Date::MONTHNAMES
|
12
|
+
# abstraction that returns an array of day names.
|
13
|
+
DAYS = Date::DAYNAMES
|
14
|
+
# constructor, takes 2 optional arguments, if you don't provide them, it will default to
|
15
|
+
# the current month and year.
|
16
|
+
def initialize(month = Time.now.mon, year = Time.now.year)
|
17
|
+
@date = Date.new(year,( month.is_a?(Integer) ? month : Month::NAMES.index(month.to_s.capitalize) ) ,1)
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module SmartMonth
|
2
|
+
# Responsible for all core month/week calculations.
|
3
|
+
module Calculations
|
4
|
+
# returns the first date of the day requested.
|
5
|
+
# if no day is defined, it returns the very first day of the month.
|
6
|
+
def first(day = nil)
|
7
|
+
(day.nil?) ? @date : nth_weekday(day,1)
|
8
|
+
end
|
9
|
+
|
10
|
+
# returns the second date of the day requested.
|
11
|
+
def second(day)
|
12
|
+
nth_weekday(day,2)
|
13
|
+
end
|
14
|
+
|
15
|
+
# returns the third date of the day requested.
|
16
|
+
def third(day)
|
17
|
+
nth_weekday(day,3)
|
18
|
+
end
|
19
|
+
|
20
|
+
# returns the fourth (or potentially last), date of the day requested.
|
21
|
+
def fourth(day)
|
22
|
+
nth_weekday(day,4) || self.last(day)
|
23
|
+
end
|
24
|
+
|
25
|
+
# returns last date of the day requested.
|
26
|
+
# if no day is defined, it returns the very last day of the month.
|
27
|
+
def last(day = nil)
|
28
|
+
(day.nil?) ? Date.new(@date.year,@date.month,-1) : every(day).last
|
29
|
+
end
|
30
|
+
|
31
|
+
# returns an array of dates of the day requested.
|
32
|
+
# if an array of dates are passed in, a hash of day names containing
|
33
|
+
# the corresponding date are returned.
|
34
|
+
def every(*days)
|
35
|
+
days = [days].flatten
|
36
|
+
if days.size == 1
|
37
|
+
dates = []
|
38
|
+
(1..5).each do |week|
|
39
|
+
dates << nth_weekday(days.first,week)
|
40
|
+
end
|
41
|
+
dates.compact!
|
42
|
+
else
|
43
|
+
dates = {}
|
44
|
+
days.each do |day|
|
45
|
+
day = day.to_sym
|
46
|
+
dates[day] = []
|
47
|
+
(1..5).each do |week|
|
48
|
+
dates[day] << nth_weekday(day,week)
|
49
|
+
end
|
50
|
+
dates[day].compact!
|
51
|
+
end
|
52
|
+
end
|
53
|
+
dates
|
54
|
+
end
|
55
|
+
|
56
|
+
# returns the total number of days in month.
|
57
|
+
def size
|
58
|
+
self.last.day.to_i
|
59
|
+
end
|
60
|
+
alias_method :length, :size
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
# this is the heart of the calculations module.
|
65
|
+
def nth_weekday(day,limit = 0)
|
66
|
+
raise RuntimeError, "#{day} is not a valid day of the week" unless Month::DAYS.collect{|d| d.downcase}.include?(day.to_s)
|
67
|
+
limit -= 1 # make more sense to humans
|
68
|
+
requested_weekday = Month::DAYS.collect{|d| d.downcase}.index(day.to_s)
|
69
|
+
iter_date = Date.new(@date.year,@date.month,1)
|
70
|
+
# find the first day that works
|
71
|
+
until iter_date.wday == requested_weekday
|
72
|
+
iter_date +=1
|
73
|
+
end
|
74
|
+
# then add a multipler based on the limit
|
75
|
+
iter_date = iter_date + (limit*7)
|
76
|
+
# if the date we've iterated to doesn't exist in this month, send out a nil.
|
77
|
+
return iter_date unless iter_date.month != @date.month
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module SmartMonth
|
2
|
+
# Responsible for all collection-based functionality.
|
3
|
+
module Collection
|
4
|
+
# this is a helper class that is used primarily by Month#each.
|
5
|
+
# should not be used standalone for any reason, really.
|
6
|
+
class Day
|
7
|
+
def initialize(date)
|
8
|
+
@date = date
|
9
|
+
end
|
10
|
+
# returns the date numeric of the day (in regards to the month it resides in).
|
11
|
+
def to_i
|
12
|
+
@date.day
|
13
|
+
end
|
14
|
+
# returns the string value of the weekday.
|
15
|
+
def to_s
|
16
|
+
Month::DAYS[@date.wday]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# allows for increment by copy
|
21
|
+
def next
|
22
|
+
if @date.month + 1 > 12
|
23
|
+
Month.new(1,@date.year+1)
|
24
|
+
else
|
25
|
+
Month.new(@date.month+1,@date.year)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# allows for in place incrementing by 1
|
30
|
+
def next!
|
31
|
+
if @date.month + 1 > 12
|
32
|
+
@date = Date.new(@date.year+1,@date.month,1)
|
33
|
+
else
|
34
|
+
@date = Date.new(@date.year,@date.month+1,1)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# allows for deincrement by copy
|
39
|
+
def previous
|
40
|
+
if @date.month - 1 == 0
|
41
|
+
Month.new(12,@date.year-1)
|
42
|
+
else
|
43
|
+
Month.new(@date.month-1,@date.year)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
alias_method :prev, :previous
|
47
|
+
|
48
|
+
# allows for in place deincrementing by 1
|
49
|
+
def previous!
|
50
|
+
if @date.month - 1 == 0
|
51
|
+
@date = Date.new(@date.year-1,12,1)
|
52
|
+
else
|
53
|
+
@date = Date.new(@date.year,@date.month-1,1)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
alias_method :prev!, :previous!
|
57
|
+
|
58
|
+
# allows us to iterate internally on the days of a given month.
|
59
|
+
# It also uses the helpful Day class that provides some fun methods. (see SmartMonth::Collection::Day)
|
60
|
+
def each(&block)
|
61
|
+
(1..self.last.day).each do |e|
|
62
|
+
block.call(Day.new(Date.new(@date.year,@date.month,e)))
|
63
|
+
end
|
64
|
+
return self
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module SmartMonth
|
2
|
+
# Magic is responsible for the rails-like dynamic "magic finders".
|
3
|
+
module Magic
|
4
|
+
|
5
|
+
# extends the Month class with the Month factory
|
6
|
+
def self.included(base)
|
7
|
+
base.extend MonthFactory
|
8
|
+
end
|
9
|
+
|
10
|
+
# This module allows your code to ignore the class instantiation, simply do Month.june!
|
11
|
+
module MonthFactory
|
12
|
+
# singleton month factory
|
13
|
+
def method_missing(meth,*args)
|
14
|
+
return new(meth.to_s.capitalize, (args.first || Time.now.year)) if Month::NAMES.include? meth.to_s.capitalize
|
15
|
+
end
|
16
|
+
|
17
|
+
# this allows you to essentially treat the month as a hash or array object to construct new months.
|
18
|
+
def [](month)
|
19
|
+
new(month)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# nice catchall to allow rails-esque APIs if you choose to.
|
24
|
+
def method_missing(meth)
|
25
|
+
raw = meth.to_s.split("_")
|
26
|
+
func = raw.slice!(0)
|
27
|
+
if func == "every"
|
28
|
+
args = raw.select { |a| a != "and" }
|
29
|
+
self.send(func,args)
|
30
|
+
else
|
31
|
+
self.send(func,raw) if %w(first second third fourth last).include? func
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module SmartMonth
|
2
|
+
# Responsible for all math related functionality.
|
3
|
+
module Math
|
4
|
+
# adds a number to the month value and returns a Fixnum.
|
5
|
+
def +(int)
|
6
|
+
self.to_i + int
|
7
|
+
end
|
8
|
+
|
9
|
+
# subtracts a number to the month value and returns a Fixnum.
|
10
|
+
def -(int)
|
11
|
+
self.to_i - int
|
12
|
+
end
|
13
|
+
|
14
|
+
# multiplies a number to the month value and returns a Fixnum.
|
15
|
+
def *(int)
|
16
|
+
self.to_i * int
|
17
|
+
end
|
18
|
+
|
19
|
+
# divides a number to the month value and returns a Fixnum.
|
20
|
+
def /(int)
|
21
|
+
self.to_i / int
|
22
|
+
end
|
23
|
+
|
24
|
+
# exponents a number to the month value and returns a Fixnum.
|
25
|
+
def **(int)
|
26
|
+
self.to_i ** int
|
27
|
+
end
|
28
|
+
|
29
|
+
# modulos a number to the month value and returns a Fixnum.
|
30
|
+
def %(int)
|
31
|
+
self.to_i % int
|
32
|
+
end
|
33
|
+
|
34
|
+
# compares the current month to another month.
|
35
|
+
def ==(month)
|
36
|
+
self.inspect! == month.inspect!
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module SmartMonth
|
2
|
+
# Responsible for odds and ends.
|
3
|
+
module Util
|
4
|
+
# returns the month as a string.
|
5
|
+
def inspect
|
6
|
+
"#{self.to_s}"
|
7
|
+
end
|
8
|
+
|
9
|
+
# returns the year of the current month.
|
10
|
+
def year
|
11
|
+
@date.year
|
12
|
+
end
|
13
|
+
|
14
|
+
# returns the month and the year as a string.
|
15
|
+
def inspect!
|
16
|
+
"#{self.to_s} #{@date.year}"
|
17
|
+
end
|
18
|
+
|
19
|
+
# returns the month as a string.
|
20
|
+
def to_s
|
21
|
+
Month::NAMES[@date.month]
|
22
|
+
end
|
23
|
+
|
24
|
+
# returns the month as a fixnum.
|
25
|
+
def to_i
|
26
|
+
Month::NAMES.index(self.to_s)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/smartmonth.gemspec
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "smartmonth"
|
3
|
+
s.version = "1.0"
|
4
|
+
s.date = "2008-08-21"
|
5
|
+
s.summary = "Making months smarter in Ruby (and Rails)."
|
6
|
+
s.email = "roland@moriz.de"
|
7
|
+
s.homepage = "http://github.com/rmoriz/smartmonth/tree/master"
|
8
|
+
s.has_rdoc = true
|
9
|
+
s.authors = ['Derek Perez']
|
10
|
+
s.files = [
|
11
|
+
"init.rb",
|
12
|
+
"lib/month.rb",
|
13
|
+
"lib/smart_month",
|
14
|
+
"lib/smart_month/calculations.rb",
|
15
|
+
"lib/smart_month/collection.rb",
|
16
|
+
"lib/smart_month/magic.rb",
|
17
|
+
"lib/smart_month/math.rb",
|
18
|
+
"lib/smart_month/util.rb",
|
19
|
+
"MIT-LICENSE",
|
20
|
+
"Rakefile",
|
21
|
+
# "rdoc/classes/Month.html",
|
22
|
+
# "rdoc/classes/SmartMonth",
|
23
|
+
# "rdoc/classes/SmartMonth/Calculations.html",
|
24
|
+
# "rdoc/classes/SmartMonth/Collection",
|
25
|
+
# "rdoc/classes/SmartMonth/Collection/Day.html",
|
26
|
+
# "rdoc/classes/SmartMonth/Collection.html",
|
27
|
+
# "rdoc/classes/SmartMonth/Magic",
|
28
|
+
# "rdoc/classes/SmartMonth/Magic/MonthFactory.html",
|
29
|
+
# "rdoc/classes/SmartMonth/Magic.html",
|
30
|
+
# "rdoc/classes/SmartMonth/Math.html",
|
31
|
+
# "rdoc/classes/SmartMonth/Util.html",
|
32
|
+
# "rdoc/classes/SmartMonth.html",
|
33
|
+
# "rdoc/created.rid",
|
34
|
+
# "rdoc/files/lib/month_rb.html",
|
35
|
+
# "rdoc/files/lib/smart_month",
|
36
|
+
# "rdoc/files/lib/smart_month/calculations_rb.html",
|
37
|
+
# "rdoc/files/lib/smart_month/collection_rb.html",
|
38
|
+
# "rdoc/files/lib/smart_month/magic_rb.html",
|
39
|
+
# "rdoc/files/lib/smart_month/math_rb.html",
|
40
|
+
# "rdoc/files/lib/smart_month/util_rb.html",
|
41
|
+
# "rdoc/files/README.html",
|
42
|
+
# "rdoc/fr_class_index.html",
|
43
|
+
# "rdoc/fr_file_index.html",
|
44
|
+
# "rdoc/fr_method_index.html",
|
45
|
+
# "rdoc/index.html",
|
46
|
+
# "rdoc/rdoc-style.css",
|
47
|
+
"README",
|
48
|
+
"smartmonth.gemspec",
|
49
|
+
"tasks/smart_month_tasks.rake",
|
50
|
+
"test/test_helper.rb",
|
51
|
+
"test/unit/smart_month_test.rb",
|
52
|
+
]
|
53
|
+
s.test_files = [
|
54
|
+
"test/test_helper.rb",
|
55
|
+
"test/unit/smart_month_test.rb",
|
56
|
+
]
|
57
|
+
s.rdoc_options = ["--main", "README"]
|
58
|
+
s.extra_rdoc_files = ["README", "MIT-LICENSE"]
|
59
|
+
end
|
60
|
+
|
File without changes
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
ENV['RAILS_ENV'] ||= 'development'
|
2
|
+
require File.dirname(__FILE__) + '/../../../../config/environment.rb' # this sucks
|
3
|
+
|
4
|
+
# Load the testing framework
|
5
|
+
require 'test_help'
|
6
|
+
silence_warnings { RAILS_ENV = ENV['RAILS_ENV'] }
|
7
|
+
|
8
|
+
# Run the migrations
|
9
|
+
ActiveRecord::Migrator.migrate("#{RAILS_ROOT}/db/migrate")
|
10
|
+
|
11
|
+
# Setup the fixtures path
|
12
|
+
Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
|
13
|
+
$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
|
14
|
+
|
15
|
+
class Test::Unit::TestCase #:nodoc:
|
16
|
+
def create_fixtures(*table_names)
|
17
|
+
if block_given?
|
18
|
+
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
|
19
|
+
else
|
20
|
+
Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
self.use_transactional_fixtures = true
|
25
|
+
self.use_instantiated_fixtures = false
|
26
|
+
end
|
@@ -0,0 +1,189 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
3
|
+
require "date"
|
4
|
+
|
5
|
+
class SmartMonthTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
# SmartMonth::Calcuations
|
8
|
+
|
9
|
+
# Tested By: Derek Perez
|
10
|
+
def test_should_return_first_day_of_month
|
11
|
+
# test an ambiguous first, no weekday defined.
|
12
|
+
assert_equal Month.april(2008).first, Date.new(2008,4,1)
|
13
|
+
# test a specific first weekday.
|
14
|
+
assert_equal Month.march(2002).first(:tuesday), Date.new(2002,3,5)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Tested By: Derek Perez
|
18
|
+
def test_should_return_second_day_of_month
|
19
|
+
assert_equal Month.june(2007).second(:friday), Date.new(2007,6,8)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Tested By: Derek Perez
|
23
|
+
def test_should_return_third_day_of_month
|
24
|
+
assert_equal Month.august(2008).third(:wednesday), Date.new(2008,8,20)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Tested By: Derek Perez
|
28
|
+
def test_should_return_last_day_of_month
|
29
|
+
# test an ambiguous last, no weekday defined.
|
30
|
+
assert_equal Month.december.last, Date.new(2008,12,31)
|
31
|
+
# test a specific last weekday.
|
32
|
+
assert_equal Month.september(2004).last(:friday), Date.new(2004,9,24)
|
33
|
+
# test a specific ambiguous, fourth, no weekday defined.
|
34
|
+
assert_equal Month.june(2004).fourth(:friday), Date.new(2004,6,25)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Tested By: Derek Perez
|
38
|
+
def test_should_return_fourth_and_last_day_of_month
|
39
|
+
assert_equal Month.june(2004).fourth(:friday), Month.june(2004).last(:friday)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Tested By: Derek Perez
|
43
|
+
def test_should_return_every_day_of_month
|
44
|
+
assertion = Month.april(2003).every(:tuesday)
|
45
|
+
assert_equal assertion.size, 5
|
46
|
+
assert_equal assertion, [Date.new(2003,4,1),Date.new(2003,4,8),Date.new(2003,4,15),Date.new(2003,4,22),Date.new(2003,4,29)]
|
47
|
+
end
|
48
|
+
|
49
|
+
# Tested By: Derek Perez
|
50
|
+
def test_should_retun_every_set_of_days_of_month
|
51
|
+
assertion = Month.august(2006).every(:monday,:wednesday)
|
52
|
+
# verify
|
53
|
+
assert assertion.is_a?(Hash)
|
54
|
+
# monday assertions
|
55
|
+
assert_equal assertion[:monday].size, 4
|
56
|
+
assert_equal assertion[:monday], [Date.new(2006,8,7),Date.new(2006,8,14),Date.new(2006,8,21),Date.new(2006,8,28)]
|
57
|
+
# wednesday assertions
|
58
|
+
assert_equal assertion[:wednesday].size, 5
|
59
|
+
assert_equal assertion[:wednesday],
|
60
|
+
[Date.new(2006,8,2),Date.new(2006,8,9),Date.new(2006,8,16),Date.new(2006,8,23),Date.new(2006,8,30)]
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
# Tested By: Derek Perez
|
65
|
+
def test_should_return_total_days_of_month
|
66
|
+
assert_equal Month.april(2004).size, 30
|
67
|
+
assert_equal Month.april(2004).length, 30 # alternate access
|
68
|
+
end
|
69
|
+
|
70
|
+
# SmartMonth::Collection
|
71
|
+
|
72
|
+
# Tested By: Derek Perez
|
73
|
+
def test_should_return_next_month
|
74
|
+
# default by copy
|
75
|
+
assert_equal Month.april.next, Month.may
|
76
|
+
# alternate approach
|
77
|
+
month = Month.april
|
78
|
+
month.next!
|
79
|
+
assert month, Month.may
|
80
|
+
end
|
81
|
+
|
82
|
+
# Tested By: Derek Perez
|
83
|
+
def test_should_return_previous_month
|
84
|
+
# default by copy
|
85
|
+
assert_equal Month.may.previous, Month.april
|
86
|
+
# alternate approach (by copy)
|
87
|
+
assert_equal Month.june.prev, Month.may
|
88
|
+
# alternate approach (by ref)
|
89
|
+
month = Month.august
|
90
|
+
month.previous!
|
91
|
+
assert_equal month, Month.july
|
92
|
+
# alternate approach (by copy)
|
93
|
+
month = Month.july
|
94
|
+
month.prev!
|
95
|
+
assert_equal month, Month.june
|
96
|
+
end
|
97
|
+
|
98
|
+
# Tested By: Derek Perez
|
99
|
+
def test_should_iterate_through_each_day_of_month
|
100
|
+
counter = 0
|
101
|
+
Month.april(2009).each do |day|
|
102
|
+
counter += 1
|
103
|
+
assert Month::DAYS.include?(day.to_s)
|
104
|
+
assert (1..31).include?(day.to_i)
|
105
|
+
end
|
106
|
+
assert_equal counter, Month.april.size
|
107
|
+
end
|
108
|
+
|
109
|
+
# SmartMonth::Magic
|
110
|
+
|
111
|
+
# Tested By: Derek Perez
|
112
|
+
def test_should_create_month_through_class_factory
|
113
|
+
assert_equal Month.april, Month.new("April")
|
114
|
+
assert_equal Month.march, Month.new(3)
|
115
|
+
assert_equal Month.june(2010), Month.new("June",2010)
|
116
|
+
end
|
117
|
+
|
118
|
+
# Tested By: Derek Perez
|
119
|
+
def test_should_magically_determine_function_execution
|
120
|
+
assert_equal Month.march.first(:tuesday), Month.march.first_tuesday
|
121
|
+
assert_equal Month.may.every_thursday, Month.may.every(:thursday)
|
122
|
+
assert_equal Month.june.every_tuesday_and_friday, Month.june.every(:tuesday,:friday)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Tested By: Derek Perez
|
126
|
+
def test_should_return_month_as_array_index
|
127
|
+
assert_equal Month[3], Month.march
|
128
|
+
end
|
129
|
+
|
130
|
+
# Tested By: Derek Perez
|
131
|
+
def test_should_return_month_as_hash_key
|
132
|
+
assert_equal Month[:june], Month.june
|
133
|
+
assert_equal Month["april"], Month.april # alternate
|
134
|
+
end
|
135
|
+
|
136
|
+
# SmartMonth::Math
|
137
|
+
|
138
|
+
# Tested By: Derek Perez
|
139
|
+
def test_should_add_month_and_fixnum
|
140
|
+
assert_equal (Month.march + 2), 5
|
141
|
+
end
|
142
|
+
|
143
|
+
# Tested By: Derek Perez
|
144
|
+
def text_should_subtract_month_and_fixnum
|
145
|
+
assert_equal (Month.june - 4), 2
|
146
|
+
end
|
147
|
+
|
148
|
+
# Tested By: Derek Perez
|
149
|
+
def test_should_divide_month_and_fixnum
|
150
|
+
assert_equal (Month.december / 2), 6
|
151
|
+
end
|
152
|
+
|
153
|
+
# Tested By: Derek Perez
|
154
|
+
def test_should_exponent_month_and_fixnum
|
155
|
+
assert_equal (Month.may ** 2), 25
|
156
|
+
end
|
157
|
+
|
158
|
+
# Tested By: Derek Perez
|
159
|
+
def test_should_modulo_month_and_fixnum
|
160
|
+
assert_equal (Month.june % 7), 6
|
161
|
+
end
|
162
|
+
|
163
|
+
#SmartMonth::Util
|
164
|
+
|
165
|
+
def test_should_return_year_of_month
|
166
|
+
assert_equal Month.march(2004).year, 2004
|
167
|
+
end
|
168
|
+
|
169
|
+
# Tested By: Derek Perez
|
170
|
+
def test_should_inspect_month_as_string
|
171
|
+
assert_equal Month.march.inspect, "March"
|
172
|
+
end
|
173
|
+
|
174
|
+
# Tested By: Derek Perez
|
175
|
+
def test_should_inspect_month_and_year_as_string
|
176
|
+
assert_equal Month.july(2012).inspect!, "July 2012"
|
177
|
+
end
|
178
|
+
|
179
|
+
# Tested By: Derek Perez
|
180
|
+
def test_should_convert_month_to_string
|
181
|
+
assert_equal Month.march.to_s, "March"
|
182
|
+
end
|
183
|
+
|
184
|
+
# Tested By: Derek Perez
|
185
|
+
def test_should_convert_month_to_fixnum
|
186
|
+
assert_equal Month.june.to_i, 6
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rmoriz-smartmonth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "1.0"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Derek Perez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-08-21 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: roland@moriz.de
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- MIT-LICENSE
|
25
|
+
files:
|
26
|
+
- init.rb
|
27
|
+
- lib/month.rb
|
28
|
+
- lib/smart_month
|
29
|
+
- lib/smart_month/calculations.rb
|
30
|
+
- lib/smart_month/collection.rb
|
31
|
+
- lib/smart_month/magic.rb
|
32
|
+
- lib/smart_month/math.rb
|
33
|
+
- lib/smart_month/util.rb
|
34
|
+
- MIT-LICENSE
|
35
|
+
- Rakefile
|
36
|
+
- README
|
37
|
+
- smartmonth.gemspec
|
38
|
+
- tasks/smart_month_tasks.rake
|
39
|
+
- test/test_helper.rb
|
40
|
+
- test/unit/smart_month_test.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/rmoriz/smartmonth/tree/master
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --main
|
46
|
+
- README
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.2.0
|
65
|
+
signing_key:
|
66
|
+
specification_version: 2
|
67
|
+
summary: Making months smarter in Ruby (and Rails).
|
68
|
+
test_files:
|
69
|
+
- test/test_helper.rb
|
70
|
+
- test/unit/smart_month_test.rb
|