codekindly-utils 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/README.md +7 -0
- data/codekindly-utils.gemspec +2 -0
- data/lib/code_kindly/utils/active_record.rb +56 -3
- data/lib/code_kindly/utils/boolean.rb +2 -2
- data/lib/code_kindly/utils/dir.rb +2 -2
- data/lib/code_kindly/utils/file.rb +8 -9
- data/lib/code_kindly/utils/o_s.rb +2 -2
- data/lib/code_kindly/utils/s_q_l.rb +41 -0
- data/lib/code_kindly/utils/shell.rb +1 -1
- data/lib/code_kindly/utils/version.rb +1 -1
- data/lib/code_kindly/utils.rb +1 -1
- data/lib/codekindly-utils.rb +7 -1
- metadata +4 -4
- data/lib/code_kindly/utils/database.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 553c215327bff6738d763dfa4ee525917c2f6758
|
4
|
+
data.tar.gz: d614eca29822630cca1e30724ae12e3809b382cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 137a2c92900370c9ac9401a253e39ba7878aaaa4bd3fa0cfd27a85aa4b7616019cb15d0acf46ae8bdaf9f94ee68371067d4dadcb8f5d7e07230134de2f0dde0e
|
7
|
+
data.tar.gz: 3ab9789d44c40fa6361761678bf733f67edff3f5e237a9b78338787fec548bf563a71002b21830550420a3fcea01f2fe6680489c79229c8be8558c2469448b03
|
data/README.md
CHANGED
@@ -22,6 +22,13 @@ 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
|
+
|
25
32
|
## Usage
|
26
33
|
|
27
34
|
I occasionally use these utilities in the main logic of an app, but usually
|
data/codekindly-utils.gemspec
CHANGED
@@ -21,6 +21,8 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
22
|
spec.require_paths = ["lib"]
|
23
23
|
|
24
|
+
spec.required_ruby_version = ">= 2.2"
|
25
|
+
|
24
26
|
spec.add_development_dependency "bundler", "~> 1.16"
|
25
27
|
spec.add_development_dependency "rake", "~> 10.0"
|
26
28
|
spec.add_development_dependency "rspec", "~> 3.0"
|
@@ -2,9 +2,62 @@ module CodeKindly
|
|
2
2
|
module Utils
|
3
3
|
class ActiveRecord
|
4
4
|
class << self
|
5
|
-
def
|
6
|
-
|
7
|
-
|
5
|
+
def active_record_classes_by_connection
|
6
|
+
@active_record_classes_by_connection ||= begin
|
7
|
+
sets = {}.with_indifferent_access
|
8
|
+
find_active_record_classes.each do |klass|
|
9
|
+
config_name = configurations.keys.select { |k| configurations[k]["database"] == klass.connection.current_database }.first
|
10
|
+
sets[config_name] ||= []
|
11
|
+
sets[config_name] << klass
|
12
|
+
end
|
13
|
+
sets
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def clear_scope (scope)
|
18
|
+
if 0 == scope.count
|
19
|
+
puts "Nothing to clear"
|
20
|
+
else
|
21
|
+
puts "Clearing #{scope.count} #{scope.name} records"
|
22
|
+
scope.delete_all
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def configurations
|
27
|
+
return unless active_record_available?
|
28
|
+
@configurations ||= YAML.load_file(::Rails.root.join(*%w[config database.yml]))
|
29
|
+
end
|
30
|
+
|
31
|
+
def config(name = nil)
|
32
|
+
return unless active_record_available?
|
33
|
+
name ||= ::Rails.env
|
34
|
+
configurations[name]
|
35
|
+
end
|
36
|
+
|
37
|
+
def default_connection_class (connection = nil)
|
38
|
+
return unless active_record_available?
|
39
|
+
connection ||= ::Rails.env
|
40
|
+
@default_connection_class ||= {}.with_indifferent_access
|
41
|
+
@default_connection_class[connection] ||= active_record_classes_by_connection.fetch(connection, []).first
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def active_record_available?
|
47
|
+
begin
|
48
|
+
::ActiveRecord
|
49
|
+
true
|
50
|
+
rescue NameError
|
51
|
+
raise NotImplementedError, "ActiveRecord is not loaded."
|
52
|
+
false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def find_active_record_classes
|
57
|
+
if ::Rails.env.development?
|
58
|
+
::Dir.glob(::Rails.root.join("app", "models").to_s + "/**/*.rb") { |f| require f }
|
59
|
+
end
|
60
|
+
ObjectSpace.each_object(Class).select { |klass| klass < ::ActiveRecord::Base && !klass.abstract_class }.sort_by(&:name)
|
8
61
|
end
|
9
62
|
end
|
10
63
|
end
|
@@ -2,8 +2,8 @@ module CodeKindly
|
|
2
2
|
module Utils
|
3
3
|
class Boolean
|
4
4
|
# modified from ActiveRecord::ConnectionAdapters::Column (4.2.9)
|
5
|
-
TRUE_VALUES = [true, 1,
|
6
|
-
FALSE_VALUES = [false, 0,
|
5
|
+
TRUE_VALUES = [true, 1, "1", "t", "T", "true", "TRUE", "on", "ON", "y", "yes"]
|
6
|
+
FALSE_VALUES = [false, 0, "0", "f", "F", "false", "FALSE", "off", "OFF", "n", "no"]
|
7
7
|
|
8
8
|
class << self
|
9
9
|
def from (value)
|
@@ -3,7 +3,7 @@ module CodeKindly
|
|
3
3
|
class Dir
|
4
4
|
class << self
|
5
5
|
def all (path)
|
6
|
-
require
|
6
|
+
require "fileutils"
|
7
7
|
return [] unless ::Dir.exist?(path)
|
8
8
|
files = ::Dir.entries(path)
|
9
9
|
files.reject!{ |f| "." == f || ".." == f || ".DS_Store" == f || ".keep" == f }
|
@@ -11,7 +11,7 @@ module CodeKindly
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def find (path)
|
14
|
-
require
|
14
|
+
require "fileutils"
|
15
15
|
all(path).select { |entry| ::File.directory?("#{path}/#{entry}") }
|
16
16
|
end
|
17
17
|
end
|
@@ -6,11 +6,10 @@ module CodeKindly
|
|
6
6
|
CodeKindly::Utils::Dir.all path
|
7
7
|
end
|
8
8
|
|
9
|
-
def choose_from_options (
|
9
|
+
def choose_from_options (directory_path, h = nil)
|
10
10
|
require "highline"
|
11
11
|
h ||= HighLine.new
|
12
|
-
|
13
|
-
file_opts = file_options(dir_path)
|
12
|
+
file_opts = file_options(directory_path)
|
14
13
|
return nil if file_opts.blank?
|
15
14
|
msg = "Select an existing file:"
|
16
15
|
file_opts.each do |k,v|
|
@@ -20,13 +19,13 @@ module CodeKindly
|
|
20
19
|
option = h.ask(msg, Integer)
|
21
20
|
file_path = file_opts.fetch(option, nil)
|
22
21
|
if file_path.present?
|
23
|
-
file_path = ::File.join(
|
22
|
+
file_path = ::File.join(directory_path, file_path)
|
24
23
|
end
|
25
24
|
file_path
|
26
25
|
end
|
27
26
|
|
28
27
|
def file_options (path)
|
29
|
-
require
|
28
|
+
require "map"
|
30
29
|
options = Map.new
|
31
30
|
key = 0
|
32
31
|
find(path).each do |file|
|
@@ -36,21 +35,21 @@ module CodeKindly
|
|
36
35
|
end
|
37
36
|
|
38
37
|
def find (path)
|
39
|
-
require
|
38
|
+
require "fileutils"
|
40
39
|
all(path).select { |entry| ::File.file?("#{path}/#{entry}") }
|
41
40
|
end
|
42
41
|
|
43
42
|
def trash! (file_string)
|
44
|
-
require
|
43
|
+
require "open3"
|
45
44
|
stdin, stdout, stderr = Open3.popen3("ls #{file_string}")
|
46
45
|
if stdout.gets
|
47
46
|
# move to trash (or delete) existing downloaded files
|
48
47
|
# sudo gem install osx-trash (http://www.dribin.org/dave/blog/archives/2008/05/24/osx_trash/)
|
49
|
-
stdin, stdout, stderr = Open3.popen3(
|
48
|
+
stdin, stdout, stderr = Open3.popen3("which trash")
|
50
49
|
trash = stdout.gets
|
51
50
|
command = case
|
52
51
|
when trash then "#{trash.strip} #{file_string}" # output of `which` has ending \n
|
53
|
-
when ::File.directory?(
|
52
|
+
when ::File.directory?("~/.Trash") then "mv #{file_string} ~/.Trash"
|
54
53
|
else "rm #{file_string}"
|
55
54
|
end
|
56
55
|
Kernel.system(command)
|
@@ -3,8 +3,8 @@ module CodeKindly
|
|
3
3
|
class OS
|
4
4
|
class << self
|
5
5
|
def notify (message)
|
6
|
-
require
|
7
|
-
stdin, stdout, stderr = Open3.popen3(
|
6
|
+
require "open3"
|
7
|
+
stdin, stdout, stderr = Open3.popen3("which terminal-notifier")
|
8
8
|
tn_path = stdout.gets
|
9
9
|
if tn_path.present?
|
10
10
|
Kernel.system("#{tn_path.chomp} -message \"#{message}\" -sound Submarine")
|
@@ -0,0 +1,41 @@
|
|
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 = nil)
|
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 = nil if connection_class < ::ActiveRecord::Base
|
33
|
+
end
|
34
|
+
connection_class ||= default_connection_class
|
35
|
+
end
|
36
|
+
connection_class.connection.send(method_name, query)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/code_kindly/utils.rb
CHANGED
data/lib/codekindly-utils.rb
CHANGED
@@ -1 +1,7 @@
|
|
1
|
-
require_relative "code_kindly/utils"
|
1
|
+
require_relative "code_kindly/utils"
|
2
|
+
|
3
|
+
if Kernel.const_defined? :CK
|
4
|
+
warn "`CK` is already defined as a constant, so you will need to use the full `CodeKindly::Utils` module name in this project."
|
5
|
+
else
|
6
|
+
CK = CodeKindly::Utils
|
7
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.2
|
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-04-
|
11
|
+
date: 2018-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,10 +73,10 @@ files:
|
|
73
73
|
- lib/code_kindly/utils.rb
|
74
74
|
- lib/code_kindly/utils/active_record.rb
|
75
75
|
- lib/code_kindly/utils/boolean.rb
|
76
|
-
- lib/code_kindly/utils/database.rb
|
77
76
|
- lib/code_kindly/utils/dir.rb
|
78
77
|
- lib/code_kindly/utils/file.rb
|
79
78
|
- lib/code_kindly/utils/o_s.rb
|
79
|
+
- lib/code_kindly/utils/s_q_l.rb
|
80
80
|
- lib/code_kindly/utils/shell.rb
|
81
81
|
- lib/code_kindly/utils/version.rb
|
82
82
|
- lib/codekindly-utils.rb
|
@@ -92,7 +92,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
92
|
requirements:
|
93
93
|
- - ">="
|
94
94
|
- !ruby/object:Gem::Version
|
95
|
-
version: '
|
95
|
+
version: '2.2'
|
96
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
97
|
requirements:
|
98
98
|
- - ">="
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module CodeKindly
|
2
|
-
module Utils
|
3
|
-
class Database
|
4
|
-
class << self
|
5
|
-
def clear_scope (scope)
|
6
|
-
if 0 == scope.count
|
7
|
-
puts "Nothing to clear"
|
8
|
-
else
|
9
|
-
puts "Clearing #{scope.count} #{scope.name} records"
|
10
|
-
scope.delete_all
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|