khoinguyen_eh_todo_list 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2e5ce172c4e885853d8cd8514a05ce33091813448dabc375c728dbd612766969
4
+ data.tar.gz: b6eb740003d3b1f9644feaace517edbf42ef35372804e9fdae688d0173a44f5b
5
+ SHA512:
6
+ metadata.gz: 4ed3bc6ee7660a4d6aa7685792db742b582811089175e871f94bb6bfafba64b282c4b007b7eb9fe0d6ac37f6080d4f8790f2fcbd7d6cf5bd826a2ba7ceed02ac
7
+ data.tar.gz: 5eafdafe40d7916305ea29d59738048499cb6ee2f5671bbb48b499cfa733fa5bdb669691cf1ebfc55f99702d24411ec73e91494da1b560a1d4983af545eb63ac
data/.idea/vcs.xml ADDED
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings" defaultProject="true" />
4
+ </project>
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="PropertiesComponent">{}</component>
4
+ </project>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at khoi.nguyen2@employmenthero.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Nguyen Chinh Khoi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # KhoinguyenEhKhoinguyenEhTodoList
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/khoinguyen_eh_todo_list`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/khoinguyen_eh_todo_list. 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/[USERNAME]/khoinguyen_eh_todo_list/blob/main/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the KhoinguyenEhKhoinguyenEhTodoList project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/khoinguyen_eh_todo_list/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "khoinguyen_eh_todo_list"
6
+
7
+ KhoinguyenEhTodoList::CLI.start(ARGV)
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/khoinguyen_eh_todo_list/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "khoinguyen_eh_todo_list"
7
+ spec.version = KhoinguyenEhKhoinguyenEhTodoList::VERSION
8
+ spec.authors = ["Nguyen Chinh Khoi"]
9
+ spec.email = ["khoi.nguyen2@employmenthero.com"]
10
+
11
+ spec.summary = "Ruby gem for managing a todo list"
12
+ spec.description = "This gem provides a simple way to manage a todo list."
13
+ spec.homepage = "https://github.com/khoinguyen-eh/khoinguyen_eh_todo_list"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
18
+
19
+ spec.metadata["homepage_uri"] = "https://github.com/khoinguyen-eh/khoinguyen_eh_todo_list"
20
+ spec.metadata["source_code_uri"] = "https://github.com/khoinguyen-eh/khoinguyen_eh_todo_list"
21
+ spec.metadata["changelog_uri"] = "https://github.com/khoinguyen-eh/khoinguyen_eh_todo_list"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) ||
28
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
29
+ end
30
+ end
31
+ spec.bindir = "bin"
32
+ spec.executables = ["khoinguyen_eh_todo_list"]
33
+ spec.require_paths = ["lib"]
34
+
35
+ # Uncomment to register a new dependency of your gem
36
+ # spec.add_dependency "example-gem", "~> 1.0"
37
+
38
+ # For more information and examples about making a new gem, check out our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+
41
+ spec.add_dependency "thor"
42
+ spec.add_dependency "yaml"
43
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ module KhoinguyenEhTodoList
6
+ # Caretaker class to manage the undo and redo history
7
+ class Caretaker
8
+ UNDO_FILE = "undo_history.yml"
9
+ REDO_FILE = "redo_history.yml"
10
+
11
+ def initialize
12
+ @undo_history = load_history(UNDO_FILE)
13
+ @redo_history = load_history(REDO_FILE)
14
+ end
15
+
16
+ # Adds a memento to the undo history and saves to file
17
+ # @param memento [Memento]
18
+ def add_memento(memento)
19
+ @undo_history.push(memento)
20
+ save_history(@undo_history, UNDO_FILE)
21
+ clear_redo_history # Clear redo history when a new action is added
22
+ end
23
+
24
+ # Returns the last memento from the undo history
25
+ # @return [Memento, nil]
26
+ def undo
27
+ memento = @undo_history.pop
28
+ save_history(@undo_history, UNDO_FILE)
29
+ @redo_history.push(memento) if memento
30
+ save_history(@redo_history, REDO_FILE)
31
+ memento
32
+ end
33
+
34
+ # Returns the last memento from the redo history
35
+ # @return [Memento, nil]
36
+ def redo
37
+ memento = @redo_history.pop
38
+ save_history(@redo_history, REDO_FILE)
39
+ @undo_history.push(memento) if memento
40
+ save_history(@undo_history, UNDO_FILE)
41
+ memento
42
+ end
43
+
44
+ # Clears the redo history
45
+ def clear_redo_history
46
+ @redo_history.clear
47
+ save_history(@redo_history, REDO_FILE)
48
+ end
49
+
50
+ # Returns the history count for undo
51
+ # @return [Integer]
52
+ def undo_history_count
53
+ @undo_history.size
54
+ end
55
+
56
+ # Returns the history count for redo
57
+ # @return [Integer]
58
+ def redo_history_count
59
+ @redo_history.size
60
+ end
61
+
62
+ private
63
+
64
+ # Loads history from a file
65
+ # @param file [String] - The file to load from
66
+ # @return [Array<Memento>]
67
+ def load_history(file)
68
+ return [] unless File.exist?(file)
69
+
70
+ YAML.load_file(file).map { |yaml| Memento.from_yaml(yaml) }
71
+ rescue StandardError
72
+ [] # Return empty array if any error occurs while loading
73
+ end
74
+
75
+ # Saves the history to a file
76
+ # @param history [Array<Memento>]
77
+ # @param file [String]
78
+ def save_history(history, file)
79
+ File.open(file, "w") { |f| f.write(history.map(&:to_yaml).to_yaml) }
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+ require "thor"
5
+
6
+ DIR = File.dirname(__FILE__)
7
+ Dir[File.join(DIR, "*.rb")].sort.each { |file| require file }
8
+ Dir[File.join(DIR, "services", "*.rb")].sort.each { |file| require file }
9
+
10
+ module KhoinguyenEhTodoList
11
+ # Command line interface for the todo list
12
+ class CLI < Thor
13
+ TASKS_FILE = "tasks.yml"
14
+
15
+ def initialize(args = [], options = {}, config = {})
16
+ super(args, options, config)
17
+ @tasks = load_tasks
18
+ @caretaker = Caretaker.new
19
+ end
20
+
21
+ desc "add TITLE", "Add a new task"
22
+
23
+ def add(title)
24
+ execute_action(KhoinguyenEhTodoList::Services::AddTaskService, title)
25
+ end
26
+
27
+ desc "remove INDEX", "Remove a task by index"
28
+
29
+ def remove(index)
30
+ execute_action(KhoinguyenEhTodoList::Services::RemoveTaskService, index)
31
+ end
32
+
33
+ desc "clear", "Clear all tasks"
34
+
35
+ def clear
36
+ execute_action(KhoinguyenEhTodoList::Services::ClearTasksService)
37
+ end
38
+
39
+ desc "move FROM TO", "Move a task from one index to another"
40
+
41
+ def move(from, to)
42
+ execute_action(KhoinguyenEhTodoList::Services::MoveTaskService, from, to)
43
+ end
44
+
45
+ desc "toggle INDEX", "Toggle task completion status"
46
+
47
+ def toggle(index)
48
+ execute_action(KhoinguyenEhTodoList::Services::ToggleCompletionService, index)
49
+ end
50
+
51
+ desc "undo", "Undo the last action"
52
+
53
+ def undo
54
+ execute_action(KhoinguyenEhTodoList::Services::UndoRedoService, @caretaker, :undo)
55
+ end
56
+
57
+ desc "redo", "Redo the last undone action"
58
+
59
+ def redo
60
+ execute_action(KhoinguyenEhTodoList::Services::UndoRedoService, @caretaker, :redo)
61
+ end
62
+
63
+ desc "list", "List all tasks"
64
+
65
+ def list
66
+ @tasks.each_with_index { |task, index| puts "#{index + 1}. #{task}" }
67
+ end
68
+
69
+ desc "history", "Show the number of undoable and redoable actions"
70
+
71
+ def history
72
+ puts "Undoable actions: #{@caretaker.undo_history_count}"
73
+ puts "Redoable actions: #{@caretaker.redo_history_count}"
74
+ end
75
+
76
+ private
77
+
78
+ # Executes an action and saves the memento
79
+ # @param service [Class] - The service class to execute
80
+ # @param args [Array] - Arguments for the service call
81
+ def execute_action(service, *args)
82
+ result = service.call(@tasks, *args)
83
+
84
+ if result && result[:success]
85
+ @caretaker.add_memento(result[:memento]) if result[:memento]
86
+ puts result[:message] if result[:message]
87
+ save_tasks
88
+ elsif result && result[:error]
89
+ puts "[ERROR] #{result[:error]}"
90
+ end
91
+ end
92
+
93
+ def load_tasks
94
+ return [] unless File.exist?(TASKS_FILE)
95
+
96
+ YAML.load_file(TASKS_FILE) || []
97
+ end
98
+
99
+ def save_tasks
100
+ File.open(TASKS_FILE, "w") { |file| file.write(@tasks.to_yaml) }
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ module KhoinguyenEhTodoList
6
+ # Represents a memento for the undo and redo history
7
+ class Memento
8
+ attr_reader :action, :data
9
+
10
+ # @param action [Symbol] The type of action (e.g., :add, :remove)
11
+ # @param data [Hash] The details of the change
12
+ def initialize(action, data)
13
+ @action = action
14
+ @data = data
15
+ end
16
+
17
+ # Serializes the memento to YAML
18
+ # @return [String]
19
+ def to_yaml
20
+ YAML.dump({ action: @action, data: @data })
21
+ end
22
+
23
+ # Deserializes memento from YAML
24
+ # @param yaml [String]
25
+ # @return [Memento]
26
+ def self.from_yaml(yaml)
27
+ state = YAML.safe_load(yaml)
28
+ new(state[:action], state[:data])
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KhoinguyenEhTodoList
4
+ module Services
5
+ # Service object to add a task to the list
6
+ class AddTaskService
7
+ def self.call(tasks, title)
8
+ task = Task.new(title)
9
+ tasks << task
10
+ {
11
+ success: true,
12
+ message: "Task added successfully.",
13
+ memento: Memento.new(:add, { index: tasks.size - 1, task: task })
14
+ }
15
+ rescue StandardError => e
16
+ { success: false, error: e.message }
17
+ end
18
+
19
+ # Reverts the addition of the task
20
+ def self.revert(tasks, memento)
21
+ tasks.delete_at(memento.data[:index])
22
+ end
23
+
24
+ # Re-applies the addition of the task
25
+ def self.apply(tasks, memento)
26
+ tasks.insert(memento.data[:index], memento.data[:task])
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KhoinguyenEhTodoList
4
+ module Services
5
+ # Service object to clear all tasks from the list
6
+ class ClearTasksService
7
+ def self.call(tasks)
8
+ previous_tasks = tasks.dup
9
+ tasks.clear
10
+ {
11
+ success: true,
12
+ message: "Tasks cleared successfully.",
13
+ memento: Memento.new(:clear, { tasks: previous_tasks })
14
+ }
15
+ rescue StandardError => e
16
+ { success: false, error: e.message }
17
+ end
18
+
19
+ # Reverts the clear action
20
+ def self.revert(tasks, memento)
21
+ tasks.concat(memento.data[:tasks])
22
+ end
23
+
24
+ # Re-applies the clear action
25
+ def self.apply(tasks, _memento)
26
+ tasks.clear
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KhoinguyenEhTodoList
4
+ module Services
5
+ # Service object to move a task in the list
6
+ class MoveTaskService
7
+ def self.call(tasks, from, to)
8
+ from_idx = from.to_i - 1
9
+ to_idx = to.to_i - 1
10
+
11
+ if from_idx.negative? || to_idx.negative? || from_idx >= tasks.size || to_idx >= tasks.size
12
+ raise "Invalid indices"
13
+ end
14
+
15
+ task = tasks.delete_at(from_idx)
16
+ tasks.insert(to_idx, task)
17
+
18
+ { success: true, memento: Memento.new(:move, { from: from_idx, to: to_idx }) }
19
+ rescue StandardError => e
20
+ { success: false, error: e.message }
21
+ end
22
+
23
+ # Reverts the move action
24
+ # @param tasks [Array<Task>] the list of tasks
25
+ # @param memento [Memento] the memento to revert
26
+ # @return [void]
27
+ def self.revert(tasks, memento)
28
+ task = tasks.delete_at(memento.data[:to])
29
+ tasks.insert(memento.data[:from], task)
30
+ end
31
+
32
+ # Re-applies the move action
33
+ # @param tasks [Array<Task>] the list of tasks
34
+ # @param memento [Memento] the memento to apply
35
+ # @return [void]
36
+ def self.apply(tasks, memento)
37
+ task = tasks.delete_at(memento.data[:from])
38
+ tasks.insert(memento.data[:to], task)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KhoinguyenEhTodoList
4
+ module Services
5
+ # Service object to remove a task from the list
6
+ class RemoveTaskService
7
+ def self.call(tasks, index)
8
+ index = index.to_i - 1
9
+ task = tasks.delete_at(index)
10
+ raise "Task not found" unless task
11
+
12
+ {
13
+ success: true,
14
+ message: "Task removed successfully.",
15
+ memento: Memento.new(:remove, { index: index, task: task })
16
+ }
17
+ rescue StandardError => e
18
+ { success: false, error: e.message }
19
+ end
20
+
21
+ # Reverts the removal of the task
22
+ def self.revert(tasks, memento)
23
+ tasks.insert(memento.data[:index], memento.data[:task])
24
+ end
25
+
26
+ # Re-applies the removal of the task
27
+ def self.apply(tasks, memento)
28
+ tasks.delete_at(memento.data[:index])
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KhoinguyenEhTodoList
4
+ module Services
5
+ # Service object to toggle the completion status of a task
6
+ class ToggleCompletionService
7
+ def self.call(tasks, index) # rubocop:disable Metrics/MethodLength
8
+ index = index.to_i - 1
9
+ task = tasks[index]
10
+ raise "Task not found" unless task
11
+
12
+ # Toggle the task completion state
13
+ task.toggle_completion
14
+
15
+ message = task.completed? ? "Task completed." : "Task marked as incomplete."
16
+
17
+ {
18
+ success: true,
19
+ message: message,
20
+ memento: Memento.new(:toggle, { index: index })
21
+ }
22
+ rescue StandardError => e
23
+ { success: false, error: e.message }
24
+ end
25
+
26
+ # Reverts the toggle action
27
+ def self.revert(tasks, memento)
28
+ task = tasks[memento.data[:index]]
29
+ task.toggle_completion
30
+ end
31
+
32
+ # Re-applies the toggle action
33
+ def self.apply(tasks, memento)
34
+ task = tasks[memento.data[:index]]
35
+ task.toggle_completion
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KhoinguyenEhTodoList
4
+ module Services
5
+ # Service object to handle undo and redo operations
6
+ class UndoRedoService
7
+ # Handles undo or redo operation based on the action passed
8
+ # @param tasks [Array<Task>]
9
+ # @param caretaker [Caretaker]
10
+ # @param action [Symbol] - :undo or :redo
11
+ # @return [Hash]
12
+ def self.call(tasks, caretaker, action) # rubocop:disable Metrics/MethodLength
13
+ memento = case action
14
+ when :undo
15
+ caretaker.undo
16
+ when :redo
17
+ caretaker.redo
18
+ else
19
+ return { success: false, message: "Invalid action. Use :undo or :redo." }
20
+ end
21
+
22
+ return { success: true, message: "Nothing to #{action}." } unless memento && memento.action != :blank
23
+
24
+ service = determine_service(memento.action)
25
+ if service
26
+ action == :undo ? service.revert(tasks, memento) : service.apply(tasks, memento)
27
+ { success: true, message: "#{action.capitalize} successful." }
28
+ else
29
+ { success: false, message: "Action not supported." }
30
+ end
31
+ end
32
+
33
+ # Determines the appropriate service for the given action
34
+ # @param action [Symbol]
35
+ # @return [Class, nil]
36
+ def self.determine_service(action) # rubocop:disable Metrics/MethodLength
37
+ case action
38
+ when :add
39
+ KhoinguyenEhTodoList::Services::AddTaskService
40
+ when :remove
41
+ KhoinguyenEhTodoList::Services::RemoveTaskService
42
+ when :move
43
+ KhoinguyenEhTodoList::Services::MoveTaskService
44
+ when :toggle
45
+ KhoinguyenEhTodoList::Services::ToggleCompletionService
46
+ when :clear
47
+ KhoinguyenEhTodoList::Services::ClearTasksService
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KhoinguyenEhTodoList
4
+ # Represents a task in the todo list
5
+ class Task
6
+ attr_accessor :title, :completed
7
+
8
+ def initialize(title)
9
+ @title = title
10
+ @completed = false
11
+ end
12
+
13
+ # Toggles the completion status of the task
14
+ def toggle_completion
15
+ @completed = !@completed
16
+ end
17
+
18
+ # Returns the completion status
19
+ # @return [Boolean]
20
+ def completed?
21
+ @completed
22
+ end
23
+
24
+ def to_s
25
+ status = @completed ? "[X]" : "[ ]"
26
+ "#{status} #{@title}"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KhoinguyenEhKhoinguyenEhTodoList
4
+ VERSION = "0.1.1"
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "khoinguyen_eh_todo_list/version"
4
+ require_relative "khoinguyen_eh_todo_list/task"
5
+ require_relative "khoinguyen_eh_todo_list/cli"
6
+
7
+ module KhoinguyenEhTodoList
8
+ class Error < StandardError; end
9
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: khoinguyen_eh_todo_list
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Nguyen Chinh Khoi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yaml
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: This gem provides a simple way to manage a todo list.
42
+ email:
43
+ - khoi.nguyen2@employmenthero.com
44
+ executables:
45
+ - khoinguyen_eh_todo_list
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".idea/vcs.xml"
50
+ - ".idea/workspace.xml"
51
+ - ".rspec"
52
+ - ".rubocop.yml"
53
+ - CODE_OF_CONDUCT.md
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - bin/khoinguyen_eh_todo_list
58
+ - khoinguyen_eh_todo_list.gemspec
59
+ - lib/khoinguyen_eh_todo_list.rb
60
+ - lib/khoinguyen_eh_todo_list/caretaker.rb
61
+ - lib/khoinguyen_eh_todo_list/cli.rb
62
+ - lib/khoinguyen_eh_todo_list/memento.rb
63
+ - lib/khoinguyen_eh_todo_list/services/add_task_service.rb
64
+ - lib/khoinguyen_eh_todo_list/services/clear_tasks_service.rb
65
+ - lib/khoinguyen_eh_todo_list/services/move_task_service.rb
66
+ - lib/khoinguyen_eh_todo_list/services/remove_task_service.rb
67
+ - lib/khoinguyen_eh_todo_list/services/toggle_completion_service.rb
68
+ - lib/khoinguyen_eh_todo_list/services/undo_redo_service.rb
69
+ - lib/khoinguyen_eh_todo_list/task.rb
70
+ - lib/khoinguyen_eh_todo_list/version.rb
71
+ homepage: https://github.com/khoinguyen-eh/khoinguyen_eh_todo_list
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ allowed_push_host: https://rubygems.org/
76
+ homepage_uri: https://github.com/khoinguyen-eh/khoinguyen_eh_todo_list
77
+ source_code_uri: https://github.com/khoinguyen-eh/khoinguyen_eh_todo_list
78
+ changelog_uri: https://github.com/khoinguyen-eh/khoinguyen_eh_todo_list
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 2.6.0
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 3.1.6
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Ruby gem for managing a todo list
98
+ test_files: []