ruby_calendar 0.0.2 → 1.0.0

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: 6048b0b0dfc52fb9363ec4759390f68da7194378f601df9b81c2f17a18476957
4
- data.tar.gz: 0b33bbd7beb16225d2808679a607ef34ccab1ee8dfe646493d6bf3b989133e21
3
+ metadata.gz: 7f96346081a190002a3559eea6c5f6c4c3686f52582ff8e2a34b5f384f902e03
4
+ data.tar.gz: d81ac7c027998558e4500d55efbb68545bf7d9478f1daf2f790ee8a733f8d212
5
5
  SHA512:
6
- metadata.gz: 4dcc5f5d57e772f9baa753601ebc31976e1f1365996f81673ec370023a66a922a186d401b90be5415cd5f3af1ad0bca6c7836679d380fe4e136ca328ca94c668
7
- data.tar.gz: b1b77df11e7cf6b51ff4a50d94a6663257358608df19ed5ed50f3bb897d9bb824e9736df8c99c0a9c34bc5da35fc3011c761b9b4008d492f4a9cb3827f9aad3a
6
+ metadata.gz: c7e5d49d0d22f0ab03e324504e970322c1811a29f5106bf39f4f3340cf31da57827db8c02ef474a1df687df45227b3827f4c3179c4a826e0caf9a6317da6e7d0
7
+ data.tar.gz: fb7cf0d34589dd45329078a85a369bf26b62a0b2bb2bc652d783921b24092402e6ff8dadf01049baf76801d7ec2d6f4fa39445ce89d83cb5d1e9975a9b35689c
data/.rubocop.yml CHANGED
@@ -1,5 +1,46 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
1
3
  AllCops:
2
- TargetRubyVersion: 2.4
4
+ SuggestExtensions: false
5
+
6
+ Documentation:
7
+ Enabled: false
8
+
9
+ Gemspec/RequiredRubyVersion:
10
+ Description: 'Checks that `required_ruby_version` of gemspec is specified and equal to `TargetRubyVersion` of .rubocop.yml.'
11
+ Enabled: true
12
+ VersionAdded: '0.52'
13
+ VersionChanged: '1.22'
14
+ Include:
15
+ - '**/*.gemspec'
16
+
17
+ Layout/LineLength:
18
+ Max: 120
19
+
20
+ Metrics/AbcSize:
21
+ Max: 100
22
+
23
+ Metrics/BlockLength:
24
+ Exclude:
25
+ - 'spec/**/*'
26
+
27
+ Metrics/CyclomaticComplexity:
28
+ Max: 15
29
+
30
+ Metrics/MethodLength:
31
+ Max: 30
32
+
33
+ Metrics/PerceivedComplexity:
34
+ Max: 15
35
+
36
+ Naming/AccessorMethodName:
37
+ Exclude:
38
+ - 'lib/ruby_calendar/calendar.rb'
39
+
40
+ Naming/MethodParameterName:
41
+ Exclude:
42
+ - 'lib/ruby_calendar/month.rb'
43
+ - 'lib/ruby_calendar/year.rb'
3
44
 
4
45
  Style/StringLiterals:
5
46
  Enabled: true
@@ -9,5 +50,6 @@ Style/StringLiteralsInInterpolation:
9
50
  Enabled: true
10
51
  EnforcedStyle: double_quotes
11
52
 
12
- Layout/LineLength:
13
- Max: 120
53
+ Style/OptionalBooleanParameter:
54
+ Exclude:
55
+ - 'lib/ruby_calendar/calendar.rb'
data/.rubocop_todo.yml ADDED
File without changes
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
- ## [Unreleased]
1
+ ## [1.0.0] - 2022-1-16
2
2
 
3
- ## [0.1.0] - 2021-09-15
3
+ - Renewal module #4
4
+ - Add spec, rubocop #5
5
+ - Add GitHub workflow #6
6
+ - Change inisialize value(calendar class) #7
7
+
8
+ ## [0.0.2] - 2021-10-13
9
+
10
+ - Fixed gemspec #2
11
+ - Change method #3
12
+ - Add year method
13
+
14
+ ## [0.0.1] - 2021-09-15
4
15
 
5
16
  - Initial release
data/README.md CHANGED
@@ -17,8 +17,8 @@ $ gem install ruby_calendar
17
17
  * Output calendars
18
18
 
19
19
  ```ruby
20
- calendar = RubyCalendar::Calendar.new
21
- puts calendar.month(2021,1)
20
+ calendar = RubyCalendar::Calendar.new(2021, 1)
21
+ puts calendar.month
22
22
  # January 2021
23
23
  # Su Mo Tu We Th Fr Sa
24
24
  # 1 2
@@ -30,36 +30,72 @@ puts calendar.month(2021,1)
30
30
  # => nil
31
31
  ```
