goldman 0.1.2 → 0.1.4

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: 313cb238421a3fb15dc26ca2c6ee4ccb12016fa177ffe50070d34c4e71f5c43b
4
- data.tar.gz: 33440dc517e091c34a781d474eb902e7225e431c99689b7cf4ace6dfa46b2c12
3
+ metadata.gz: 8f773ac07453545627e3f47a174af08c8260d8e0146861adeea9808bc7d174ea
4
+ data.tar.gz: 788b04e2dba00c069d331ae648841b4ffd156b70639d4773a2cca000014625df
5
5
  SHA512:
6
- metadata.gz: 0b751499deeeb6e35928493b41a559287c04c9933d3be4b6d0e4d9b6c5ab4afa3aff4e0d43efbb34b8dd25f312e1e677b30c0eb73b57f4a9366f3d62a86026dc
7
- data.tar.gz: 9329e65c929da794f4a2da183e34b21dae32e50699659cd449496a3f3345ba6be68ee09646158eb94288acb18800df9c6cae154f9b6635bbded5a9dc97644828
6
+ metadata.gz: 7c6bf7496046d91315c3344809ea010655d2078d9aa12932db6bb1f364b4c8d17e8b63ccccddf150f220fadc883e156160ad06f0f64b6e32f33310b97380d1b8
7
+ data.tar.gz: 3dae0f42687a4777fff508003d1f6d30055e1e22b71831787d19a110f45e198e1053a21aa0c2a790ac298ae279ac751752f4c5259f206eca5dc481be1715a46f
data/Rakefile CHANGED
@@ -14,3 +14,21 @@ require "rubocop/rake_task"
14
14
  RuboCop::RakeTask.new
15
15
 
16
16
  task default: %i[test rubocop]
17
+
18
+ namespace "gem" do
19
+ desc "Build a gem package"
20
+ task :build do
21
+ sh "gem build goldman.gemspec"
22
+ end
23
+
24
+ desc "Push a gem package"
25
+ task :push do
26
+ Rake::Task["gem:build"].execute
27
+ sh "gem push goldman-#{Goldman::VERSION}.gem"
28
+ end
29
+
30
+ desc "Clean built gem packages"
31
+ task :clean do
32
+ sh "rm goldman-*.gem"
33
+ end
34
+ end
data/exe/goldman CHANGED
@@ -33,16 +33,25 @@ def error(message)
33
33
  end
34
34
 
35
35
  begin
36
- date_form = Goldman::DateForm.new(@options)
37
- config = Goldman::Config.new(@options[:config])
38
- formatter = Goldman::Formatter.new(config)
39
- data = Goldman::DataLoader.new(config).data
40
- generator = Goldman::Generator.new(data, formatter, date_form)
36
+ dates = Goldman::DateRange.new(
37
+ start_date_str: @options[:start_date],
38
+ end_date_str: @options[:end_date],
39
+ day_str: @options[:day],
40
+ week_str: @options[:week]
41
+ ).to_a
42
+ config = Goldman::Config.new(path: @options[:config])
43
+ formatter = Goldman::Formatter.new(customized: config.format)
44
+ schedules = Goldman::Schedule.new(data: config.data).generate
45
+ presenter = Goldman::Presenter.new(
46
+ schedules: schedules,
47
+ formatter: formatter,
48
+ dates: dates
49
+ )
41
50
 
42
51
  @options.each do |k, v|
43
52
  case k
44
53
  when :generate
45
- generator.exec
54
+ presenter.exec
46
55
  when *NON_OPERATIONAL_OPTS
47
56
  # Do nothing here
48
57
  when :version
@@ -4,13 +4,13 @@ require "psych"
4
4
 
5
5
  module Goldman
6
6
  class Config
7
- attr_reader :raw_data, :customized_format
7
+ attr_reader :data, :format
8
8
 
9
- def initialize(path)
9
+ def initialize(path:)
10
10
  @path = path
11
11
  @config = load_file
12
- @raw_data = load_data
13
- @customized_format = load_customized_format
12
+ @data = load_data
13
+ @format = load_format
14
14
  end
15
15
 
16
16
  private
@@ -25,7 +25,7 @@ module Goldman
25
25
  @config[:data]
