goldman 0.1.4 → 0.1.6

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: 6f1879b6e3dde08040bc60cbd042d30fd6d8033b69d0324c18572001af199bc6
4
+ data.tar.gz: 2cb786913beaf9da72d3e3c9d8ea98d13930e8957bae83f47f6f159235eedccb
5
5
  SHA512:
6
- metadata.gz: 7c6bf7496046d91315c3344809ea010655d2078d9aa12932db6bb1f364b4c8d17e8b63ccccddf150f220fadc883e156160ad06f0f64b6e32f33310b97380d1b8
7
- data.tar.gz: 3dae0f42687a4777fff508003d1f6d30055e1e22b71831787d19a110f45e198e1053a21aa0c2a790ac298ae279ac751752f4c5259f206eca5dc481be1715a46f
6
+ metadata.gz: 9d43fbbf48bd7f062ed6a0fbe02f2d74d1c7da1c7aa5f8c9d186ac80bf0dbc94c660663e15144065ef3f976e576d4151f0438b466d652aafe2f866e3c6cc5fec
7
+ data.tar.gz: bbaeca4190f77c282715f486fb0a256ad5218888451d6aad5469119883ca942b4f0fbb3f4e5acd56876ab704b0fc044e25b0dfbf8a7f72b8d73bb4feee80b403
data/Rakefile CHANGED
@@ -14,21 +14,3 @@ 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
@@ -5,61 +5,10 @@
5
5
  # MIT license
6
6
 
7
7
  require "goldman"
8
- require "optparse"
9
-
10
- DEFAULT_CONFIG_FILEPATH = ENV["GOLDMAN_RB_PATH"] || File.expand_path("~/.goldman/config.yml")
11
- NON_OPERATIONAL_OPTS = [:config]
12
-
13
- # default value
14
- @options = {
15
- config: DEFAULT_CONFIG_FILEPATH
16
- }
17
-
18
- opts = OptionParser.new do |opts|
19
- opts.on("-c", "--config FILE", "Use a config file") { |v| @options[:config] = v }
20
- opts.on("-g", "--generate", "Generate list of schedule options") { |v| @options[:generate] = v }
21
- opts.on("-s", "--start [DATE]", "Set start date") { |v| @options[:start_date] = v }
22
- opts.on("-e", "--end [DATE]", "Set end date") { |v| @options[:end_date] = v }
23
- opts.on("-d", "--day [INTEGER]", "Set day interval from start date") { |v| @options[:day] = v }
24
- opts.on("-w", "--week [INTEGER]", "Set week interval from start date") { |v| @options[:week] = v }
25
- opts.on("-v", "--version", "Show tool version") { |v| @options[:version] = v }
26
-
27
- opts.parse!(ARGV)
28
- end
29
-
30
- def error(message)
31
- warn "ERROR: #{message}"
32
- exit 1
33
- end
34
8
 
35
9
  begin
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
- )
50
-
51
- @options.each do |k, v|
52
- case k
53
- when :generate
54
- presenter.exec
55
- when *NON_OPERATIONAL_OPTS
56
- # Do nothing here
57
- when :version
58
- Goldman.version
59
- end
60
- end
10
+ Goldman::Cli.new(ARGV).run
61
11
  rescue Goldman::Error => ex
62
- error ex.message
63
- rescue Errno::ENOENT
64
- error "Config file (#{@options[:config]}) dose not exist."
12
+ warn "ERROR: #{ex}"
13
+ exit 1
65
14
  end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+
5
+ module Goldman
6
+ class Cli
7
+ NON_OPERATIONAL_OPTS = [:config]
8
+
9
+ def initialize(argv)
10
+ @options = parse_argv(argv)
11
+ end
12
+
13
+ def run
14
+ @options.each do |k, v|
15
+ case k
16
+ when :generate
17
+ cmd_generate
18
+ when *NON_OPERATIONAL_OPTS
19
+ # Do nothing here
20
+ when :version
21
+ cmd_version
22
+ end
23
+ end
24
+ end
25
+
26
+ def cmd_generate
27
+ dates = Goldman::DateRange.new(
28
+ start_date_str: @options[:start_date],
29
+ end_date_str: @options[:end_date],
30
+ day_str: @options[:day],
31
+ week_str: @options[:week]
32
+ ).to_a
33
+ config = Goldman::Config.new(path: @options[:config])
34
+ formatter = Goldman::Formatter.new(customized: config.format)
35
+ list = Goldman::ScheduleList.new(
36
+ data: config.data,
37
+ dates: dates
38
+ ).generate
39
+
40
+ Goldman::Presenter.print_schedule_list(
41
+ list: list,
42
+ formatter: formatter
43
+ )
44
+ end
45
+
46
+ def cmd_version
47
+ Goldman.version
48
+ end
49
+
50
+ private
51
+
52
+ def parse_argv(argv)
53
+ # default value
54
+ options = {
55
+ config: ENV["GOLDMAN_RB_PATH"] || File.expand_path("~/.goldman/config.yml")
56
+ }
57
+
58
+ OptionParser.new do |opts|
59
+ opts.on("-c", "--config FILE", "Use a config file") { |v| options[:config] = v }
60
+ opts.on("-g", "--generate", "Generate list of schedule options") { |v| options[:generate] = v }
61
+ opts.on("-s", "--start [DATE]", "Set start date") { |v| options[:start_date] = v }
62
+ opts.on("-e", "--end [DATE]", "Set end date") { |v| options[:end_date] = v }
63
+ opts.on("-d", "--day [INTEGER]", "Set day interval from start date") { |v| options[:day] = v }
64
+ opts.on("-w", "--week [INTEGER]", "Set week interval from start date") { |v| options[:week] = v }
65
+ opts.on("-v", "--version", "Show tool version") { |v| options[:version] = v }
66
+ opts.parse!(argv)
67
+ end
68
+
69
+ return options
70
+ end
71
+ end
72
+ end
@@ -16,7 +16,11 @@ module Goldman
16
16
  private