32
32
 
33
- ## Calendar class methods
33
+ ## Setting calendar methods
34
34
 
35
- ### month(year, month, w=0, l=0)
35
+ ### set_year()
36
36
 
37
- Return a month’s calendar in a multi-line string.
38
- * w: it specifies the width of the date columns which are centered(default: 0).
39
- * l: it specifies the number of lines that each week will use(default: 0).
37
+ It specifies the year for which the calendar is displayed.
38
+ - default values
39
+ - year: Date.today.year
40
+ - month: Date.today.month
41
+ - firstweekday: 0
40
42
 
41
- ### set_firstweekday()
43
+ Change by giving a number to the argument.
44
+
45
+ ex) 2021 -> 2022.
46
+ ```ruby
47
+ calendar = RubyCalendar::Calendar.new(2021, 1)
48
+ # => #<RubyCalendar::Calendar:0x00007f9a58161788 @year=2021, @month=1, @firstweekday=0>
49
+ calendar.set_year(2022)
50
+ # => #<RubyCalendar::Calendar:0x00007f9a58161788 @year=2022, @month=1, @firstweekday=0>
51
+ ```
42
52
 
53
+ ### set_month()
43
54
 
44
- * it set the first day of the week to Monday(1) or to any other weekday.
55
+ It specifies the month to display(month method) the monthly calendar(default: January).
56
+ Change by giving a number to the argument.
45
57
 
58
+ ex) 1(January) -> 1(December).
46
59
  ```ruby
47
- # at initialization 0(Sunday).
48
- calendar = RubyCalendar::Calendar.new
49
- # => #<RubyCalendar::Calendar:0x00007fd334b9c0d0 @firstweekday=0>
60
+ calendar = RubyCalendar::Calendar.new(2021, 1)
61
+ # => #<RubyCalendar::Calendar:0x00007f9a58161788 @year=2021, @month=1, @firstweekday=0>
62
+ calendar.set_month(12)
63
+ # => #<RubyCalendar::Calendar:0x00007f9a58149598 @year=2021, @month=12, @firstweekday=0>
64
+ ```
50
65
 
51
- calendar.set_firstweekday(1) # set the first day of the week to Monday
52
- puts calendar.month(2021,1)
53
- # January 2021
54
- # Mo Tu We Th Fr Sa Su
55
- # 1 2 3
56
- # 4 5 6 7 8 9 10
57
- # 11 12 13 14 15 16 17
58
- # 18 19 20 21 22 23 24
59
- # 25 26 27 28 29 30 31
60
- => nil
66
+ ### set_firstweekday()
67
+
68
+ It specifies the beginning of the week(default: Sunday).
69
+ Change by giving a number to the argument.
70
+
71
+ ex) 0(Sunday) -> 1(Monday).
72
+ ```ruby
73
+ calendar = RubyCalendar::Calendar.new(2021, 1)
74
+ # => #<RubyCalendar::Calendar:0x00007f9a58132460 @year=2021, @month=1, @firstweekday=0>
75
+ calendar.set_firstweekday(1)
76
+ # => #<RubyCalendar::Calendar:0x00007f9a58132460 @year=2021, @month=1, @firstweekday=1>
61
77
  ```
62
78
 
79
+ ## Output calendar methods
80
+
81
+ ### month(year, month, w:0, l:0)
82
+
83
+ Return a month’s calendar in a multi-line string.
84
+
85
+ - The following keyword arguments can be used.
86
+ - w: it specifies the width of the date columns which are centered(default: 0).
87
+ - l: it specifies the number of lines that each week will use(default: 0).
88
+
89
+ ### year(year, w:0, l:0, c:6, m:3)
90
+
91
+ Return a year’s calendar in a multi-line string.
92
+
93
+ - The following keyword arguments can be used.
94
+ - w: it specifies the width of the date columns which are centered(default: 0).
95
+ - l: it specifies the number of lines that each week will use(default: 0).
96
+ - c: it specifies the monthly calendar interval(default: 6).
97
+ - m: it specifies the number of months to display on one line(default: 3).
98
+
63
99
  ## Contributing
64
100
 
65
101
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby_calendar. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/saku888/ruby-calendar/blob/master/CODE_OF_CONDUCT.md).
@@ -1,82 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ruby_calendar/month"
4
+ require "ruby_calendar/year"
5
+
1
6
  module RubyCalendar
2
7
  class Calendar
8
+ include RubyCalendar::Month
9
+ include RubyCalendar::Year
10
+
3
11
  attr_accessor :firstweekday
4
12
 
5
13
  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
7
14
 
8
- def initialize
9
- @firstweekday = 0
15
+ def initialize(year = today_year, month = today_month, firstweekday: 0)
16
+ @year = year
17
+ @month = month
18
+ @firstweekday = firstweekday
10
19
  end
11
20
 
12
- def set_firstweekday(firstweekday)
13
- @firstweekday = firstweekday
21
+ def set_year(year)
22
+ @year = year
23
+ self
14
24
  end
15
25
 
16
- def month(year, month, w=0, l=0)
17
- w = [2, w].max
18
- l = [1, l].max
19
- s = format_month_name(year, month, 7*(w+1)-1)
20
- s = s.rstrip
21
- s += "\n" * l
22
- s += format_week_header(w).rstrip
23
- s += "\n" * l
24
- month_days_calendar(year, month).each_slice(7).to_a.each do |week|
25
- s += format_week_name(week, w).rstrip
26
- s += "\n" * l
27
- end
28
- s
26
+ def set_month(month)
27
+ @month = month
28
+ self
29
29
  end
30
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
31
+ def set_firstweekday(firstweekday)
32
+ @firstweekday = firstweekday
33
+ self
58
34
  end
59
35
 
60
36
  private
61
37
 
62
- def month_to_i(month)
63
- MONTH_NAMES.index(month)+1
64
- end
65
-
66
- def localized_day(format)
67
- days = []
68
- for i in 1..7 do
69
- days << (Date.new(2001,1,i).strftime(format))
70
- end
71
- days
38
+ def today_year
39
+ Date.today.year
72
40
  end
73
41
 
74
- def format_day(day, width)
75
- s = day.zero? ? '' : day.to_s.rjust(2)
76
- s.center(width)
42
+ def today_month
43
+ Date.today.month
77
44
  end
78
45
 
79
- def format_month_name(year, month, width, withyear=true)
46
+ def format_month_name(year, month, width, withyear = true)
80
47
  if withyear
81
48
  Date.new(year, month).strftime("%B #{year}").center(width)
82
49
  else
@@ -85,34 +52,39 @@ module RubyCalendar
85
52
  end
86
53
 
87
54
  def format_week_header(width)
88
- format_week_day(width).map{|day| day.center(width) }.join(' ')
55
+ format_week_day(width).map { |day| day.center(width) }.join(" ")
89
56
  end
90
57
 
91
- def format_week_day(width)
92
- names = WEEK_DAY_NAME.rotate(@firstweekday)
93
- if width >= 9
94
- names
58
+ def format_week_name(week, width)
59
+ if week.empty?
60
+ Array.new(7) { 0 }.map { |day| format_day(day, width) }.join(" ")
95
61
  else
96
- names.map{|name| name.slice(0, [3, width].min)} if width < 9
62
+ week.map { |day| format_day(day, width) }.join(" ")
97
63
  end
98
64
  end
99
65
 
66
+ def format_day(day, width)
67
+ s = day.zero? ? "" : day.to_s.rjust(2)
68
+ s.center(width)
69
+ end
70
+
100
71
  def month_days_calendar(year, month)
101
72
  firstday_wday = (Date.new(year, month, 1).wday - @firstweekday) % 7
102
73
  lastday_date = Date.new(year, month, -1).day
103
74
  lastday_wday = (6 + @firstweekday - Date.new(year, month, -1).wday) % 7
104
75
 
105
- days = Array.new(firstday_wday){0}
76
+ days = Array.new(firstday_wday) { 0 }
106
77
  days.push(*1..lastday_date)
107
- lastday_wday.times{ days.push(0) }
78
+ lastday_wday.times { days.push(0) }
108
79
  days
109
80
  end
110
81
 
111
- def format_week_name(week, width)
112
- if week.empty?
113
- Array.new(7){0}.map{|day| format_day(day, width)}.join(' ')
82
+ def format_week_day(width)
83
+ names = WEEK_DAY_NAME.rotate(@firstweekday)
84
+ if width >= 9
85
+ names
114
86
  else
115
- week.map{|day| format_day(day, width)}.join(' ')
87
+ names.map { |name| name.slice(0, [3, width].min) }
116
88
  end
117
89
  end