26
26
  end
27
27
 
28
- def load_customized_format
28
+ def load_format
29
29
  return {} unless @config.has_key?(:format)
30
30
 
31
31
  @config[:format]
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ module Goldman
6
+ class DateRange
7
+ attr_reader :start_date, :end_date
8
+
9
+ def initialize(start_date_str:, end_date_str:, day_str:, week_str:)
10
+ @week = to_integer!(week_str, "week") || default_week
11
+ @day = to_integer!(day_str, "day") || default_day
12
+ @start_date = to_date!(start_date_str, "start_date") || default_start_date
13
+ @end_date = to_date!(end_date_str, "end_date") || calculate_end_date
14
+ validate!
15
+ end
16
+
17
+ def to_array
18
+ (@start_date..@end_date).to_a
19
+ end
20
+
21
+ alias_method :to_a, :to_array
22
+
23
+ private
24
+
25
+ def calculate_end_date
26
+ if @week > 0
27
+ @start_date + @week * 7 - 1
28
+ elsif @day > 0
29
+ @start_date + @day - 1
30
+ else
31
+ nil
32
+ end
33
+ end
34
+
35
+ def validate!
36
+ if @end_date.nil?
37
+ end_date_err_msg = "The parameter of week (#{@week}) or day (#{@day}) is invalid."
38
+ raise Goldman::ParameterError.new(end_date_err_msg)
39
+ elsif @start_date > @end_date
40
+ date_range_err_msg = "The start_date (#{@start_date}) should be before end_date (#{@end_date})."
41
+ raise Goldman::ParameterError.new(date_range_err_msg)
42
+ end
43
+ end
44
+
45
+ def to_date!(str, source)
46
+ return if str.nil?
47
+
48
+ begin
49
+ Date.parse(str)
50
+ rescue Date::Error, TypeError
51
+ raise Goldman::ParameterError.new("The parameter of #{source} (#{str}) is invalid.")
52
+ end
53
+ end
54
+
55
+ def to_integer!(str, source)
56
+ return if str.nil?
57
+
58
+ begin
59
+ Integer(str)
60
+ rescue ArgumentError, TypeError
61
+ raise Goldman::ParameterError.new("The parameter of #{source} (#{str}) is invalid.")
62
+ end
63
+ end
64
+
65
+ def default_start_date
66
+ Date.today
67
+ end
68
+
69
+ def default_week
70
+ 0
71
+ end
72
+
73
+ def default_day
74
+ 7
75
+ end
76
+ end
77
+ end
@@ -4,9 +4,9 @@ module Goldman
4
4
  class Formatter
5
5
  attr_reader :customized_format, :default_format
6
6
 
7
- def initialize(config)
8
- @customized_format = config.customized_format
9
- @default_format = DEFAULT_FORMAT
7
+ def initialize(customized:, default: DEFAULT_FORMAT)
8
+ @customized_format = customized
9
+ @default_format = default
10
10
  end
11
11
 
12
12
  def schedule(date, time_str)
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ module Goldman
6
+ class Presenter
7
+ def initialize(schedules:, formatter:, dates:)
8
+ @schedules = schedules
9
+ @formatter = formatter
10
+ @dates = dates
11
+ end
12
+
13
+ def exec
14
+ raise Goldman::OperationError.new("No data for schedule. Please define them in the config file.") \
15
+ if @schedules.empty?
16
+
17
+ @dates.each do |date|
18
+ @schedules[date.cwday]&.each do |time|
19
+ @formatter.schedule(date, time)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,12 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Goldman
4
- class DataLoader
5
- def initialize(config)
6
- @raw_data = config.raw_data
4
+ class Schedule
5
+
6
+ def initialize(data:)
7
+ @data = data
7
8
  end
8
9
 
9
- def data
10
+ def generate
10
11
  result = {}
11
12
  Goldman::Wday.members.each do |w|
12
13
  periods = []
@@ -20,26 +21,24 @@ module Goldman
20
21
  result
21
22
  end
22
23
 
23
- private
24
-
25
24
  def sort(periods)
26
25
  periods.sort_by { _1.split(":").first.to_i }
27
26
  end
28
27
 
29
28
  def wday(name)