17
17
 
18
18
  def load_file
19
- Psych.safe_load_file(@path, symbolize_names: true)
19
+ begin
20
+ Psych.safe_load_file(@path, symbolize_names: true)
21
+ rescue Errno::ENOENT
22
+ raise Goldman::ConfigurationError.new("Config file (#{@path}) dose not exist.")
23
+ end
20
24
  end
21
25
 
22
26
  def load_data
@@ -2,6 +2,20 @@
2
2
 
3
3
  module Goldman
4
4
  class Formatter
5
+ DEFAULT_FORMAT = {
6
+ schedule: "%{date}(%{wday}) %{time}",
7
+ date: "%Y/%m/%d",
8
+ wday: {
9
+ Monday: "Mon",
10
+ Tuesday: "Tue",
11
+ Wednesday: "Wed",
12
+ Thursday: "Thu",
13
+ Friday: "Fri",
14
+ Saturday: "Sat",
15
+ Sunday: "Sun"
16
+ }
17
+ }
18
+
5
19
  attr_reader :customized_format, :default_format
6
20
 
7
21
  def initialize(customized:, default: DEFAULT_FORMAT)
@@ -9,8 +23,16 @@ module Goldman
9
23
  @default_format = default
10
24
  end
11
25
 
12
- def schedule(date, time_str)
13
- puts format(:schedule) % { date: formatted_date(date), wday: formatted_wday(date.cwday), time: time_str }
26
+ def schedule(date, option)
27
+ {
28
+ date: formatted_date(date),
29
+ wday: formatted_wday(date.cwday),
30
+ time: option
31
+ }
32
+ end
33
+
34
+ def schedule_format
35
+ format(:schedule)
14
36
  end
15
37
 
16
38
  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.6"
5
5
 
6
6
  def self.version
7
7
  puts "Goldman version #{VERSION}"
data/lib/goldman.rb CHANGED
@@ -1,28 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "goldman/cli"
3
4
  require_relative "goldman/config"
4
5
  require_relative "goldman/date_range"
5
6
  require_relative "goldman/formatter"
6
7
  require_relative "goldman/presenter"
7
- require_relative "goldman/schedule"
8
+ require_relative "goldman/schedule_list"
8
9
  require_relative "goldman/version"
9
10
  require_relative "goldman/wday"
10
11
 
11
12
  module Goldman
12
- DEFAULT_FORMAT = {
13
- schedule: "%{date}(%{wday}) %{time}",
14
- date: "%Y/%m/%d",
15
- wday: {
16
- Monday: "Mon",
17
- Tuesday: "Tue",
18
- Wednesday: "Wed",
19
- Thursday: "Thu",
20
- Friday: "Fri",
21
- Saturday: "Sat",
22
- Sunday: "Sun"
23
- }
24
- }
25
-
26
13
  class Error < StandardError; end
27
14
  class ParameterError < Error; end
28
15
  class ConfigurationError < Error; end
data/pkg/.keep ADDED
File without changes
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.6
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-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: goldman for generating option list of schedule
14
14
  email:
@@ -25,15 +25,16 @@ files:
25
25
  - README.md
26
26
  - Rakefile
27
27
  - exe/goldman
28
- - goldman.gemspec
29
28
  - lib/goldman.rb
29
+ - lib/goldman/cli.rb
30
30
  - lib/goldman/config.rb
31
31
  - lib/goldman/date_range.rb
32
32
  - lib/goldman/formatter.rb
33
33
  - lib/goldman/presenter.rb
34
- - lib/goldman/schedule.rb
34
+ - lib/goldman/schedule_list.rb
35
35
  - lib/goldman/version.rb
36
36
  - lib/goldman/wday.rb
37
+ - pkg/.keep
37
38
  homepage: https://github.com/willsmile/goldman-rb
38
39
  licenses:
39
40
  - MIT
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