ruby_calendar 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8dbda9b1b6104d2ea1d4090ef7b3727923addcdb2011fe9e4a4efe846708985
4
- data.tar.gz: e83bb3b4e9dc078ccf6eec87ab848bd8d7425a31e04adcf7dc0bb3d49c713c2e
3
+ metadata.gz: 6048b0b0dfc52fb9363ec4759390f68da7194378f601df9b81c2f17a18476957
4
+ data.tar.gz: 0b33bbd7beb16225d2808679a607ef34ccab1ee8dfe646493d6bf3b989133e21
5
5
  SHA512:
6
- metadata.gz: 78d09f93fde491dd14fcdc8b4f1b7df37432db3cecbb7f27ca59f7cc330ea2278ad815151fbe985c06c97e42a3a008cb0466613fb16cbcd8098f57b3aff536e5
7
- data.tar.gz: 2ccb12cb15861beb4d61c0cd04fa26d3efa8f0b3a88dde480ecea6b8928575023716e23d9fe28de5830e034bcc3166c57df3a47d35e8962573b3aaaa1d7dd260
6
+ metadata.gz: 4dcc5f5d57e772f9baa753601ebc31976e1f1365996f81673ec370023a66a922a186d401b90be5415cd5f3af1ad0bca6c7836679d380fe4e136ca328ca94c668
7
+ data.tar.gz: b1b77df11e7cf6b51ff4a50d94a6663257358608df19ed5ed50f3bb897d9bb824e9736df8c99c0a9c34bc5da35fc3011c761b9b4008d492f4a9cb3827f9aad3a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby_calendar (0.0.1)
4
+ ruby_calendar (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -18,7 +18,7 @@ $ gem install ruby_calendar
18
18
 
19
19
  ```ruby
20
20
  calendar = RubyCalendar::Calendar.new
21
- puts calendar.format_month(2021,1)
21
+ puts calendar.month(2021,1)
22
22
  # January 2021
23
23
  # Su Mo Tu We Th Fr Sa
24
24
  # 1 2
@@ -32,7 +32,7 @@ puts calendar.format_month(2021,1)
32
32
 
33
33
  ## Calendar class methods
34
34
 
35
- ### format_month(year, month, w=0, l=0)
35
+ ### month(year, month, w=0, l=0)
36
36
 
37
37
  Return a month’s calendar in a multi-line string.
38
38
  * w: it specifies the width of the date columns which are centered(default: 0).
@@ -49,7 +49,7 @@ calendar = RubyCalendar::Calendar.new
49
49
  # => #<RubyCalendar::Calendar:0x00007fd334b9c0d0 @firstweekday=0>
50
50
 
51
51
  calendar.set_firstweekday(1) # set the first day of the week to Monday
52
- puts calendar.format_month(2021,1)
52
+ puts calendar.month(2021,1)
53
53
  # January 2021
54
54
  # Mo Tu We Th Fr Sa Su
55
55
  # 1 2 3
@@ -3,6 +3,7 @@ module RubyCalendar
3
3
  attr_accessor :firstweekday
4
4
 
5
5
  WEEK_DAY_NAME = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday].freeze
6
+ MONTH_NAMES = %w[January February March April May June July August September October November December].freeze
6
7
 
7
8
  def initialize
8
9
  @firstweekday = 0
@@ -12,7 +13,7 @@ module RubyCalendar
12
13
  @firstweekday = firstweekday
13
14
  end
14
15
 
15
- def format_month(year, month, w=0, l=0)
16
+ def month(year, month, w=0, l=0)
16
17
  w = [2, w].max
17
18
  l = [1, l].max
18
19
  s = format_month_name(year, month, 7*(w+1)-1)
@@ -27,8 +28,41 @@ module RubyCalendar
27
28
  s
28
29
  end
29
30
 
31
+ def calendar(year, w=2, l=1, c=6, m=3)
32
+ w = [2, w].max
33
+ l = [1, l].max
34
+ c = [2, c].max
35
+ colwidth = 7*(w+1)-1
36
+ a = []
37
+ a.push(year.to_s.center(colwidth*m+c*(m-1)).rstrip)
38
+ a.push("\n"*l)
39
+ header = format_week_header(w)
40
+ MONTH_NAMES.each_slice(m).to_a.each do |months|
41
+ a.push(months.map{|month| format_month_name(year, month_to_i(month), colwidth, false) << (' '*c)}.join.rstrip)
42
+ a.push("\n"*l)
43
+ a.push(months.map{format_week_header(w) << (' '*c)}.join.rstrip)
44
+ a.push("\n"*l)
45
+ month_array = months.map{|month| month_days_calendar(year, month_to_i(month))}
46
+ slice_month_array = month_array.map{|array| array.each_slice(7).to_a}
47
+ max_element_count = slice_month_array.map{|array| array.length}.max - 1
48
+ replace_array = []
49
+ for num in 0..max_element_count do
50
+ replace_array.push(slice_month_array.map {|array| array[num].nil? ? [] : array[num]})
51
+ end
52
+ replace_array.each do |weeks|
53
+ a.push(weeks.map{|week| format_week_name(week, w) << (' '*c)}.join.rstrip)
54
+ a.push("\n"*l)
55
+ end
56
+ end
57
+ a
58
+ end
59
+
30
60
  private
31
61
 
62
+ def month_to_i(month)
63
+ MONTH_NAMES.index(month)+1
64
+ end
65
+
32
66
  def localized_day(format)
33
67
  days = []
34
68
  for i in 1..7 do
@@ -42,8 +76,12 @@ module RubyCalendar
42
76
  s.center(width)
43
77
  end
44
78
 
45
- def format_month_name(year, month, width)
46
- Date.new(year, month).strftime("%B #{year}").center(width)
79
+ def format_month_name(year, month, width, withyear=true)
80
+ if withyear
81
+ Date.new(year, month).strftime("%B #{year}").center(width)
82
+ else
83
+ Date.new(year, month).strftime("%B").center(width)
84
+ end
47
85
  end
48
86
 
49
87
  def format_week_header(width)
@@ -62,13 +100,20 @@ module RubyCalendar
62
100
  def month_days_calendar(year, month)
63
101
  firstday_wday = (Date.new(year, month, 1).wday - @firstweekday) % 7
64
102
  lastday_date = Date.new(year, month, -1).day
103
+ lastday_wday = (6 + @firstweekday - Date.new(year, month, -1).wday) % 7
65
104
 
66
105
  days = Array.new(firstday_wday){0}
67
106
  days.push(*1..lastday_date)
107
+ lastday_wday.times{ days.push(0) }
108
+ days
68
109
  end
69
110
 
70
111
  def format_week_name(week, width)
71
- week.map{|day| format_day(day, width)}.join(' ')
112
+ if week.empty?
113
+ Array.new(7){0}.map{|day| format_day(day, width)}.join(' ')
114
+ else
115
+ week.map{|day| format_day(day, width)}.join(' ')
116
+ end
72
117
  end
73
118
  end
74
119
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyCalendar
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
@@ -5,20 +5,19 @@ require_relative "lib/ruby_calendar/version"
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "ruby_calendar"
7
7
  spec.version = RubyCalendar::VERSION
8
- spec.authors = ["sakura888"]
8
+ spec.authors = ["saku888"]
9
9
  spec.email = ["sakura.orn888@gmail.com"]
10
10
 
11
11
  spec.summary = %q{This is a calender of gem}
12
12
  spec.description = %q{This gem produces a calendar output similar to the Unix cal program}
13
- spec.homepage = "https://github.com/sakura888/ruby_calendar"
13
+ spec.homepage = "https://github.com/saku888/ruby-calendar"
14
14
  spec.license = "MIT"
15
15
  spec.required_ruby_version = ">= 2.4.0"
16
16
 
17
17
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
-
19
18
  spec.metadata["homepage_uri"] = spec.homepage
20
- spec.metadata["source_code_uri"] = "https://github.com/sakura888/ruby_calendar"
21
- spec.metadata["changelog_uri"] = "https://github.com/sakura888/calender/blob/main/CHANGELOG.md"
19
+ spec.metadata["source_code_uri"] = "https://github.com/saku888/ruby-calendar"
20
+ spec.metadata["changelog_uri"] = "https://github.com/saku888/ruby-calendar/blob/master/CHANGELOG.md"
22
21
 
23
22
  # Specify which files should be added to the gem when it is released.
24
23
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_calendar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
- - sakura888
7
+ - saku888
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-16 00:00:00.000000000 Z
11
+ date: 2021-10-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem produces a calendar output similar to the Unix cal program
14
14
  email:
@@ -32,14 +32,14 @@ files:
32
32
  - lib/ruby_calendar/calendar.rb
33
33
  - lib/ruby_calendar/version.rb
34
34
  - ruby_calendar.gemspec
35
- homepage: https://github.com/sakura888/ruby_calendar
35
+ homepage: https://github.com/saku888/ruby-calendar
36
36
  licenses:
37
37
  - MIT
38
38
  metadata:
39
39
  allowed_push_host: https://rubygems.org
40
- homepage_uri: https://github.com/sakura888/ruby_calendar
41
- source_code_uri: https://github.com/sakura888/ruby_calendar
42
- changelog_uri: https://github.com/sakura888/calender/blob/main/CHANGELOG.md
40
+ homepage_uri: https://github.com/saku888/ruby-calendar
41
+ source_code_uri: https://github.com/saku888/ruby-calendar
42
+ changelog_uri: https://github.com/saku888/ruby-calendar/blob/master/CHANGELOG.md
43
43
  post_install_message:
44
44
  rdoc_options: []
45
45
  require_paths: