tcravit_ruby_lib 0.0.8 → 0.2.7

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 10035a5eb75e32c6e501a23706115d956173a4ad0c1ef200fd788321b903c51f
4
+ data.tar.gz: '0428138258d21e3df798abb6f4966d1e8910f5c5158818e3bf04ea95a830075c'
5
+ SHA512:
6
+ metadata.gz: f87829a1df51e1bed94ee6b8caf23db45994567a8a759a2adf7b56cec1001f38e76bc51935a03d257be3252bb18128d1015ced8a957b46673155f66f9ebbc55e
7
+ data.tar.gz: e9ce8fcc9249239b74a5e4375ed8c05a2c2ec2a0871ced5f2a953ca44158e465da66bf56cbc5abe064ec7f8f732b1193e27af93c86227d506399ebe0822c24e2
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ tcravit_ruby_lib
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.4.3
data/.travis.yml CHANGED
@@ -1,2 +1,5 @@
1
1
  rvm:
2
2
  - 1.9.3
3
+ - 2.4.3
4
+ - 2.3.6
5
+ - 2.2.9
data/README.md CHANGED
@@ -2,3 +2,22 @@
2
2
 
3
3
  This gem is a collection of random Ruby stuff I use all over the place and
4
4
  am collecting here.
5
+
6
+ For information about what's here and how to use it, consult the RSpec
7
+ tests in the spec/ directory.
8
+
9
+ == License
10
+
11
+ Copyright 2018, Tammy Cravit.
12
+
13
+ Licensed under the Apache License, Version 2.0 (the "License");
14
+ you may not use this file except in compliance with the License.
15
+ You may obtain a copy of the License at
16
+
17
+ http://www.apache.org/licenses/LICENSE-2.0
18
+
19
+ Unless required by applicable law or agreed to in writing, software
20
+ distributed under the License is distributed on an "AS IS" BASIS,
21
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
+ See the License for the specific language governing permissions and
23
+ limitations under the License.
data/Rakefile CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rspec/core/rake_task'
2
2
  require "bundler/gem_tasks"
3
+ require "tcravit_ruby_lib/rake_tasks"
3
4
 
4
5
  RSpec::Core::RakeTask.new('spec')
5
6
 
6
- task :default => [:spec]
7
+ task :default => [:spec]
@@ -0,0 +1,64 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : app_banner.rb
5
+ # Description : Helper function for generating app startup banners for
6
+ # command line applications/scripts.
7
+ ############################################################################
8
+ # Copyright 2011-2018, Tammy Cravit.
9
+ #
10
+ # Licensed under the Apache License, Version 2.0 (the "License");
11
+ # you may not use this file except in compliance with the License.
12
+ # You may obtain a copy of the License at
13
+ #
14
+ # http://www.apache.org/licenses/LICENSE-2.0
15
+ #
16
+ # Unless required by applicable law or agreed to in writing, software
17
+ # distributed under the License is distributed on an "AS IS" BASIS,
18
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ # See the License for the specific language governing permissions and
20
+ # limitations under the License.
21
+ ############################################################################
22
+
23
+ module TcravitRubyLib #-nodoc-#
24
+ def self.Banner(opts={})
25
+ raise ArgumentError, "Name not provided" unless opts.keys.include?("name".to_sym)
26
+
27
+ line_length = 76
28
+ line_length = opts[:line_length] if opts.keys.include?(:line_length)
29
+
30
+ lines = []
31
+ lines.push("*" * line_length)
32
+
33
+ name_line = "#{opts[:name]}"
34
+ if (opts.keys.include?(:description)) then
35
+ name_line = name_line + ": " + opts[:description]
36
+ end
37
+ lines.push(asterisk_pad(name_line, line_length))
38
+
39
+ date_line_parts = []
40
+ if (opts.keys.include?(:version)) then
41
+ date_line_parts.push("Version #{opts[:version]}")
42
+ end
43
+ if (opts.keys.include?(:date)) then
44
+ date_line_parts.push(opts[:date])
45
+ end
46
+ if (opts.keys.include?(:author)) then
47
+ date_line_parts.push(opts[:author])
48
+ end
49
+ unless date_line_parts.empty?
50
+ lines.push(asterisk_pad(date_line_parts.join(", ")))
51
+ end
52
+
53
+ lines.push("*" * line_length)
54
+
55
+ return lines.join("\n")
56
+ end
57
+
58
+ def self.asterisk_pad(data, length=76)
59
+ buf = "* " + (" " * (((length-4) - data.length) / 2)) + data
60
+ buf = buf + (" " * ((length-2) - buf.length)) + " *"
61
+ return buf
62
+ end
63
+
64
+ end
@@ -1,3 +1,24 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : app_config.rb
5
+ # Description : Simple and flexible DSL-like app config storage.
6
+ ############################################################################
7
+ # Copyright 2011-2018, Tammy Cravit.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ ############################################################################
21
+
1
22
  module TcravitRubyLib #:nodoc:
