codekindly-utils 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +6 -5
- data/.rspec +0 -1
- data/.rubocop.yml +4 -0
- data/Appraisals +18 -0
- data/CONTRIBUTING.md +35 -0
- data/Gemfile +4 -2
- data/README.md +1 -19
- data/Rakefile +5 -3
- data/all_rubies +62 -0
- data/bin/console +4 -3
- data/codekindly-utils.gemspec +24 -15
- data/gemfiles/activerecord_4_2.gemfile +7 -0
- data/gemfiles/activerecord_5_0.gemfile +7 -0
- data/gemfiles/activerecord_5_1.gemfile +7 -0
- data/gemfiles/activerecord_5_2.gemfile +7 -0
- data/lib/code_kindly/utils.rb +18 -9
- data/lib/code_kindly/utils/active_record.rb +65 -42
- data/lib/code_kindly/utils/boolean.rb +27 -11
- data/lib/code_kindly/utils/command.rb +35 -0
- data/lib/code_kindly/utils/deprecation.rb +24 -0
- data/lib/code_kindly/utils/dir.rb +9 -5
- data/lib/code_kindly/utils/file.rb +33 -35
- data/lib/code_kindly/utils/os.rb +31 -0
- data/lib/code_kindly/utils/presence.rb +24 -0
- data/lib/code_kindly/utils/rake_task.rb +3 -1
- data/lib/code_kindly/utils/shell.rb +9 -4
- data/lib/code_kindly/utils/sql.rb +52 -0
- data/lib/code_kindly/utils/version.rb +3 -1
- data/lib/codekindly-utils.rb +5 -2
- metadata +117 -8
- data/lib/code_kindly/utils/o_s.rb +0 -16
- data/lib/code_kindly/utils/s_q_l.rb +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f325579c294333c23f0f4aa054072e3a9d5b7cac
|
4
|
+
data.tar.gz: 88087f5c9db6f4a3e43f6e1b042c5b1b44e1014d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fad5068a35c9277f4473027d0fac0ce9467632422197b40ff060932509eb572161049f776b2a3fd5f7418365546b45599ca7cb4feb62b74c09c341870c2f462b
|
7
|
+
data.tar.gz: d51b4dd853e25e92896a40539ce19e9914f6a08bc0c522be1a102923e6cc1494002f13abb9503bac095851c012734c6c8881a2f86a6d88cbe36900e4fbc67e1b
|
data/.gitignore
CHANGED
@@ -1,13 +1,14 @@
|
|
1
|
-
|
1
|
+
.bundle/
|
2
2
|
.DS_Store
|
3
|
-
|
3
|
+
.rspec_status
|
4
|
+
.ruby-version
|
4
5
|
/_yardoc/
|
6
|
+
/.yardoc
|
5
7
|
/coverage/
|
6
8
|
/doc/
|
9
|
+
/Gemfile.lock
|
10
|
+
/gemfiles/*.lock
|
7
11
|
/pkg/
|
8
12
|
/spec/reports/
|
9
13
|
/tmp/
|
10
14
|
/vendor/ruby
|
11
|
-
|
12
|
-
# rspec failure tracking
|
13
|
-
.rspec_status
|
data/.rspec
CHANGED
data/.rubocop.yml
ADDED
data/Appraisals
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/local/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
appraise 'activerecord-4-2' do
|
5
|
+
gem 'activerecord', '~> 4.2.10'
|
6
|
+
end
|
7
|
+
|
8
|
+
appraise 'activerecord-5-0' do
|
9
|
+
gem 'activerecord', '~> 5.0.7'
|
10
|
+
end
|
11
|
+
|
12
|
+
appraise 'activerecord-5-1' do
|
13
|
+
gem 'activerecord', '~> 5.1.6'
|
14
|
+
end
|
15
|
+
|
16
|
+
appraise 'activerecord-5-2' do
|
17
|
+
gem 'activerecord', '~> 5.2.0'
|
18
|
+
end
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Contributing to CodeKindly::Utils
|
2
|
+
|
3
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jlw/codekindly-utils.
|
4
|
+
|
5
|
+
## Setup
|
6
|
+
|
7
|
+
`bundle install --path vendor`
|
8
|
+
|
9
|
+
## Run tests
|
10
|
+
|
11
|
+
To run tests against all supported Ruby versions and all supported major dependency versions:
|
12
|
+
|
13
|
+
```Shell
|
14
|
+
./all_rubies appraisal-install
|
15
|
+
./all_rubies appraisal-spec
|
16
|
+
```
|
17
|
+
|
18
|
+
## Submitting changes
|
19
|
+
|
20
|
+
1. Fork the repository
|
21
|
+
2. Make your code changes
|
22
|
+
3. Add complete test scenarios for your changes
|
23
|
+
4. Ensure your code follows the Ruby Style Guide: `bundle exec rubocop`
|
24
|
+
5. Submit a pull request on GitHub
|
25
|
+
|
26
|
+
## Publishing
|
27
|
+
|
28
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
29
|
+
|
30
|
+
To release a new version of the gem to RubyGems.org:
|
31
|
+
|
32
|
+
```Shell
|
33
|
+
bundle exec rake install
|
34
|
+
gem push vendor/ruby/2.5.0/cache/codekindly-utils-x.y.z.gem
|
35
|
+
```
|
data/Gemfile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
6
|
|
5
7
|
# Specify your gem's dependencies in codekindly-utils.gemspec
|
6
8
|
gemspec
|
data/README.md
CHANGED
@@ -22,33 +22,15 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
$ gem install codekindly-utils
|
24
24
|
|
25
|
-
## Publishing
|
26
|
-
|
27
|
-
```Shell
|
28
|
-
be rake install
|
29
|
-
gem push vendor/ruby/2.2.0/cache/codekindly-utils-0.0.x.gem
|
30
|
-
```
|
31
|
-
|
32
25
|
## Usage
|
33
26
|
|
34
27
|
I occasionally use these utilities in the main logic of an app, but usually
|
35
28
|
use them as conveniences in various rake tasks that are automated or part of my
|
36
29
|
general workflow.
|
37
30
|
|
38
|
-
## Development
|
39
|
-
|
40
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
41
|
-
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
42
|
-
prompt that will allow you to experiment.
|
43
|
-
|
44
|
-
To install this gem onto your local machine, run `bundle exec rake install`.
|
45
|
-
To release a new version, update the version number in `version.rb`, and then
|
46
|
-
run `bundle exec rake release`, which will create a git tag for the version,
|
47
|
-
push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
48
|
-
|
49
31
|
## Contributing
|
50
32
|
|
51
|
-
Bug reports and pull requests are welcome
|
33
|
+
Bug reports and pull requests are welcome, see [CONTRIBUTING.md](CONTRIBUTING.md).
|
52
34
|
|
53
35
|
## License
|
54
36
|
|
data/Rakefile
CHANGED
data/all_rubies
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
versions=('2.2.10' '2.3.7' '2.4.4' '2.5.1')
|
4
|
+
|
5
|
+
switcher=`which rbenv`
|
6
|
+
if [[ $switcher = *[!\ ]* ]]; then
|
7
|
+
verb="local"
|
8
|
+
else
|
9
|
+
switcher=`which rvm`
|
10
|
+
if [[ $switcher = *[!\ ]* ]]; then
|
11
|
+
verb="use"
|
12
|
+
else
|
13
|
+
echo "Please install rbenv or rvm"
|
14
|
+
exit 1
|
15
|
+
fi
|
16
|
+
fi
|
17
|
+
|
18
|
+
case "$1" in
|
19
|
+
appraisal-install)
|
20
|
+
for version in ${versions[@]}
|
21
|
+
do
|
22
|
+
echo "Appraisal install for Ruby $version"
|
23
|
+
eval "$switcher $verb $version"
|
24
|
+
bundle exec appraisal install
|
25
|
+
done
|
26
|
+
;;
|
27
|
+
|
28
|
+
appraisal-spec)
|
29
|
+
for version in ${versions[@]}
|
30
|
+
do
|
31
|
+
echo "Run appraisal specs with Ruby $version"
|
32
|
+
eval "$switcher $verb $version"
|
33
|
+
bundle exec appraisal rspec
|
34
|
+
done
|
35
|
+
;;
|
36
|
+
|
37
|
+
bundle)
|
38
|
+
for version in ${versions[@]}
|
39
|
+
do
|
40
|
+
echo "Bundle for Ruby $version"
|
41
|
+
eval "$switcher $verb $version"
|
42
|
+
gem list --local bundler | grep bundler || gem install --no-ri --no-rdoc bundler
|
43
|
+
bundle install --path vendor
|
44
|
+
bundle update
|
45
|
+
done
|
46
|
+
;;
|
47
|
+
|
48
|
+
spec)
|
49
|
+
for version in ${versions[@]}
|
50
|
+
do
|
51
|
+
echo "Run specs with Ruby $version"
|
52
|
+
eval "$switcher $verb $version"
|
53
|
+
bundle exec rspec spec
|
54
|
+
done
|
55
|
+
;;
|
56
|
+
|
57
|
+
*)
|
58
|
+
echo $"Usage: $0 {bundle|spec|appraisal-install|appraisal-spec}"
|
59
|
+
exit 1
|
60
|
+
esac
|
61
|
+
|
62
|
+
exit 0
|
data/bin/console
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
|
-
require
|
4
|
-
require
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'codekindly-utils'
|
5
6
|
|
6
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
8
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +11,5 @@ require "codekindly-utils"
|
|
10
11
|
# require "pry"
|
11
12
|
# Pry.start
|
12
13
|
|
13
|
-
require
|
14
|
+
require 'irb'
|
14
15
|
IRB.start(__FILE__)
|
data/codekindly-utils.gemspec
CHANGED
@@ -1,29 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
|
-
lib = File.expand_path(
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
5
|
+
require 'code_kindly/utils/version'
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
8
|
+
spec.name = 'codekindly-utils'
|
8
9
|
spec.version = CodeKindly::Utils::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
10
|
+
spec.authors = ['Jeremy Weathers']
|
11
|
+
spec.email = ['jeremy@codekindly.com']
|
11
12
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
13
|
+
spec.summary = 'These are small utilities that I like to have around in my projects.'
|
14
|
+
spec.description = "There is nothing terribly unique or interesting here and there are almost certainly better ways to do things than this. These are little snippets I've found useful in the past and continue to use through inertia."
|
15
|
+
spec.homepage = 'https://github.com/jlw/codekindly-utils'
|
16
|
+
spec.license = 'MIT'
|
16
17
|
|
17
18
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
19
|
f.match(%r{^(test|spec|features)/})
|
19
20
|
end
|
20
|
-
spec.bindir =
|
21
|
+
spec.bindir = 'exe'
|
21
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
-
spec.require_paths = [
|
23
|
+
spec.require_paths = ['lib']
|
23
24
|
|
24
|
-
spec.required_ruby_version =
|
25
|
+
spec.required_ruby_version = '>= 2.2'
|
25
26
|
|
26
|
-
spec.
|
27
|
-
spec.
|
28
|
-
|
27
|
+
spec.add_dependency 'highline'
|
28
|
+
spec.add_dependency 'map'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'activerecord'
|
31
|
+
spec.add_development_dependency 'appraisal'
|
32
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
33
|
+
spec.add_development_dependency 'pry-byebug'
|
34
|
+
spec.add_development_dependency 'rake'
|
35
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
36
|
+
spec.add_development_dependency 'rubocop'
|
37
|
+
spec.add_development_dependency 'sqlite3'
|
29
38
|
end
|
data/lib/code_kindly/utils.rb
CHANGED
@@ -1,11 +1,20 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require_relative "utils/dir"
|
5
|
-
require_relative "utils/file"
|
6
|
-
require_relative "utils/o_s"
|
7
|
-
require_relative "utils/rake_task"
|
8
|
-
require_relative "utils/shell"
|
3
|
+
require_relative 'utils/version'
|
9
4
|
|
10
|
-
require_relative
|
11
|
-
|
5
|
+
require_relative 'utils/deprecation'
|
6
|
+
|
7
|
+
require_relative 'utils/boolean'
|
8
|
+
require_relative 'utils/presence'
|
9
|
+
|
10
|
+
require_relative 'utils/command'
|
11
|
+
require_relative 'utils/dir'
|
12
|
+
require_relative 'utils/file'
|
13
|
+
require_relative 'utils/os'
|
14
|
+
require_relative 'utils/rake_task'
|
15
|
+
require_relative 'utils/shell'
|
16
|
+
|
17
|
+
if Kernel.const_defined? :ActiveRecord
|
18
|
+
require_relative 'utils/active_record'
|
19
|
+
require_relative 'utils/sql'
|
20
|
+
end
|
@@ -1,73 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
|
1
5
|
module CodeKindly
|
2
6
|
module Utils
|
3
7
|
class ActiveRecord
|
8
|
+
include Deprecation
|
9
|
+
|
10
|
+
Rails = Kernel.const_defined?(:Rails) ? ::Rails : nil
|
11
|
+
|
4
12
|
class << self
|
5
13
|
def active_record_classes_by_connection
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
config_name ||= ::Rails.env
|
11
|
-
sets[config_name] ||= []
|
12
|
-
sets[config_name] << klass
|
13
|
-
end
|
14
|
-
sets
|
15
|
-
end
|
14
|
+
deprecate :active_record_classes_by_connection,
|
15
|
+
:classes_by_connection,
|
16
|
+
:'0.1.0'
|
17
|
+
classes_by_connection
|
16
18
|
end
|
17
19
|
|
18
|
-
def
|
19
|
-
|
20
|
-
puts "Nothing to clear"
|
21
|
-
else
|
22
|
-
puts "Clearing #{scope.count} #{scope.name} records"
|
23
|
-
scope.delete_all
|
24
|
-
end
|
20
|
+
def classes_by_connection
|
21
|
+
@classes_by_connection ||= find_classes_by_connection
|
25
22
|
end
|
26
23
|
|
27
|
-
def
|
28
|
-
|
29
|
-
|
24
|
+
def clear_scope(scope)
|
25
|
+
if scope.count.zero?
|
26
|
+
puts 'Nothing to clear'
|
27
|
+
return
|
28
|
+
end
|
29
|
+
puts "Clearing #{scope.count} #{scope.name} records"
|
30
|
+
scope.delete_all
|
30
31
|
end
|
31
32
|
|
32
33
|
def config(name = nil)
|
33
|
-
|
34
|
-
name ||= ::Rails.env
|
35
|
-
configurations[name]
|
34
|
+
configs[name || default_name]
|
36
35
|
end
|
37
36
|
|
38
|
-
def
|
39
|
-
|
40
|
-
connection ||= ::Rails.env
|
41
|
-
@default_connection_class ||= {}.with_indifferent_access
|
42
|
-
@default_connection_class[connection] ||= active_record_classes_by_connection.fetch(connection, []).first
|
43
|
-
@default_connection_class[connection] ||= active_record_classes_by_connection.fetch(active_record_classes_by_connection.keys).first
|
37
|
+
def configs
|
38
|
+
@configs ||= ::ActiveRecord::Base.configurations
|
44
39
|
end
|
45
40
|
|
46
|
-
|
41
|
+
def configurations
|
42
|
+
deprecate :configurations, :configs, :'0.1.0'
|
43
|
+
configs
|
44
|
+
end
|
47
45
|
|
48
|
-
def
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
46
|
+
def default_connection_class(connection = nil)
|
47
|
+
connection ||= default_name
|
48
|
+
@default_connection_class ||= {}.with_indifferent_access
|
49
|
+
@default_connection_class[connection] ||= begin
|
50
|
+
it = classes_by_connection.fetch(connection, []).first
|
51
|
+
key = classes_by_connection.keys.first
|
52
|
+
it ||= classes_by_connection.fetch(key).first
|
53
|
+
it
|
55
54
|
end
|
56
55
|
end
|
57
56
|
|
58
|
-
|
57
|
+
private
|
58
|
+
|
59
|
+
def application_active_record_class?(klass)
|
59
60
|
return false unless klass < ::ActiveRecord::Base
|
60
61
|
return false if klass.abstract_class
|
61
62
|
return false if klass.name =~ /ActiveRecord::/
|
62
63
|
true
|
63
64
|
end
|
64
65
|
|
65
|
-
def
|
66
|
-
|
67
|
-
|
66
|
+
def default_name
|
67
|
+
@default_name ||= Rails.try(:env) || configs.keys.first || 'default'
|
68
|
+
end
|
69
|
+
|
70
|
+
def find_classes
|
71
|
+
if Rails.try(:env).try(:development?)
|
72
|
+
model_files = Rails.root.join('app', 'models').to_s + '/**/*.rb'
|
73
|
+
::Dir.glob(model_files) { |f| require f }
|
74
|
+
end
|
75
|
+
ObjectSpace.each_object(Class).select do |klass|
|
76
|
+
application_active_record_class?(klass)
|
77
|
+
end.sort_by(&:name)
|
78
|
+
end
|
79
|
+
|
80
|
+
# rubocop:disable Metrics/AbcSize
|
81
|
+
def find_classes_by_connection
|
82
|
+
sets = {}.with_indifferent_access
|
83
|
+
find_classes.each do |klass|
|
84
|
+
config_name = configs.keys.select do |k|
|
85
|
+
configs[k]['database'] == klass.connection.current_database
|
86
|
+
end.first
|
87
|
+
config_name ||= default_name
|
88
|
+
sets[config_name] ||= []
|
89
|
+
sets[config_name] << klass
|
68
90
|
end
|
69
|
-
|
91
|
+
sets
|
70
92
|
end
|
93
|
+
# rubocop:enable Metrics/AbcSize
|
71
94
|
end
|
72
95
|
end
|
73
96
|
end
|
@@ -1,32 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module CodeKindly
|
2
4
|
module Utils
|
3
5
|
class Boolean
|
6
|
+
include Deprecation
|
7
|
+
|
4
8
|
# modified from ActiveRecord::ConnectionAdapters::Column (4.2.9)
|
5
|
-
|
6
|
-
|
9
|
+
TRUES = [true, 1, '1', 't', 'true', 'on', 'y', 'yes']
|
10
|
+
.map(&:freeze).freeze
|
11
|
+
FALSES = [false, 0, '0', 'f', 'false', 'off', 'n', 'no']
|
12
|
+
.map(&:freeze).freeze
|
7
13
|
|
8
14
|
class << self
|
9
|
-
def from
|
10
|
-
return true if
|
11
|
-
return false if
|
15
|
+
def from(value)
|
16
|
+
return true if true?(value)
|
17
|
+
return false if false?(value)
|
12
18
|
nil
|
13
19
|
end
|
14
20
|
|
15
|
-
def
|
16
|
-
return true if
|
21
|
+
def false?(value)
|
22
|
+
return true if FALSES.include?(value)
|
17
23
|
if value.respond_to?(:downcase)
|
18
|
-
return true if
|
24
|
+
return true if FALSES.include?(value.downcase)
|
19
25
|
end
|
20
26
|
false
|
21
27
|
end
|
22
28
|
|
23
|
-
def
|
24
|
-
return true if
|
29
|
+
def true?(value)
|
30
|
+
return true if TRUES.include?(value)
|
25
31
|
if value.respond_to?(:downcase)
|
26
|
-
return true if
|
32
|
+
return true if TRUES.include?(value.downcase)
|
27
33
|
end
|
28
34
|
false
|
29
35
|
end
|
36
|
+
|
37
|
+
def is_false?(value)
|
38
|
+
deprecate :is_false?, :false?, :'0.1.0'
|
39
|
+
false?(value)
|
40
|
+
end
|
41
|
+
|
42
|
+
def is_true?(value)
|
43
|
+
deprecate :is_true?, :true?, :'0.1.0'
|
44
|
+
true?(value)
|
45
|
+
end
|
30
46
|
end
|
31
47
|
end
|
32
48
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CodeKindly
|
4
|
+
module Utils
|
5
|
+
class Command
|
6
|
+
include Presence
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def run(command)
|
10
|
+
new(command).run
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :command
|
15
|
+
|
16
|
+
def initialize(command)
|
17
|
+
@command = command
|
18
|
+
end
|
19
|
+
|
20
|
+
def run
|
21
|
+
require 'open3'
|
22
|
+
command = @command
|
23
|
+
command.join(' ') if command.is_a?(Array)
|
24
|
+
@std_in, @std_out, @std_err = Open3.capture3(command)
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def result
|
29
|
+
return nil if blank? @std_out
|
30
|
+
@std_out.chomp!
|
31
|
+
blank? @std_out ? nil : @std_out
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CodeKindly
|
4
|
+
module Utils
|
5
|
+
module Deprecation
|
6
|
+
class << self
|
7
|
+
private
|
8
|
+
|
9
|
+
def deprecate(old_m, new_m = nil, version = nil)
|
10
|
+
msg = "[DEPRECATION] `#{old_m}` is deprecated"
|
11
|
+
version ? " and will be removed in version #{version}." : '.'
|
12
|
+
msg += " Please use `#{new_m}` instead." if new_m
|
13
|
+
warn msg
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def deprecate(old_m, new_m = nil, version = nil)
|
20
|
+
self.class.deprecate(old_m, new_m, version)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -1,17 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module CodeKindly
|
2
4
|
module Utils
|
3
5
|
class Dir
|
6
|
+
SKIP_DIRS = ['.', '..', '.DS_Store', '.keep'].map(&:freeze).freeze
|
7
|
+
|
4
8
|
class << self
|
5
|
-
def all
|
6
|
-
require
|
9
|
+
def all(path)
|
10
|
+
require 'fileutils'
|
7
11
|
return [] unless ::Dir.exist?(path)
|
8
12
|
files = ::Dir.entries(path)
|
9
|
-
files.reject!{ |f|
|
13
|
+
files.reject! { |f| SKIP_DIRS.include? f }
|
10
14
|
files.sort
|
11
15
|
end
|
12
16
|
|
13
|
-
def find
|
14
|
-
require
|
17
|
+
def find(path)
|
18
|
+
require 'fileutils'
|
15
19
|
all(path).select { |entry| ::File.directory?("#{path}/#{entry}") }
|
16
20
|
end
|
17
21
|
end
|
@@ -1,58 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module CodeKindly
|
2
4
|
module Utils
|
3
5
|
class File
|
6
|
+
include Presence
|
7
|
+
|
4
8
|
class << self
|
5
|
-
def all
|
9
|
+
def all(path)
|
6
10
|
CodeKindly::Utils::Dir.all path
|
7
11
|
end
|
8
12
|
|
9
|
-
def choose_from_options
|
10
|
-
require
|
11
|
-
|
12
|
-
file_opts = file_options(
|
13
|
-
return nil if
|
14
|
-
msg =
|
15
|
-
|
16
|
-
msg += "\n #{k}: #{v}"
|
17
|
-
end
|
18
|
-
msg += "\n 0: None of the above"
|
19
|
-
option = h.ask(msg, Integer)
|
13
|
+
def choose_from_options(dir_path, h_l = nil)
|
14
|
+
require 'highline'
|
15
|
+
h_l ||= HighLine.new
|
16
|
+
file_opts = file_options(dir_path)
|
17
|
+
return nil if blank? file_opts
|
18
|
+
msg = file_opts.inject('') { |(k, v), m| m + "\n #{k}: #{v}" }
|
19
|
+
option = h_l.ask("Select a file:#{msg}\n 0: None", Integer)
|
20
20
|
file_path = file_opts.fetch(option, nil)
|
21
|
-
if file_path.
|
22
|
-
|
23
|
-
end
|
24
|
-
file_path
|
21
|
+
return if file_path.nil?
|
22
|
+
::File.join(dir_path, file_path)
|
25
23
|
end
|
26
24
|
|
27
|
-
def file_options
|
28
|
-
require
|
25
|
+
def file_options(path)
|
26
|
+
require 'map'
|
29
27
|
options = Map.new
|
30
28
|
key = 0
|
31
29
|
find(path).each do |file|
|
32
|
-
options[key+=1] = file
|
30
|
+
options[key += 1] = file
|
33
31
|
end
|
34
32
|
options
|
35
33
|
end
|
36
34
|
|
37
|
-
def find
|
38
|
-
require
|
35
|
+
def find(path)
|
36
|
+
require 'fileutils'
|
39
37
|
all(path).select { |entry| ::File.file?("#{path}/#{entry}") }
|
40
38
|
end
|
41
39
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
40
|
+
# move to trash (or delete) existing downloaded files
|
41
|
+
# sudo gem install osx-trash (http://www.dribin.org/dave/blog/archives/2008/05/24/osx_trash/)
|
42
|
+
def trash!(file_string)
|
43
|
+
Kernel.system(command_to_trash_files(file_string))
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def command_to_trash_files(file_string)
|
49
|
+
return if Command.run("ls #{file_string}").result.nil?
|
50
|
+
trash = OS.which('trash')
|
51
|
+
if trash then "#{trash.chomp} #{file_string}"
|
52
|
+
elsif ::File.directory?('~/.Trash') then "mv #{file_string} ~/.Trash"
|
53
|
+
else "rm #{file_string}"
|
56
54
|
end
|
57
55
|
end
|
58
56
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CodeKindly
|
4
|
+
module Utils
|
5
|
+
class OS
|
6
|
+
class << self
|
7
|
+
def notify(message)
|
8
|
+
return if terminal_notifier.nil?
|
9
|
+
Command.run [
|
10
|
+
terminal_notifier,
|
11
|
+
"-message \"#{message}\"",
|
12
|
+
'-sound Submarine'
|
13
|
+
].join(' ')
|
14
|
+
end
|
15
|
+
|
16
|
+
def which(program)
|
17
|
+
Command.run("which #{program}").result
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def terminal_notifier
|
23
|
+
unless instance_variable_defined? :@terminal_notifier
|
24
|
+
@terminal_notifer = which('terminal-notifier')
|
25
|
+
end
|
26
|
+
@terminal_notifier
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CodeKindly
|
4
|
+
module Utils
|
5
|
+
module Presence
|
6
|
+
def self.blank?(value)
|
7
|
+
# http://apidock.com/rails/Object/blank%3F
|
8
|
+
value.respond_to?(:empty?) ? !!value.empty? : !value
|
9
|
+
end
|
10
|
+
|
11
|
+
def blank?(value)
|
12
|
+
self.class.blank?(value)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.present?(value)
|
16
|
+
!blank?(value)
|
17
|
+
end
|
18
|
+
|
19
|
+
def present?(value)
|
20
|
+
self.class.present?(value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -1,11 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module CodeKindly
|
2
4
|
module Utils
|
3
5
|
class Shell
|
6
|
+
include Deprecation
|
7
|
+
|
4
8
|
class << self
|
5
|
-
def run
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
+
def run(command)
|
10
|
+
deprecate :'Shell.run', :'Command.run', :'0.1.0'
|
11
|
+
require 'open3'
|
12
|
+
command = command.join(' ') if command.is_a?(Array)
|
13
|
+
Open3.capture3 command
|
9
14
|
end
|
10
15
|
end
|
11
16
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
module CodeKindly
|
6
|
+
module Utils
|
7
|
+
class SQL
|
8
|
+
class << self
|
9
|
+
def method_missing(method, *args)
|
10
|
+
m_name = method.to_s
|
11
|
+
return process(m_name, *args) if respond_to_missing?(m_name)
|
12
|
+
select_mn = 'select_' + m_name
|
13
|
+
return process(select_mn, *args) if respond_to_missing?(select_mn)
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
def respond_to_missing?(method, _include_all = false)
|
18
|
+
return false unless default_connection_class
|
19
|
+
default_connection_class.connection.respond_to?(method)
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def default_connection_class
|
25
|
+
@default_connection_class ||= begin
|
26
|
+
CodeKindly::Utils::ActiveRecord.default_connection_class
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def process(method_name, query, connection_class = NilClass)
|
31
|
+
connection_class = find_connection_class(query, connection_class)
|
32
|
+
query = query.to_sql if query.is_a?(::ActiveRecord::Relation)
|
33
|
+
connection_class.connection.send(method_name, query)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def find_connection_class(query, klass)
|
39
|
+
return query.klass if query.is_a?(::ActiveRecord::Relation)
|
40
|
+
if klass.respond_to? :to_sym
|
41
|
+
klass = CodeKindly::Utils::ActiveRecord.default_connection_class(klass)
|
42
|
+
else
|
43
|
+
klass = klass.class unless klass.is_a?(Class)
|
44
|
+
klass = NilClass if klass < ::ActiveRecord::Base
|
45
|
+
end
|
46
|
+
klass = default_connection_class if klass == NilClass
|
47
|
+
klass
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/codekindly-utils.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'code_kindly/utils'
|
2
4
|
|
3
5
|
if Kernel.const_defined? :CK
|
4
|
-
warn
|
6
|
+
warn '`CK` is already defined as a constant, so you will need to use' \
|
7
|
+
' the full `CodeKindly::Utils` module name in this project.'
|
5
8
|
else
|
6
9
|
CK = CodeKindly::Utils
|
7
10
|
end
|
metadata
CHANGED
@@ -1,15 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codekindly-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Weathers
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: highline
|
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: map
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: appraisal
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
13
69
|
- !ruby/object:Gem::Dependency
|
14
70
|
name: bundler
|
15
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -24,20 +80,34 @@ dependencies:
|
|
24
80
|
- - "~>"
|
25
81
|
- !ruby/object:Gem::Version
|
26
82
|
version: '1.16'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry-byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
27
97
|
- !ruby/object:Gem::Dependency
|
28
98
|
name: rake
|
29
99
|
requirement: !ruby/object:Gem::Requirement
|
30
100
|
requirements:
|
31
|
-
- - "
|
101
|
+
- - ">="
|
32
102
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
103
|
+
version: '0'
|
34
104
|
type: :development
|
35
105
|
prerelease: false
|
36
106
|
version_requirements: !ruby/object:Gem::Requirement
|
37
107
|
requirements:
|
38
|
-
- - "
|
108
|
+
- - ">="
|
39
109
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
110
|
+
version: '0'
|
41
111
|
- !ruby/object:Gem::Dependency
|
42
112
|
name: rspec
|
43
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,6 +122,34 @@ dependencies:
|
|
52
122
|
- - "~>"
|
53
123
|
- !ruby/object:Gem::Version
|
54
124
|
version: '3.0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: sqlite3
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
55
153
|
description: There is nothing terribly unique or interesting here and there are almost
|
56
154
|
certainly better ways to do things than this. These are little snippets I've found
|
57
155
|
useful in the past and continue to use through inertia.
|
@@ -63,22 +161,33 @@ extra_rdoc_files: []
|
|
63
161
|
files:
|
64
162
|
- ".gitignore"
|
65
163
|
- ".rspec"
|
164
|
+
- ".rubocop.yml"
|
165
|
+
- Appraisals
|
166
|
+
- CONTRIBUTING.md
|
66
167
|
- Gemfile
|
67
168
|
- LICENSE.txt
|
68
169
|
- README.md
|
69
170
|
- Rakefile
|
171
|
+
- all_rubies
|
70
172
|
- bin/console
|
71
173
|
- bin/setup
|
72
174
|
- codekindly-utils.gemspec
|
175
|
+
- gemfiles/activerecord_4_2.gemfile
|
176
|
+
- gemfiles/activerecord_5_0.gemfile
|
177
|
+
- gemfiles/activerecord_5_1.gemfile
|
178
|
+
- gemfiles/activerecord_5_2.gemfile
|
73
179
|
- lib/code_kindly/utils.rb
|
74
180
|
- lib/code_kindly/utils/active_record.rb
|
75
181
|
- lib/code_kindly/utils/boolean.rb
|
182
|
+
- lib/code_kindly/utils/command.rb
|
183
|
+
- lib/code_kindly/utils/deprecation.rb
|
76
184
|
- lib/code_kindly/utils/dir.rb
|
77
185
|
- lib/code_kindly/utils/file.rb
|
78
|
-
- lib/code_kindly/utils/
|
186
|
+
- lib/code_kindly/utils/os.rb
|
187
|
+
- lib/code_kindly/utils/presence.rb
|
79
188
|
- lib/code_kindly/utils/rake_task.rb
|
80
|
-
- lib/code_kindly/utils/s_q_l.rb
|
81
189
|
- lib/code_kindly/utils/shell.rb
|
190
|
+
- lib/code_kindly/utils/sql.rb
|
82
191
|
- lib/code_kindly/utils/version.rb
|
83
192
|
- lib/codekindly-utils.rb
|
84
193
|
homepage: https://github.com/jlw/codekindly-utils
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module CodeKindly
|
2
|
-
module Utils
|
3
|
-
class OS
|
4
|
-
class << self
|
5
|
-
def notify (message)
|
6
|
-
require "open3"
|
7
|
-
stdin, stdout, stderr = Open3.popen3("which terminal-notifier")
|
8
|
-
tn_path = stdout.gets
|
9
|
-
if tn_path.present?
|
10
|
-
Kernel.system("#{tn_path.chomp} -message \"#{message}\" -sound Submarine")
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
module CodeKindly
|
2
|
-
module Utils
|
3
|
-
class SQL
|
4
|
-
class << self
|
5
|
-
def method_missing (method, *args)
|
6
|
-
method_name = method.to_s
|
7
|
-
return process(method_name, *args) if respond_to_missing?(method_name)
|
8
|
-
select_method_name = "select_" + method_name
|
9
|
-
return process(select_method_name, *args) if respond_to_missing?(select_method_name)
|
10
|
-
super
|
11
|
-
end
|
12
|
-
|
13
|
-
def respond_to_missing? (method, _include_all = false)
|
14
|
-
default_connection_class && default_connection_class.connection.respond_to?(method)
|
15
|
-
end
|
16
|
-
|
17
|
-
protected
|
18
|
-
|
19
|
-
def default_connection_class
|
20
|
-
@default_connection_class ||= CodeKindly::Utils::ActiveRecord.default_connection_class
|
21
|
-
end
|
22
|
-
|
23
|
-
def process (method_name, query, connection_class = NilClass)
|
24
|
-
if query.is_a?(::ActiveRecord::Relation)
|
25
|
-
connection_class = query.klass
|
26
|
-
query = query.to_sql
|
27
|
-
else
|
28
|
-
if connection_class.respond_to? :to_sym
|
29
|
-
connection_class = CodeKindly::Utils::ActiveRecord.default_connection_class(connection_class)
|
30
|
-
else
|
31
|
-
connection_class = connection_class.class unless connection_class.is_a?(Class)
|
32
|
-
connection_class = NilClass if connection_class < ::ActiveRecord::Base
|
33
|
-
end
|
34
|
-
end
|
35
|
-
connection_class = default_connection_class if connection_class == NilClass
|
36
|
-
connection_class.connection.send(method_name, query)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|