dcal 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: 929e76bf2d797ce1cdbf15b077d397d64553b887
4
+ data.tar.gz: 96a0f887abd1de03f012d48cf5847e2cdfaf4b8c
5
+ !binary "U0hBNTEy":
6
+ metadata.gz: 294834412be578f0cd64c8b357f35f9049dfdf74dcb0f23866938f1100f366f1910d5f95c422ca514ef5885f81b4f36a522ac287da529925f44fe07014fc1b3e
7
+ data.tar.gz: ad815acb41453c041b765fa9da9ed62c17dcf772c22cbb93b88745a4b2e32d2894beb1fb12e72a51919e720b539b64631855a8ae598c11e431588d4e34b95461
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Chun-wei Kuo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ # DCal
2
+
3
+ Just a stupid `cal`.
4
+
5
+ ## Installation
6
+
7
+ Please do not install it.
8
+
9
+ If you insist to do so:
10
+
11
+ $ gem install dcal
12
+
13
+ ## Usage
14
+
15
+ The `dcal` command would be installed with the gem.
16
+
17
+ It works almost like the `cal` command, but stupider.
18
+
19
+ Provide it the numbers of month and year:
20
+
21
+ $ dcal 12 2012
22
+
23
+ And the output would be like:
24
+
25
+ ```
26
+ December 2012
27
+ Su Mo Tu We Th Fr Sa
28
+ 1
29
+ 2 3 4 5 6 7 8
30
+ 9 10 11 12 13 14 15
31
+ 16 17 18 19 20 21 22
32
+ 23 24 25 26 27 28 29
33
+ 30 31
34
+ ```
35
+
36
+ No any other fancier functionality.
37
+
38
+ ## Compatibility
39
+
40
+ Compatible with Ruby 2.0.0-dev.
41
+
42
+ Please install Bundler 1.3.0+ by running:
43
+
44
+ $ gem install bundler --pre
45
+
46
+ ## Todo
47
+
48
+ This section is intentionally left blank.
49
+
50
+ ## License
51
+
52
+ Copyright (c) 2012 Chun-wei Kuo
53
+
54
+ MIT License
55
+
56
+ See [LICENSE] for futher details.
57
+
58
+ [LICENSE]: https://github.com/Domon/dcal/blob/master/LICENSE.txt
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.pattern = 'test/**/test_*.rb'
8
+ end
9
+
10
+ desc "Run tests"
11
+ task :default => :test
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+
4
+ require 'dcal'
5
+
6
+ month = ARGV[0]
7
+ year = ARGV[1]
8
+
9
+ unless month && year
10
+ puts "usage: cal month year"
11
+ exit
12
+ end
13
+
14
+ begin
15
+ cal = DCal::Cal.new month, year
16
+ print cal.output
17
+ rescue RangeError => e
18
+ puts "dcal: #{e.message}"
19
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dcal/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'dcal'
8
+ gem.version = DCal::VERSION
9
+ gem.summary = 'Just a stupid cal.'
10
+ gem.description = 'Just a stupid cal.'
11
+
12
+ gem.license = 'MIT'
13
+
14
+ gem.author = 'Chun-wei Kuo'
15
+ gem.email = 'Dendoh@gmail.com'
16
+ gem.homepage = 'https://github.com/Domon/dcal'
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^test/})
21
+ gem.require_path = 'lib'
22
+ end
@@ -0,0 +1,5 @@
1
+ require 'dcal/version'
2
+ require 'dcal/cal'
3
+
4
+ module DCal
5
+ end
@@ -0,0 +1,55 @@
1
+ require 'dcal/month'
2
+
3
+ module DCal
4
+ class Cal
5
+ ABBR_DAYNAMES = %w(Su Mo Tu We Th Fr Sa)
6
+
7
+ def initialize(month, year)
8
+ @month = DCal::Month.new(month, year)
9
+ @year = DCal::Year.new(year)
10
+ end
11
+
12
+ def output
13
+ title + "\n" +
14
+ header + "\n" +
15
+ weeks + "\n"
16
+ end
17
+
18
+ def month
19
+ @month.month
20
+ end
21
+
22
+ def year
23
+ @year
24
+ end
25
+
26
+ private
27
+
28
+ def title
29
+ "#{@month.name} #{@year}".center(20)
30
+ end
31
+
32
+ def header
33
+ ABBR_DAYNAMES.join(" ")
34
+ end
35
+
36
+ def weeks
37
+ weeks = cells.each_slice(7).to_a
38
+ weeks.map { |week| week.join(" ") }.join("\n")
39
+ end
40
+
41
+ def cells
42
+ cells = Array.new(42, " ")
43
+
44
+ days = 1..@month.days_in_month
45
+ start = @month.wday_of_first_day
46
+
47
+ cells.each_index do |i|
48
+ day = i - start + 1
49
+ cells[i] = day.to_s.rjust(2) if days.cover?(day)
50
+ end
51
+
52
+ cells
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,65 @@
1
+ require 'dcal/year'
2
+
3
+ module DCal
4
+ class Month
5
+ attr_reader :month, :year
6
+
7
+ MONTHNAMES = [
8
+ nil,
9
+ "January", "February", "March",
10
+ "April", "May", "June",
11
+ "July", "August", "September",
12
+ "October", "November", "December"
13
+ ]
14
+
15
+ COMMON_YEAR_DAYS_IN_MONTH = [
16
+ nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
17
+ ]
18
+
19
+ def initialize(month, year)
20
+ @month = month.to_i
21
+ @year = DCal::Year.new(year.to_i)
22
+
23
+ unless (1..12).cover? @month
24
+ raise RangeError, "#{@month} is not a month number (1..12)"
25
+ end
26
+ end
27
+
28
+ def name
29
+ MONTHNAMES[@month]
30
+ end
31
+
32
+ def days_since_1970
33
+ (@year - 1970) * 365 + @year.leap_years_since_1970 + passed_days_this_year
34
+ end
35
+
36
+ def days_in_month
37
+ return 29 if @month == 2 && @year.leap?
38
+ COMMON_YEAR_DAYS_IN_MONTH[@month]
39
+ end
40
+
41
+ def wday_of_first_day
42
+ # 1970-01-01 is Thursday (4).
43
+ (days_since_1970 + 4) % 7
44
+ end
45
+
46
+ # For inspect
47
+ def to_s
48
+ "#{@year}-#{@month}"
49
+ end
50
+
51
+ # For better output in console
52
+ def inspect
53
+ "#<#{self.class.name} #{to_s}>"
54
+ end
55
+
56
+ private
57
+
58
+ def passed_days_this_year
59
+ return 0 if @month == 1
60
+ (1...@month).reduce(0) { |sum, m| sum + DCal::Month.new(m, @year).days_in_month }
61
+ end
62
+
63
+ end
64
+ end
65
+
@@ -0,0 +1,3 @@
1
+ module DCal
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,50 @@
1
+ module DCal
2
+ class Year
3
+ include Comparable
4
+
5
+ def initialize(value)
6
+ @value = value.to_i
7
+
8
+ unless (1970..9999).cover? @value
9
+ raise RangeError, "year #{@value} not in range 1970..9999"
10
+ end
11
+ end
12
+
13
+ def leap?
14
+ # ref: http://en.wikipedia.org/wiki/Leap_year#Algorithm
15
+ @value % 400 == 0 || (@value % 100 != 0 && @value % 4 == 0)
16
+ end
17
+
18
+ def leap_years_since_1970
19
+ return 0 if @value == 1970
20
+ (1970...@value).select { |y| DCal::Year.new(y).leap? }.size
21
+ end
22
+
23
+ def <=>(other)
24
+ if other.integer?
25
+ to_i <=> other
26
+ else
27
+ to_i <=> other.to_i
28
+ end
29
+ end
30
+
31
+ def -(integer)
32
+ to_i - integer
33
+ end
34
+
35
+ # For calculations
36
+ def to_i
37
+ @value
38
+ end
39
+
40
+ # For inspect
41
+ def to_s
42
+ @value.to_s
43
+ end
44
+
45
+ # For better output in console
46
+ def inspect
47
+ "#<#{self.class.name} #{to_s}>"
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,9 @@
1
+ require 'test/unit'
2
+ require 'dcal'
3
+
4
+ class TestCal < Test::Unit::TestCase
5
+ def test_constants
6
+ assert_equal 'Su', DCal::Cal::ABBR_DAYNAMES[0]
7
+ assert_equal 7 , DCal::Cal::ABBR_DAYNAMES.size
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ require 'test/unit'
2
+ require 'dcal'
3
+
4
+ class TestCalNew < Test::Unit::TestCase
5
+ def setup
6
+ @cal = DCal::Cal.new 12, 2012
7
+ end
8
+
9
+ def test_new
10
+ assert_instance_of DCal::Cal, @cal
11
+ end
12
+
13
+ def test_month
14
+ assert_equal 12, @cal.month
15
+ end
16
+
17
+ def test_year
18
+ assert_equal 2012, @cal.year
19
+ end
20
+ end
@@ -0,0 +1,47 @@
1
+ require 'test/unit'
2
+ require 'dcal'
3
+
4
+ class TestCalOutput < Test::Unit::TestCase
5
+ def test_output
6
+ output_1_1970 = <<-OUTPUT
7
+ January 1970
8
+ Su Mo Tu We Th Fr Sa
9
+ 1 2 3
10
+ 4 5 6 7 8 9 10
11
+ 11 12 13 14 15 16 17
12
+ 18 19 20 21 22 23 24
13
+ 25 26 27 28 29 30 31
14
+
15
+ OUTPUT
16
+
17
+ output_8_1998 = <<-OUTPUT
18
+ August 1998
19
+ Su Mo Tu We Th Fr Sa
20
+ 1
21
+ 2 3 4 5 6 7 8
22
+ 9 10 11 12 13 14 15
23
+ 16 17 18 19 20 21 22
24
+ 23 24 25 26 27 28 29
25
+ 30 31
26
+ OUTPUT
27
+
28
+ output_12_2012 = <<-OUTPUT
29
+ December 2012
30
+ Su Mo Tu We Th Fr Sa
31
+ 1
32
+ 2 3 4 5 6 7 8
33
+ 9 10 11 12 13 14 15
34
+ 16 17 18 19 20 21 22
35
+ 23 24 25 26 27 28 29
36
+ 30 31
37
+ OUTPUT
38
+
39
+ cal_1_1970 = DCal::Cal.new( 1, 1970)
40
+ cal_8_1998 = DCal::Cal.new( 8, 1998)
41
+ cal_12_2012 = DCal::Cal.new(12, 2012)
42
+
43
+ assert_equal output_1_1970, cal_1_1970.output
44
+ assert_equal output_8_1998, cal_8_1998.output
45
+ assert_equal output_12_2012, cal_12_2012.output
46
+ end
47
+ end
@@ -0,0 +1,46 @@
1
+ require 'test/unit'
2
+ require 'dcal'
3
+
4
+ class TestMonth < Test::Unit::TestCase
5
+ def setup
6
+ @month = DCal::Month.new(1, 1970)
7
+ end
8
+
9
+ def test_constants
10
+ assert_nil DCal::Month::COMMON_YEAR_DAYS_IN_MONTH[0]
11
+ assert_equal 31, DCal::Month::COMMON_YEAR_DAYS_IN_MONTH[1]
12
+ assert_equal 13 , DCal::Month::COMMON_YEAR_DAYS_IN_MONTH.size
13
+
14
+ assert_nil DCal::Month::MONTHNAMES[0]
15
+ assert_equal 'January', DCal::Month::MONTHNAMES[1]
16
+ assert_equal 13 , DCal::Month::MONTHNAMES.size
17
+ end
18
+
19
+ def test_new
20
+ assert_raise(RangeError) { DCal::Month.new(13, 2000) }
21
+ end
22
+
23
+ def test_name
24
+ assert 'January', @month.name
25
+ end
26
+
27
+ def test_days_since_1970
28
+ month_1_1970 = DCal::Month.new(1, 1970)
29
+ month_12_2012 = DCal::Month.new(12, 2012)
30
+
31
+ assert_equal 0, month_1_1970.days_since_1970
32
+ assert_equal 15675, month_12_2012.days_since_1970
33
+ end
34
+
35
+ def test_days_in_month
36
+ month_1_1970 = DCal::Month.new(1, 1970)
37
+ month_2_2012 = DCal::Month.new(2, 2012)
38
+
39
+ assert_equal 31, month_1_1970.days_in_month
40
+ assert_equal 29, month_2_2012.days_in_month
41
+ end
42
+
43
+ def test_inspect
44
+ assert_equal "#<DCal::Month 1970-1>", @month.inspect
45
+ end
46
+ end
@@ -0,0 +1,53 @@
1
+ require 'test/unit'
2
+ require 'dcal'
3
+
4
+ class TestYear < Test::Unit::TestCase
5
+ def setup
6
+ @year = DCal::Year.new(2012)
7
+ end
8
+
9
+ def test_new
10
+ assert_raise(RangeError) { DCal::Year.new(1900) }
11
+ assert_raise(RangeError) { DCal::Year.new(99999) }
12
+ end
13
+
14
+ def test_comparable
15
+ assert_equal 2012, @year
16
+ end
17
+
18
+ def test_substraction
19
+ assert_equal 2011, @year - 1
20
+ end
21
+
22
+ def test_to_i
23
+ assert_equal 2012, @year.to_i
24
+ end
25
+
26
+ def test_to_s
27
+ assert_equal "2012", @year.to_s
28
+ end
29
+
30
+ def test_inspect
31
+ assert_equal "#<DCal::Year 2012>", @year.inspect
32
+ end
33
+
34
+ def test_leap
35
+ y_1970 = DCal::Year.new(1970)
36
+ y_2000 = DCal::Year.new(2000)
37
+ y_2012 = DCal::Year.new(2012)
38
+ y_2100 = DCal::Year.new(2100)
39
+
40
+ assert_equal false, y_1970.leap?
41
+ assert_equal true , y_2000.leap?
42
+ assert_equal true , y_2012.leap?
43
+ assert_equal false, y_2100.leap?
44
+ end
45
+
46
+ def test_leap_years_since_1970
47
+ y_1970 = DCal::Year.new(1970)
48
+ y_2012 = DCal::Year.new(2012)
49
+
50
+ assert_equal 0, y_1970.leap_years_since_1970
51
+ assert_equal 10, y_2012.leap_years_since_1970
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dcal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chun-wei Kuo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2012-12-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Just a stupid cal.
14
+ email: Dendoh@gmail.com
15
+ executables:
16
+ - dcal
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .gitignore
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - bin/dcal
26
+ - dcal.gemspec
27
+ - lib/dcal.rb
28
+ - lib/dcal/cal.rb
29
+ - lib/dcal/month.rb
30
+ - lib/dcal/version.rb
31
+ - lib/dcal/year.rb
32
+ - test/dcal/test_cal.rb
33
+ - test/dcal/test_cal_new.rb
34
+ - test/dcal/test_cal_output.rb
35
+ - test/dcal/test_month.rb
36
+ - test/dcal/test_year.rb
37
+ homepage: https://github.com/Domon/dcal
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.0.0.preview2.1
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Just a stupid cal.
61
+ test_files:
62
+ - test/dcal/test_cal.rb
63
+ - test/dcal/test_cal_new.rb
64
+ - test/dcal/test_cal_output.rb
65
+ - test/dcal/test_month.rb
66
+ - test/dcal/test_year.rb