goldman 0.1.4 → 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: 8f773ac07453545627e3f47a174af08c8260d8e0146861adeea9808bc7d174ea
4
- data.tar.gz: 788b04e2dba00c069d331ae648841b4ffd156b70639d4773a2cca000014625df
3
+ metadata.gz: 1b2b295e5ec720d4b21567f3dab4a266f24a65e765e93941122d2291f86aeb11
4
+ data.tar.gz: 522073bb4ad24a862c3554d8232ac7de700fc3f187d07a76439f8566bc9e9908
5
5
  SHA512:
6
- metadata.gz: 7c6bf7496046d91315c3344809ea010655d2078d9aa12932db6bb1f364b4c8d17e8b63ccccddf150f220fadc883e156160ad06f0f64b6e32f33310b97380d1b8
7
- data.tar.gz: 3dae0f42687a4777fff508003d1f6d30055e1e22b71831787d19a110f45e198e1053a21aa0c2a790ac298ae279ac751752f4c5259f206eca5dc481be1715a46f
6
+ metadata.gz: 7619fed6a8146dd32bb1a4993c806addca42c948c0877291d78ad215abadee958b2da15b701a8dce477637aeba1dce58e8a0731b9fdd8e5bbe3a003ffb6f2af2
7
+ data.tar.gz: 1eec49aba2251e932cbe5a57863e7e693903779290ed3db6a22fdfb026ac962521d4a63a128ca189412bab3685e6ce06712110d276d20d4afb98a0e6affebf3e
data/exe/goldman CHANGED
@@ -41,17 +41,18 @@ begin
41
41
  ).to_a
42
42
  config = Goldman::Config.new(path: @options[:config])
43
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,
44
+ list = Goldman::ScheduleList.new(
45
+ data: config.data,
48
46
  dates: dates
49
- )
47
+ ).generate
50
48
 
51
49
  @options.each do |k, v|
52
50
  case k
53
51
  when :generate
54
- presenter.exec
52
+ Goldman::Presenter.print_schedule_list(
53
+ list: list,
54
+ formatter: formatter
55
+ )
55
56
  when *NON_OPERATIONAL_OPTS
56
57
  # Do nothing here
57
58
  when :version
@@ -9,8 +9,16 @@ module Goldman
9
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)
@@ -4,21 +4,20 @@ require "date"
4
4
 
5
5
  module Goldman
6
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?
7
+ class << self
8
+ def print_schedule_list(list:, formatter:)
9
+ print_no_schedule and return if list.empty?
16
10
 
17
- @dates.each do |date|
18
- @schedules[date.cwday]&.each do |time|
19
- @formatter.schedule(date, time)
11
+ list.each do |s|
12
+ puts formatter.schedule_format % formatter.schedule(s.date, s.option)
20
13
  end
21
14
  end
15
+
16
+ private
17
+
18
+ def print_no_schedule
19
+ puts "There is no schedule in the period."
20
+ end
22
21
  end
23
22
  end
24
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.4"
4
+ VERSION = "0.1.5"
5
5
 
6
6
  def self.version
7
7
  puts "Goldman version #{VERSION}"
data/lib/goldman.rb CHANGED
@@ -4,7 +4,7 @@ require_relative "goldman/config"
4
4
  require_relative "goldman/date_range"
5
5
  require_relative "goldman/formatter"
6
6
  require_relative "goldman/presenter"
7
- require_relative "goldman/schedule"
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.4
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-13 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
30
  - lib/goldman/date_range.rb
32
31
  - lib/goldman/formatter.rb
33
32
  - lib/goldman/presenter.rb
34
- - lib/goldman/schedule.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,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Goldman
4
- class Schedule
5
-
6
- def initialize(data:)
7
- @data = data
8
- end
9
-
10
- def generate
11
- result = {}
12
- Goldman::Wday.members.each do |w|
13
- periods = []
14
- periods += wday(w) unless wday(w).nil?
15
- periods += everyday unless everyday.nil?
16
- periods += weekday if Goldman::Wday.weekday?(w) && !weekday.nil?
17
- periods += weekend if Goldman::Wday.weekend?(w) && !weekend.nil?
18
- result[Goldman::Wday.to_num(w)] = sort(periods) unless periods.empty?
19
- end
20
-
21
- result
22
- end
23
-
24
- def sort(periods)
25
- periods.sort_by { _1.split(":").first.to_i }
26
- end
27
-
28
- def wday(name)
29
- @data[name]
30
- end
31
-
32
- def weekday
33
- @data[:Weekday]
34
- end
35
-
36
- def weekend
37
- @data[:Weekend]
38
- end
39
-
40
- def everyday
41
- @data[:Everyday]
42
- end
43
- end
44
- end