118
90
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyCalendar
4
+ module Month
5
+ def month(year = @year, month = @month, w: 0, l: 0)
6
+ w = [2, w].max
7
+ l = [1, l].max
8
+ s = format_month_name(year, month, 7 * (w + 1) - 1)
9
+ s = s.rstrip
10
+ s += "\n" * l
11
+ s += format_week_header(w).rstrip
12
+ s += "\n" * l
13
+ month_days_calendar(year, month).each_slice(7).to_a.each do |week|
14
+ s += format_week_name(week, w).rstrip
15
+ s += "\n" * l
16
+ end
17
+ s
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyCalendar
4
- VERSION = "0.0.2"
4
+ VERSION = "1.0.0"
5
5
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyCalendar
4
+ module Year
5
+ MONTH_NAMES = %w[January February March April May June July August September October November December].freeze
6
+
7
+ def year(year = @year, w: 2, l: 1, c: 6, m: 3)
8
+ w = [2, w].max
9
+ l = [1, l].max
10
+ c = [2, c].max
11
+ colwidth = 7 * (w + 1) - 1
12
+ calendar = []
13
+ calendar.push(year.to_s.center(colwidth * m + c * (m - 1)).rstrip)
14
+ calendar.push("\n" * l)
15
+ MONTH_NAMES.each_slice(m).to_a.each do |months|
16
+ calendar.push(months.map do |month|
17
+ format_month_name(year, month_to_i(month), colwidth, false) << (" " * c)
18
+ end.join.rstrip)
19
+ calendar.push("\n" * l)
20
+ calendar.push(months.map { format_week_header(w) << (" " * c) }.join.rstrip)
21
+ calendar.push("\n" * l)
22
+ month_array = months.map { |month| month_days_calendar(year, month_to_i(month)) }
23
+ slice_month_array = month_array.map { |array| array.each_slice(7).to_a }
24
+ max_element_count = slice_month_array.map(&:length).max - 1
25
+ replace_array = []
26
+ (0..max_element_count).each do |num|
27
+ replace_array.push(slice_month_array.map { |array| array[num].nil? ? [] : array[num] })
28
+ end
29
+ replace_array.each do |weeks|
30
+ calendar.push(weeks.map { |week| format_week_name(week, w) << (" " * c) }.join.rstrip)
31
+ calendar.push("\n" * l)
32
+ end
33
+ end
34
+ calendar.join.to_s
35
+ end
36
+
37
+ private
38
+
39
+ def month_to_i(month)
40
+ MONTH_NAMES.index(month) + 1
41
+ end
42
+ end
43
+ end
data/lib/ruby_calendar.rb CHANGED
@@ -1,6 +1,8 @@
1
- require 'date'
2
- require 'ruby_calendar/calendar'
3
- require 'ruby_calendar/version'
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "ruby_calendar/calendar"
5
+ require "ruby_calendar/version"
4
6
 
5
7
  module RubyCalendar
6
8
  class Error < StandardError; end
@@ -8,11 +8,11 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ["saku888"]
9
9
  spec.email = ["sakura.orn888@gmail.com"]
10
10
 
11
- spec.summary = %q{This is a calender of gem}
12
- spec.description = %q{This gem produces a calendar output similar to the Unix cal program}
11
+ spec.summary = "This is a calender of gem"
12
+ spec.description = "This gem produces a calendar output similar to the Unix cal program"
13
13
  spec.homepage = "https://github.com/saku888/ruby-calendar"
14
14
  spec.license = "MIT"
15
- spec.required_ruby_version = ">= 2.4.0"
15
+ spec.required_ruby_version = ">= 2.5.0"
16
16
 
17
17
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
18
  spec.metadata["homepage_uri"] = spec.homepage
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.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - saku888
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-13 00:00:00.000000000 Z
11
+ date: 2022-01-16 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:
@@ -19,6 +19,7 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - ".rspec"
21
21
  - ".rubocop.yml"
22
+ - ".rubocop_todo.yml"
22
23
  - CHANGELOG.md
23
24
  - CODE_OF_CONDUCT.md
24
25
  - Gemfile
@@ -30,7 +31,9 @@ files:
30
31
  - bin/setup
31
32
  - lib/ruby_calendar.rb
32
33
  - lib/ruby_calendar/calendar.rb
34
+ - lib/ruby_calendar/month.rb
33
35
  - lib/ruby_calendar/version.rb
36
+ - lib/ruby_calendar/year.rb
34
37
  - ruby_calendar.gemspec
35
38
  homepage: https://github.com/saku888/ruby-calendar
36
39
  licenses:
@@ -48,7 +51,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
48
51
  requirements:
49
52
  - - ">="
50
53
  - !ruby/object:Gem::Version
51
- version: 2.4.0
54
+ version: 2.5.0
52
55
  required_rubygems_version: !ruby/object:Gem::Requirement
53
56
  requirements:
54
57
  - - ">="