yuuki 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3f43903b0871e192e128847cf0e772066dec93cfa0b882afb9ac53bb0dfbf3ca
4
+ data.tar.gz: 1114888a7eb4a790792556ab6aa03b9ace4da9a15bd56201f8fdf98cf855df30
5
+ SHA512:
6
+ metadata.gz: 742ddbe4914aa114d780a8a5bdd6285ba04c6ca651fa23d62c995741b155a935c58742240eb51114f43c6ab82347ea1d457a92f40b63ae87324fbf81ae8073a7
7
+ data.tar.gz: 6ae20a7613818d726fd3442a7057354355106656a42c45c9f8c8dcf1dd1eb8172905307f35b9559e7e15c4e3c0b4f60cfad74017ccd254edff93dabf8fab485f
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /test/
10
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in yuuki.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Ishotihadus
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.
@@ -0,0 +1,31 @@
1
+ # Yuuki
2
+
3
+ A caller / runner framework for Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'yuuki'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install yuuki
20
+
21
+ ## Usage
22
+
23
+ あとで書く。
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/ishotihadus/yuuki](https://github.com/ishotihadus/yuuki).
28
+
29
+ ## License
30
+
31
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "yuuki"
5
+
6
+ require "pry"
7
+ Pry.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ require 'yuuki/runner'
2
+ require 'yuuki/caller'
3
+ require 'yuuki/error'
4
+ require 'yuuki/version'
@@ -0,0 +1,135 @@
1
+ require 'yuuki/runner'
2
+
3
+ module Yuuki
4
+ class Caller
5
+ Runner = Struct.new(:method, :tags, :exclude, :threading, :priority)
6
+
7
+ def self.require_dir(dir, recursive = false)
8
+ Dir.glob(recursive ? "#{require_dir}/**/*.rb" : "#{require_dir}/*.rb"){|file| require file}
9
+ end
10
+
11
+ def initialize(*klass)
12
+ @runners = []
13
+ @threads = []
14
+ add(*klass)
15
+ end
16
+
17
+ def add(*klass)
18
+ klass.each do |klass|
19
+ instance = klass.new
20
+ instance.instance_variable_set(:@caller, self)
21
+ info = klass.instance_variable_get(:@runner_info) || {}
22
+ methods = klass.public_instance_methods(false)
23
+ methods |= klass.instance_variable_get(:@runner_adds) || []
24
+ methods -= klass.instance_variable_get(:@runner_excepts) || []
25
+ @runners += methods.map do |method|
26
+ tags = info.dig(method, :tags) || []
27
+ exclude = !!info.dig(method, :exclude)
28
+ threading = !!info.dig(method, :threading)
29
+ priority = info.dig(method, :priority) || 0
30
+ Runner.new(instance.method(method), tags, exclude, threading, priority)
31
+ end
32
+ end
33
+ @runners.sort_by!{|e| -e.priority}
34
+ end
35
+
36
+ def run(**args)
37
+ run_internal(@runners.reject(&:exclude), args)
38
+ end
39
+
40
+ def run_all(**args)
41
+ run_internal(@runners, args)
42
+ end
43
+
44
+ def run_class(*klass, **args)
45
+ run_internal(@runners.select{|e| !e.exclude && klass.any?{|k| e.method.receiver.instance_of?(k)}}, args)
46
+ end
47
+
48
+ def run_class_all(*klass, **args)
49
+ run_internal(@runners.select{|e| klass.any?{|k| e.method.receiver.instance_of?(k)}}, args)
50
+ end
51
+
52
+ def run_tag(*tags, **args)
53
+ run_internal(@runners.select{|e| tags.any?{|t| e.tags.any?(t)}}, args)
54
+ end
55
+
56
+ def run_select(**args)
57
+ run_internal(@runners.select{|e| yield(e)}, args)
58
+ end
59
+
60
+ def run_method(klass, method, **args)
61
+ runners = klass ? @runners.select{|e| e.method.receiver.class == klass} : @runners
62
+ runners.select!{|e| e.method.name == method} if method
63
+ run_internal(runners, args)
64
+ end
65
+
66
+ def join
67
+ @threads.each(&:join)
68
+ end
69
+
70
+ def alive?
71
+ @threads.any?(&:alive?)
72
+ end
73
+ alias running? alive?
74
+
75
+ private
76
+
77
+ def run_internal(runners, args)
78
+ runners.each do |runner|
79
+ if runner.threading
80
+ @threads << Thread.new(runner.method, args) do |method, args|
81
+ run_method_internal(method, args)
82
+ end
83
+ else
84
+ run_method_internal(runner.method, args)
85
+ end
86
+ end
87
+ @threads.select!(&:alive?)
88
+ end
89
+
90
+ def run_method_internal(method, args)
91
+ params = method.parameters
92
+ return method[] if params.size == 0
93
+ params_array = []
94
+ params_hash = {}
95
+ params_block = nil
96
+ nonspecified_last_opt = nil
97
+ params.each do |type, name|
98
+ case type
99
+ when :req
100
+ raise Yuuki::Error.new("A required argument '#{name}' was not found on running #{method.owner}::#{method.name}") unless args.key?(name)
101
+ params_array << args[name]
102
+ when :opt
103
+ next nonspecified_last_opt = name unless args.key?(name)
104
+ raise Yuuki::Error.new("A required argument '#{nonspecified_last_opt}' was not found"\
105
+ " on running #{method.owner}::#{method.name}"" with optional argument '#{name}'") if nonspecified_last_opt
106
+ params_array << args[name]
107
+ when :rest
108
+ next unless args.key?(name)
109
+ raise Yuuki::Error.new("A required argument '#{nonspecified_last_opt}' not found"\
110
+ " on running #{method.owner}::#{method.name}"" with rest argument '#{name}'") if nonspecified_last_opt
111
+ if args[name].respond_to?(:to_ary)
112
+ params_array += args[name]
113
+ else
114
+ params_array << args[name]
115
+ end
116
+ when :keyreq
117
+ raise Yuuki::Error.new("A required key argument '#{name}' was not found on running #{method.owner}::#{method.name}") unless args.key?(name)
118
+ params_hash[name] = args[name]
119
+ when :key
120
+ params_hash[name] = args[name] if args.key?(name)
121
+ when :keyrest
122
+ next unless args.key?(name)
123
+ if args[name].respond_to?(:to_hash)
124
+ params_hash.merge!(args[name])
125
+ else
126
+ params_hash[name] = args[name]
127
+ end
128
+ when :block
129
+ params_block = args[name]
130
+ end
131
+ end
132
+ params_hash.empty? ? method[*params_array, &params_block] : method[*params_array, **params_hash, &params_block]
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,4 @@
1
+ module Yuuki
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,48 @@
1
+ require 'yuuki/error'
2
+
3
+ module Yuuki
4
+ class RunnerBase
5
+ class << self
6
+ private
7
+
8
+ def add(*methods)
9
+ @runner_adds ||= []
10
+ @runner_adds |= methods
11
+ end
12
+
13
+ def delete(*methods)
14
+ @runner_excepts ||= []
15
+ @runner_excepts |= methods
16
+ end
17
+
18
+ def tag(method, *tags)
19
+ @runner_info ||= {}
20
+ @runner_info[method] ||= {}
21
+ @runner_info[method][:tags] ||= []
22
+ @runner_info[method][:tags] |= tags
23
+ end
24
+
25
+ def exclude(*methods)
26
+ @runner_info ||= {}
27
+ methods.each do |method|
28
+ @runner_info[method] ||= {}
29
+ @runner_info[method][:exclude] = true
30
+ end
31
+ end
32
+
33
+ def threading(*methods)
34
+ @runner_info ||= {}
35
+ methods.each do |method|
36
+ @runner_info[method] ||= {}
37
+ @runner_info[method][:threading] = true
38
+ end
39
+ end
40
+
41
+ def priority(method, priority)
42
+ @runner_info ||= {}
43
+ @runner_info[method] ||= {}
44
+ @runner_info[method][:priority] = priority
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Yuuki
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'yuuki/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'yuuki'
7
+ spec.version = Yuuki::VERSION
8
+ spec.authors = ['Ishotihadus']
9
+ spec.email = ['hanachan.pao@gmail.com']
10
+
11
+ spec.summary = 'A caller / runner framework for Ruby'
12
+ spec.description = 'A caller / runner framework for Ruby.'
13
+ spec.homepage = 'https://github.com/ishotihadus/yuuki'
14
+ spec.license = 'MIT'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'pry'
28
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yuuki
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ishotihadus
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-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: '0'
20
+ type: :development
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: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A caller / runner framework for Ruby.
56
+ email:
57
+ - hanachan.pao@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - lib/yuuki.rb
70
+ - lib/yuuki/caller.rb
71
+ - lib/yuuki/error.rb
72
+ - lib/yuuki/runner.rb
73
+ - lib/yuuki/version.rb
74
+ - yuuki.gemspec
75
+ homepage: https://github.com/ishotihadus/yuuki
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
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: '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.0.1
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: A caller / runner framework for Ruby
98
+ test_files: []