goldman 0.1.3 → 0.1.5

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: c43ad648ae8b70aa4fc9c685d49c4e4a51dfc5fcf8502568591a232f6456f58b
4
- data.tar.gz: ed94b8ab0a2d8275e4dccba56a85f54e987afad71973086052727c545a92037f
3
+ metadata.gz: 1b2b295e5ec720d4b21567f3dab4a266f24a65e765e93941122d2291f86aeb11
4
+ data.tar.gz: 522073bb4ad24a862c3554d8232ac7de700fc3f187d07a76439f8566bc9e9908
5
5
  SHA512:
6
- metadata.gz: a67132dae664ff6501afddcdf2a04098b2e74bb7aabde76422e01b7df80e40d8f02685a7ecb862705f7b848df1e4a8d80f7c050ff87a0cd20623b3f54c2abb7f
7
- data.tar.gz: b0cbf1313bef97ab2907743aed72b9e7113accf6c009ca21e46d3823cf67fee04a5ef7bad5a42bc16cf5d6b362cc316fee2e10f0815aac4d8f2d44845048966c
6
+ metadata.gz: 7619fed6a8146dd32bb1a4993c806addca42c948c0877291d78ad215abadee958b2da15b701a8dce477637aeba1dce58e8a0731b9fdd8e5bbe3a003ffb6f2af2
7
+ data.tar.gz: 1eec49aba2251e932cbe5a57863e7e693903779290ed3db6a22fdfb026ac962521d4a63a128ca189412bab3685e6ce06712110d276d20d4afb98a0e6affebf3e
data/exe/goldman CHANGED
@@ -33,19 +33,26 @@ def error(message)
33
33
  end
34
34
 
35
35
  begin
36
- date_list = Goldman::DateRange.new(start_date_str: @options[:start_date],
37
- end_date_str: @options[:end_date],
38
- day_str: @options[:day],
39
- week_str: @options[:week]).date_list
40
- config = Goldman::Config.new(@options[:config])
41
- formatter = Goldman::Formatter.new(config)
42
- data = Goldman::DataLoader.new(config).data
43
- generator = Goldman::Generator.new(data, formatter, date_list)
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
+ list = Goldman::ScheduleList.new(
45
+ data: config.data,
46
+ dates: dates
47
+ ).generate
44
48
 
45
49
  @options.each do |k, v|
46
50
  case k
47
51
  when :generate
48
- generator.exec
52
+ Goldman::Presenter.print_schedule_list(
53
+ list: list,
54
+ formatter: formatter
55
+ )
49
56
  when *NON_OPERATIONAL_OPTS
50
57
  # Do nothing here
51
58
  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]
@@ -14,10 +14,12 @@ module Goldman
14
14
  validate!
15
15
  end
16
16
 
17
- def date_list
17
+ def to_array
18
18
  (@start_date..@end_date).to_a
19
19
  end
20
20
 
21
+ alias_method :to_a, :to_array
22
+
21
23
  private
22
24
 
23
25
  def calculate_end_date
@@ -31,11 +33,13 @@ module Goldman
31
33
  end
32
34
 
33
35
  def validate!
34
- end_date_err_msg = "The parameter of week (#{@week}) or day (#{@day}) is invalid."
35
- raise Goldman::ParameterError.new(end_date_err_msg) if @end_date.nil?
36
-
37
- date_range_err_msg = "The start_date (#{@start_date}) should be before end_date (#{@end_date})."
38
- raise Goldman::ParameterError.new(date_range_err_msg) if @start_date > @end_date
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
39
43
  end
40
44
 
41
45
  def to_date!(str, source)
@@ -4,13 +4,21 @@ 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
- def schedule(date, time_str)
13
- puts format(:schedule) % { date: formatted_date(date), wday: formatted_wday(date.cwday), time: time_str }
12
+ def schedule(date, option)
13
+ {
14
+ date: formatted_date(date),
15
+ wday: formatted_wday(date.cwday),
16
+ time: option
17
+ }
18
+ end
19
+
20
+ def schedule_format
21
+ format(:schedule)
14
22
  end
15
23
 
