swiftcore-tasks 0.1.0

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: 9d0d01200027a06200339af48535cbe026187165932737e2d425f2ec3e257488
4
+ data.tar.gz: a6ff25224dc5667f9d4d1c3f9e80b206e20e1bd75da26c17e1764acf16ce703f
5
+ SHA512:
6
+ metadata.gz: e24f0a3a35d9a7ab75cc28405f33a159c4e3fb8d484a1ef22d7d9f96fc8e74a3768311aabcde4853021f29567014692bb27d77b7010b8d15939f018892b034f2
7
+ data.tar.gz: 4dc177905310462e6c5f21140461a573bfc5fed6506b0d696c7990eb64ca8c389954ff7a040dc335c13857fa539ecd39497bee64440417971e08bc1704e8bd83
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.4
7
+ before_install: gem install bundler -v 1.16.6
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in swiftcore-tasks.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ swiftcore-tasks (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.11.3)
10
+ rake (10.5.0)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 1.16)
17
+ minitest (~> 5.0)
18
+ rake (~> 10.0)
19
+ swiftcore-tasks!
20
+
21
+ BUNDLED WITH
22
+ 1.16.6
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Kirk Haines
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Kirk Haines
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,54 @@
1
+ # Swiftcore::Tasks
2
+
3
+ This little library encapsulates a very simple task queue implementation.
4
+ Tasks have a priority, and an object that responds to #call.
5
+
6
+ A TaskList is a set of Task objects that it orders according to priority.
7
+
8
+ The Tasklist allows running the whole queue of tasks, or running just a single
9
+ task at a time.
10
+
11
+ If any Task returns a TaskList, that TaskList gets merged into the parent's
12
+ list of tasks.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'swiftcore-tasks'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install swiftcore-tasks
29
+
30
+ ## Usage
31
+
32
+ ```ruby
33
+ result_list = []
34
+ list = Swiftcore::Tasks::TaskList.new
35
+ list << Swiftcore::Tasks::Task.new(4) { result_list << 3 }
36
+ list << Swiftcore::Tasks::Task.new(2) do
37
+ sublist = Swiftcore::Tasks::TaskList.new
38
+ sublist << Swiftcore::Tasks::Task.new(3) { result_list << 2 }
39
+ sublist << Swiftcore::Tasks::Task.new(5) { result_list << 5 }
40
+ sublist
41
+ end
42
+ list << Swiftcore::Tasks::Task.new(1) { result_list << 1 }
43
+
44
+ list.run
45
+ result_list == [1,2,3,5]
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/swiftcore-tasks.
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task defaults: :test
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'swiftcore/tasks/version'
4
+ require 'swiftcore/tasks/tasklist'
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Swiftcore
4
+ module Tasks
5
+ # This is a sortable, orderable container for a #call()able object.
6
+ class Task
7
+ include Comparable
8
+
9
+ attr_accessor :order, :status, :result
10
+ attr_reader :task, :taskord
11
+
12
+ def initialize(order = 0, &task)
13
+ @order = order
14
+ self.task = task
15
+ @status = :pending
16
+ @result = nil
17
+ end
18
+
19
+ def task=(val)
20
+ @taskord = val.to_s
21
+ @task = val
22
+ end
23
+
24
+ def less_than(other)
25
+ (@order < other.order) ||
26
+ (@order == other.order && @taskord < other.taskord)
27
+ end
28
+
29
+ def greater_than(other)
30
+ (@order > other.order) ||
31
+ (@order == other.order && @taskord > other.taskord)
32
+ end
33
+
34
+ def <=>(other)
35
+ if less_than(other)
36
+ -1
37
+ elsif greater_than(other)
38
+ 1
39
+ else
40
+ 0
41
+ end
42
+ end
43
+
44
+ def call(*args)
45
+ @status = :running
46
+ @result = @task&.call(*args)
47
+ @status = :finished
48
+ @result
49
+ rescue StandardError => e
50
+ @status = e
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+ require 'swiftcore/tasks/task'
5
+
6
+ module Swiftcore
7
+ module Tasks
8
+ # Just a SortedSet with a few special powers.
9
+ class TaskList < SortedSet
10
+ attr_accessor :status
11
+
12
+ def initialize(*args)
13
+ @status = :pending
14
+ super(*args)
15
+ end
16
+
17
+ def run
18
+ @status = :running
19
+ tick while any?
20
+ @status = :finished
21
+ end
22
+
23
+ def finished?
24
+ @status = :finished if empty?
25
+ @status == :finished
26
+ end
27
+
28
+ # Run a single task in the queue.
29
+ def tick
30
+ prior_status = @status
31
+ @status = :tick
32
+ task = first
33
+ result = task.call
34
+ delete task
35
+ merge(result) if result.is_a?(TaskList)
36
+ @status = prior_status
37
+ finished?
38
+ result
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Swiftcore
4
+ module Tasks
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'swiftcore/tasks/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'swiftcore-tasks'
9
+ spec.version = Swiftcore::Tasks::VERSION
10
+ spec.authors = ['Kirk Haines']
11
+ spec.email = ['kirk-haines@cookpad.com']
12
+
13
+ spec.summary = <<~ESUMMARY
14
+ Simple task list library.
15
+ ESUMMARY
16
+ spec.description = <<~EDESCRIPTION
17
+ This is a very simple task list library that I use in a bunch of other places.
18
+ EDESCRIPTION
19
+ spec.homepage = 'https://github.com/wyhaines/swiftcore-tasks'
20
+ spec.license = 'MIT'
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added
24
+ # into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ end
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_development_dependency 'bundler', '~> 1.16'
33
+ spec.add_development_dependency 'minitest', '~> 5.0'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swiftcore-tasks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kirk Haines
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: 'This is a very simple task list library that I use in a bunch of other
56
+ places.
57
+
58
+ '
59
+ email:
60
+ - kirk-haines@cookpad.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".rubocop.yml"
67
+ - ".travis.yml"
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/swiftcore/tasks.rb
75
+ - lib/swiftcore/tasks/task.rb
76
+ - lib/swiftcore/tasks/tasklist.rb
77
+ - lib/swiftcore/tasks/version.rb
78
+ - swiftcore-tasks.gemspec
79
+ homepage: https://github.com/wyhaines/swiftcore-tasks
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.7.6
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Simple task list library.
103
+ test_files: []