2
23
 
3
24
  # Simple and Flexible Configuration Data Storage.
@@ -20,12 +41,6 @@ module TcravitRubyLib #:nodoc:
20
41
  # TcravitRubyLib::AppConfig.some_value = 2048
21
42
  # TcravitRubyLib::AppConfig.some_value # => 2048
22
43
  #
23
- # Author:: Tammy Cravit (mailto:tammy@tammycravit.com)
24
- # Copyright:: Copyright (c) 2011, Tammy Cravit. All rights reserved.
25
- # License:: This program is free software: you can redistribute it and/or modify
26
- # it under the terms of the GNU General Public License as published by
27
- # the Free Software Foundation, either version 3 of the License, or
28
- # (at your option) any later version.
29
44
 
30
45
  module AppConfig
31
46
  extend self
@@ -1,3 +1,25 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : config_searcher.rb
5
+ # Description : Locate the configuration directory for an app by traversing
6
+ # the filesystem looking for a .git directory.
7
+ ############################################################################
8
+ # Copyright 2011-2018, Tammy Cravit.
9
+ #
10
+ # Licensed under the Apache License, Version 2.0 (the "License");
11
+ # you may not use this file except in compliance with the License.
12
+ # You may obtain a copy of the License at
13
+ #
14
+ # http://www.apache.org/licenses/LICENSE-2.0
15
+ #
16
+ # Unless required by applicable law or agreed to in writing, software
17
+ # distributed under the License is distributed on an "AS IS" BASIS,
18
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ # See the License for the specific language governing permissions and
20
+ # limitations under the License.
21
+ ############################################################################
22
+
1
23
  require 'pathname'
2
24
 
3
25
  module TcravitRubyLib #:nodoc:
@@ -45,7 +67,8 @@ module TcravitRubyLib #:nodoc:
45
67
 
46
68
  dir = Pathname.new(start_dir)
47
69
  app_config_dir = dir + config_dir_name
48
-
70
+
71
+ begin
49
72
  if dir.children.include?(app_config_dir)
50
73
  if only_container_dir
51
74
  app_config_dir.to_s.split('/')[0..-2].join('/')
@@ -59,6 +82,9 @@ module TcravitRubyLib #:nodoc:
59
82
  # I'm sure there's a better way to do this.
60
83
  locate_config_dir(opts.reject{|k,v| k == :start_in}.merge({start_in: dir.parent.to_s}))
61
84
  end
85
+ rescue
86
+ raise ArgumentError, "Start directory \"#{start_dir}\" doesn't exist"
87
+ end
62
88
  end
63
89
  end
64
- end
90
+ end
@@ -0,0 +1,73 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : configurable.rb
5
+ # Description : Make a class configurable via a simple DSL
6
+ ############################################################################
7
+ # Copyright 2011-2018, Tammy Cravit.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ ############################################################################
21
+
22
+ module TcravitRubyLib #:nodoc:
23
+
24
+ # Make a class configurable via a DSL
25
+ #
26
+ # From https://www.toptal.com/ruby/ruby-dsl-metaprogramming-guide
27
+ #
28
+ # Sample Usage:
29
+ #
30
+ # class MyApp
31
+ # include Configurable.with(:app_id, :title, :cookie_name)
32
+ #
33
+ # # ...
34
+ # end
35
+ #
36
+ # SomeClass.configure do
37
+ # app_id "my_app"
38
+ # title "My App"
39
+ # cookie_name { "#{app_id}_session" }
40
+ # end
41
+
42
+ module Configurable
43
+ def self.with(*attrs)
44
+ not_provided = Object.new
45
+ config_class = Class.new do
46
+ attrs.each do |attr|
47
+ define_method attr do |value = not_provided, &block|
48
+ if value === not_provided && block.nil?
49
+ result = instance_variable_get("@#{attr}")
50
+ result.is_a?(Proc) ? instance_eval(&result) : result
51
+ else
52
+ instance_variable_set("@#{attr}", block || value)
53
+ end
54
+ end
55
+ end
56
+ attr_writer *attrs
57
+ end
58
+ class_methods = Module.new do
59
+ define_method :config do
60
+ @config ||= config_class.new
61
+ end
62
+ def configure(&block)
63
+ config.instance_eval(&block)
64
+ end
65
+ end
66
+ Module.new do
67
+ singleton_class.send :define_method, :included do |host_class|
68
+ host_class.extend class_methods
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -1,17 +1,48 @@
1
- ###########################################################################
2
- # A bit of ruby hackery to enable the script to be run directly
3
- # from the command line, as well as included into other code.
4
- # (Courtesy of elliottcable).
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
5
3
  #