16
24
  def format(target)
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ module Goldman
6
+ class Presenter
7
+ class << self
8
+ def print_schedule_list(list:, formatter:)
9
+ print_no_schedule and return if list.empty?
10
+
11
+ list.each do |s|
12
+ puts formatter.schedule_format % formatter.schedule(s.date, s.option)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def print_no_schedule
19
+ puts "There is no schedule in the period."
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Goldman
4
+ class ScheduleList
5
+ Schedule = Struct.new(:date, :option)
6
+
7
+ def initialize(data:, dates:)
8
+ @data = data
9
+ @dates = dates
10
+ end
11
+
12
+ def generate
13
+ return [] if @data.nil? || @data.empty?
14
+
15
+ list = []
16
+ @dates.each do |date|
17
+ options = select_options_by_date(date)
18
+ next if options.empty?
19
+ sorted_options = sort(options)
20
+ sorted_options.each do |option|
21
+ list << Schedule.new(date, option)
22
+ end
23
+ end
24
+
25
+ list
26
+ end
27
+
28
+ private
29
+
30
+ def select_options_by_date(date)
31
+ options = []
32
+ wday_name = Goldman::Wday.to_name(date.cwday)
33
+ options += wday(wday_name) unless wday(wday_name).nil?
34
+ options += everyday unless everyday.nil?
35
+ options += weekday if Goldman::Wday.weekday?(wday_name) && !weekday.nil?
36
+ options += weekend if Goldman::Wday.weekend?(wday_name) && !weekend.nil?
37
+
38
+ return options
39
+ end
40
+
41
+ def sort(options)
42
+ options.sort_by { _1.split(":").first.to_i }
43
+ end
44
+
45
+ def wday(name)
46
+ @data[name]
47
+ end
48
+
49
+ def weekday
50
+ @data[:Weekday]
51
+ end
52
+
53
+ def weekend
54
+ @data[:Weekend]
55
+ end
56
+
57
+ def everyday
58
+ @data[:Everyday]
59
+ end
60
+ end
61
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Goldman
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
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
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_list"
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.3
4
+ version: 0.1.5
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-12 00:00:00.000000000 Z
11
+ date: 2023-05-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: goldman for generating option list of schedule
14
14
  email:
@@ -25,13 +25,12 @@ files:
25
25
  - README.md
26
26
  - Rakefile
27
27
  - exe/goldman
28
- - goldman.gemspec
29
28
  - lib/goldman.rb
30
29
  - lib/goldman/config.rb
31
- - lib/goldman/data_loader.rb
32
30
  - lib/goldman/date_range.rb
33
31
  - lib/goldman/formatter.rb
34
- - lib/goldman/generator.rb
32
+ - lib/goldman/presenter.rb
33
+ - lib/goldman/schedule_list.rb
35
34
  - lib/goldman/version.rb
36
35
  - lib/goldman/wday.rb
37
36
  homepage: https://github.com/willsmile/goldman-rb
data/goldman.gemspec DELETED
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/goldman/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "goldman"
7
- spec.version = Goldman::VERSION
8
- spec.authors = ["Wei Chen (willsmile)"]
9
- spec.email = ["willsmile.me@gmail.com"]
10
-
11
- spec.summary = %q{goldman}
12
- spec.description = %q{goldman for generating option list of schedule}
13
- spec.homepage = "https://github.com/willsmile/goldman-rb"
14
- spec.license = "MIT"
15
- spec.required_ruby_version = ">= 3.0.0"
16
-
17
- # Specify which files should be added to the gem when it is released.
18
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
- spec.files = Dir.chdir(__dir__) do
20
- `git ls-files -z`.split("\x0").reject do |f|
21
- (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
22
- end
23
- end
24
- spec.bindir = "exe"
25
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
- spec.require_paths = ["lib"]
27
-
28
- # Uncomment to register a new dependency of your gem
29
- # spec.add_dependency "example-gem", "~> 1.0"
30
-
31
- # For more information and examples about making a new gem, check out our
32
- # guide at: https://bundler.io/guides/creating_gem.html
33
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Goldman
4
- class DataLoader
5
- def initialize(config)
6
- @raw_data = config.raw_data
7
- end
8
-
9
- def data
10
- result = {}
11
- Goldman::Wday.members.each do |w|
12
- periods = []
13
- periods += wday(w) unless wday(w).nil?
14
- periods += everyday unless everyday.nil?
15
- periods += weekday if Goldman::Wday.weekday?(w) && !weekday.nil?
16
- periods += weekend if Goldman::Wday.weekend?(w) && !weekend.nil?
17
- result[Goldman::Wday.to_num(w)] = sort(periods) unless periods.empty?
18
- end
19
-
20
- result
21
- end
22
-
23
- private
24
-
25
- def sort(periods)
26
- periods.sort_by { _1.split(":").first.to_i }
27
- end
28
-
29
- def wday(name)
30
- @raw_data[name]
31
- end
32
-
33
- def weekday
34
- @raw_data[:Weekday]
35
- end
36
-
37
- def weekend
38
- @raw_data[:Weekend]
39
- end
40
-
41
- def everyday
42
- @raw_data[:Everyday]
43
- end
44
- end
45
- 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_list)
8
- @data = data
9
- @formatter = formatter
10
- @date_list = date_list
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_list.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