30
- @raw_data[name]
29
+ @data[name]
31
30
  end
32
31
 
33
32
  def weekday
34
- @raw_data[:Weekday]
33
+ @data[:Weekday]
35
34
  end
36
35
 
37
36
  def weekend
38
- @raw_data[:Weekend]
37
+ @data[:Weekend]
39
38
  end
40
39
 
41
40
  def everyday
42
- @raw_data[:Everyday]
41
+ @data[:Everyday]
43
42
  end
44
43
  end
45
44
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Goldman
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.4"
5
5
 
6
6
  def self.version
7
7
  puts "Goldman version #{VERSION}"
data/lib/goldman.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "goldman/config"
4
- require_relative "goldman/data_loader"
5
- require_relative "goldman/date_form"
4
+ require_relative "goldman/date_range"
6
5
  require_relative "goldman/formatter"
7
- require_relative "goldman/generator"
6
+ require_relative "goldman/presenter"
7
+ require_relative "goldman/schedule"
8
8
  require_relative "goldman/version"
9
9
  require_relative "goldman/wday"
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goldman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wei Chen (willsmile)
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-08 00:00:00.000000000 Z
11
+ date: 2023-05-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: goldman for generating option list of schedule
14
14
  email:
@@ -28,10 +28,10 @@ files:
28
28
  - goldman.gemspec
29
29
  - lib/goldman.rb
30
30
  - lib/goldman/config.rb
31
- - lib/goldman/data_loader.rb
32
- - lib/goldman/date_form.rb
31
+ - lib/goldman/date_range.rb
33
32
  - lib/goldman/formatter.rb
34
- - lib/goldman/generator.rb
33
+ - lib/goldman/presenter.rb
34
+ - lib/goldman/schedule.rb
35
35
  - lib/goldman/version.rb
36
36
  - lib/goldman/wday.rb
37
37
  homepage: https://github.com/willsmile/goldman-rb
@@ -1,64 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "date"
4
-
5
- module Goldman
6
- class DateForm
7
- def initialize(options)
8
- @week_num = parse_num!(options[:week], "week")
9
- @day_num = parse_num!(options[:day], "day") || default_day_num
10
- @start_date = parse_date!(options[:start_date], "start_date") || today
11
- @end_date = parse_date!(options[:end_date], "end_date")
12
- validate_end_date!
13
- end
14
-
15
- def range
16
- return @start_date..@end_date if @end_date
17
-
18
- @start_date..(@start_date + interval_day)
19
- end
20
-
21
- def interval_day
22
- return @week_num * 7 - 1 if @week_num
23
-
24
- @day_num - 1
25
- end
26
-
27
- private
28
-
29
- def validate_end_date!
30
- return if @end_date.nil?
31
-
32
- err_msg = "The date in start_date (#{@start_date}) should be before the date in end_date (#{@end_date})."
33
- raise Goldman::ParameterError.new(err_msg) if @start_date > @end_date
34
- end
35
-
36
- def parse_date!(str, source)
37
- return if str.nil?
38
-
39
- begin
40
- Date.parse(str)
41
- rescue Date::Error, TypeError
42
- raise Goldman::ParameterError.new("The parameter of #{source} (#{str}) is invalid.")
43
- end
44
- end
45
-
46
- def parse_num!(str, source)
47
- return if str.nil?
48
-
49
- begin
50
- Integer(str)
51
- rescue ArgumentError, TypeError
52
- raise Goldman::ParameterError.new("The parameter of #{source} (#{str}) is invalid.")
53
- end
54
- end
55
-
56
- def today
57
- Date.today
58
- end
59
-
60
- def default_day_num
61
- 7
62
- end
63
- end
64
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "date"
4
-
5
- module Goldman
6
- class Generator
7
- def initialize(data, formatter, date_form)
8
- @data = data
9
- @formatter = formatter
10
- @date_form = date_form
11
- end
12
-
13
- def exec
14
- raise Goldman::OperationError.new("No data for schedule. Please define them in the config file.") if @data.empty?
15
-
16
- @date_form.range.each do |date|
17
- @data[date.cwday]&.each do |time|
18
- @formatter.schedule(date, time)
19
- end
20
- end
21
- end
22
- end
23
- end