6
- # Not autoloaded by the Gem; require 'tcravit_ruby_lib/on_execute' to pull
7
- # it in.
8
- ###########################################################################
4
+ # File : on_execute.rb
5
+ # Description : Make a script both includable and directly runnable
6
+ ############################################################################
7
+ # Copyright 2011-2018, Tammy Cravit.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ ############################################################################
21
+
9
22
 
10
23
  module Kernel
24
+ #########################################################################
25
+ # A bit of ruby hackery to enable the script to be run directly
26
+ # from the command line, as well as included into other code.
27
+ # (Courtesy of elliottcable).
28
+ #
29
+ # Example:
30
+ #
31
+ # class MyApp
32
+ # # ...
33
+ # def start()
34
+ # #...
35
+ # end
36
+ # end
37
+ #
38
+ # on_execute do
39
+ # MyApp.new().start()
40
+ # end
41
+ #########################################################################
11
42
  def on_execute
12
43
  calling_file = caller.first.split(':').first
13
44
  if File.expand_path(calling_file) == File.expand_path($0)
14
45
  yield
15
46
  end
16
47
  end
17
- end
48
+ end
@@ -0,0 +1,36 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : rake_tasks.rb
5
+ # Description : Helper for loading shared Rake tasks
6
+ ############################################################################
7
+ # Copyright 2011-2018, Tammy Cravit.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ ############################################################################
21
+
22
+ require 'rake'
23
+
24
+ module TcravitRubyLib
25
+ class RakeTasks
26
+ include Rake::DSL if defined?(Rake::DSL)
27
+
28
+ def install_tasks
29
+ Dir["#{File.join(File.dirname(__FILE__), 'tasks')}/**/*.rake"].each do |f|
30
+ load f
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ TcravitRubyLib::RakeTasks.new.install_tasks
@@ -0,0 +1,136 @@
1
+ ############################################################################
2
+ # Rake tasks to update the version.rb file for a Gem
3
+ #
4
+ # Version 1.0, 2018-02-01, tammycravit@me.com
5
+ ############################################################################
6
+ # Copyright 2018, Tammy Cravit.
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ ############################################################################
20
+
21
+ def find_version_file
22
+ curr_dir = File.dirname(__FILE__)
23
+ while File.exist?(File.join(curr_dir, "Rakefile")) == false
24
+ if curr_dir == "/"
25
+ return nil
26
+ else
27
+ curr_dir = File.expand_path(curr_dir + "/..")
28
+ end
29
+ end
30
+ if File.directory?(File.join(curr_dir)) then
31
+ ver_file = Dir["#{curr_dir}/**/version.rb"]
32
+ if ver_file.nil?
33
+ return nil
34
+ else
35
+ return ver_file[0]
36
+ end
37
+ end
38
+ end
39
+
40
+ def write_output_to(file, output)
41
+ open(file, "w") do |f|
42
+ f.puts(output)
43
+ end
44
+ end
45
+
46
+ def create_file_contents(module_name, version_data)
47
+ buf = "module #{module_name}\n"
48
+ buf = buf + "\tVERSION_DATA = [" + version_data.join(", ") + "]\n"
49
+ buf = buf + "\tVERSION = VERSION_DATA.join(\".\")\n"
50
+ buf = buf + "end\n"
51
+ end
52
+
53
+ def load_gem_version_file
54
+ version_file = find_version_file
55
+ if version_file.nil?
56
+ raise ArgumentError, "Could not find version file"
57
+ exit
58
+ end
59
+ require version_file
60
+ return version_file
61
+ end
62
+
63
+ def find_module_name_from(version_file)
64
+ module_name = ""
65
+ File.foreach(version_file) do |l|
66
+ next if l.match(/^\s*$/)
67
+ next if l.match(/^\s*\#/)
68
+ m = l.match(/^\s*module (\S+)\s*$/)
69
+ unless m.nil?
70
+ module_name = m[1]
71
+ break
72
+ end
73
+ end
74
+ return module_name
75
+ end
76
+
77
+ def update_gem_version_part(index=2, test_mode=false)
78
+ positions = ['major', 'minor', 'build']
79
+
80
+ version_file = load_gem_version_file
81
+ module_name = find_module_name_from(version_file)
82
+
83
+ vd = TcravitRubyLib::VERSION_DATA
84
+ vd[index] = vd[index] + 1
85
+
86
+ if test_mode then
87
+ write_output_to("/tmp/bump_ver.out", create_file_contents(module_name, vd))
88
+ else
89
+ write_output_to(version_file, create_file_contents(module_name, vd))
90
+ end
91
+
92
+ puts "Updated #{positions[index]} number to #{vd[index]}; version is now #{vd.join('.')}" unless test_mode
93
+ end
94
+
95
+ def set_gem_version(major, minor, build, test_mode=false)
96
+ version_file = load_gem_version_file
97
+ module_name = find_module_name_from(version_file)
98
+
99
+ vd = [major, minor, build]
100
+
101
+ if test_mode then
102
+ write_output_to("/tmp/bump_ver.out", create_file_contents(module_name, vd))
103
+ else
104
+ write_output_to(version_file, create_file_contents(module_name, vd))
105
+ end
106
+
107
+ puts "Forced Gem version to #{vd.join('.')}" unless test_mode
108
+ end
109
+
110
+ namespace :version do
111
+ namespace :bump do
112
+ desc 'Increment the major number of the gem'
113
+ task :major, [:test_mode] do |t, args|
114
+ update_gem_version_part 0, args[:test_mode]
115
+ end
116
+
117
+ desc 'Increment the minor number of the gem'
118
+ task :minor, [:test_mode] do |t, args|
119
+ update_gem_version_part 1, args[:test_mode]
120
+ end
121
+
122
+ desc 'Increment the build number of the gem'
123
+ task :build, [:test_mode] do |t, args|
124
+ update_gem_version_part 2, args[:test_mode]
125
+ end
126
+
127
+ desc "Set the version number to a specific version"
128
+ task :set, [:major, :minor, :build, :test_mode] do |t, args|
129
+ major = args[:major] || 1
130
+ minor = args[:minor] || 0
131
+ build = args[:build] || 0
132
+
133
+ set_gem_version major, minor, build, args[:test_mode]
134
+ end
135
+ end
136
+ end
@@ -1,11 +1,37 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : utility.rb
5
+ # Description : Random small utility functions
6
+ ############################################################################
7
+ # Copyright 2011-2018, Tammy Cravit.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ ############################################################################
21
+
22
+ require 'simple-password-gen'
23
+
1
24
  module TcravitRubyLib
2
25
  module Utility
3
26
  extend self
4
27
 
5
- def random_alphanumeric(size=16)
6
- alphanumerics = [('0'..'9'),('A'..'Z'),('a'..'z')].map {|range| range.to_a}.flatten
7
- (0...size).map { alphanumerics[Kernel.rand(alphanumerics.size)] }.join
28
+ def random_alphanumeric(size=16, pronounceable=false)
29
+ if pronounceable then
30
+ return Password.pronounceable(size*2)[0..(size-1)]
31
+ else
32
+ return Password.random(size*2)[0..(size-1)]
33
+ end
8
34
  end
9
35
 
10
36
  end
11
- end
37
+ end
@@ -1,3 +1,4 @@
1
1
  module TcravitRubyLib
2
- VERSION = "0.0.8"
2
+ VERSION_DATA = [0, 2, 7]
3
+ VERSION = VERSION_DATA.join(".")
3
4
  end
@@ -1,8 +1,32 @@
1
+ ############################################################################
2
+ # TcravitRubyLib: Random useful stuff for Ruby programming.
3
+ #
4
+ # File : tcravit_ruby_lib.rb
5
+ # Description : Main include file for the library; autoloads all its parts
6
+ ############################################################################
7
+ # Copyright 2011-2018, Tammy Cravit.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ ############################################################################
21
+
1
22
  $:.unshift File.dirname(__FILE__)
2
23
 
24
+ # Make sure we load the version definitions first
3
25
  require "tcravit_ruby_lib/version"
4
- require 'tcravit_ruby_lib/config_searcher'
5
- require 'tcravit_ruby_lib/app_config'
26
+
27
+ Dir[File.join(File.dirname(__FILE__), "tcravit_ruby_lib", "*.rb")].each do |f|
28
+ require File.join("tcravit_ruby_lib", File.basename(f)) unless File.basename(f) == "version.rb"
29
+ end
6
30
 
7
31
  module TcravitRubyLib # :nodoc:
8
32
  end