extras 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
+ SHA1:
3
+ metadata.gz: f103046f49374cf61bc34341760c19d4983be7a9
4
+ data.tar.gz: 16aca5fe7a55cc6ecbb4d3cd19f462e9f7753470
5
+ SHA512:
6
+ metadata.gz: b58cb7195b2f4fada3e477b240467c190f7ae0815d52ef594ee827f428bcb39f458a3c7d44a9f5d8ae6797f6b58034948f126dd0f659d2c5d0c08527c03abb36
7
+ data.tar.gz: a3c5c9a244badd20e5279b8276ace39bb1ab064d78f0cd1944c9b4a6f0bc6d4f0375cfe4924d4f9e3a6f43fe3918aef51cfee8edc12cf71f84261b1f41b8ac1d
data/Gemfile ADDED
@@ -0,0 +1,24 @@
1
+ # ----------------------------------------------------------------------------
2
+ # Frozen-string-literal: true
3
+ # Copyright: 2015-2016 Jordon Bedwell - MIT License
4
+ # Encoding: utf-8
5
+ # ----------------------------------------------------------------------------
6
+
7
+ source "https://rubygems.org"
8
+ gem "rake", :require => false
9
+ gem "addressable"
10
+ gem "i18n"
11
+ gemspec
12
+
13
+ group :test do
14
+ gem "liquid", :require => false
15
+ gem "codeclimate-test-reporter", :require => false
16
+ gem "luna-rspec-formatters", :require => false
17
+ gem "rspec", :require => false
18
+ end
19
+
20
+ group :development do
21
+ gem "luna-rubocop-formatters", :require => false
22
+ gem "rubocop", :github => "bbatsov/rubocop", :require => false
23
+ gem "pry", :require => false
24
+ end
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2015-2016 Jordon Bedwell
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the "Software"),
5
+ to deal in the Software without restriction, including without limitation
6
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
+ and/or sell copies of the Software, and to permit persons to whom the
8
+ Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included
11
+ in all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19
+ USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # ----------------------------------------------------------------------------
2
+ # Frozen-string-literal: true
3
+ # Copyright: 2015-2016 Jordon Bedwell - MIT License
4
+ # Encoding: utf-8
5
+ # ----------------------------------------------------------------------------
6
+
7
+ require "luna/rubocop/rake/task"
8
+ $LOAD_PATH.unshift(File.expand_path("../lib", __FILE__))
9
+ require "rspec/core/rake_task"
10
+
11
+ task :default => [:spec]
12
+ RSpec::Core::RakeTask.new :spec
13
+ task :test => :spec
data/lib/extras/all.rb ADDED
@@ -0,0 +1,9 @@
1
+ # ----------------------------------------------------------------------------
2
+ # Frozen-string-literal: true
3
+ # Copyright: 2015-2016 Jordon Bedwell - MIT License
4
+ # Encoding: utf-8
5
+ # ----------------------------------------------------------------------------
6
+
7
+ Dir[File.join(File.expand_path(__dir__), "{*,**/*}.rb")].each do |f|
8
+ require f
9
+ end
@@ -0,0 +1,62 @@
1
+ # ----------------------------------------------------------------------------
2
+ # Frozen-string-literal: true
3
+ # Copyright: 2015-2016 Jordon Bedwell - MIT License
4
+ # Encoding: utf-8
5
+ # ----------------------------------------------------------------------------
6
+
7
+ module Extras
8
+ module Array
9
+ module ClassMethods
10
+ def allowed
11
+ @allowed ||= begin
12
+ out = {
13
+ :keys => [::NilClass, ::Hash, ::TrueClass, \
14
+ ::FalseClass, ::Regexp, ::Array, ::Set, ::Fixnum,
15
+ ::Bignum, ::Float]
16
+ }
17
+ end
18
+ end
19
+ end
20
+
21
+ # ------------------------------------------------------------------------
22
+ # Stringify an array's keys, skipping anything within the allowed list.
23
+ # ------------------------------------------------------------------------
24
+
25
+ def stringify(allowed_keys: nil, allowed_vals: nil)
26
+ keys = allowed_keys || self.class.allowed[:keys]
27
+
28
+ map do |v|
29
+ v = v.to_s unless keys.include?(v.class)
30
+ !v.respond_to?(:stringify) ? v : v.stringify({
31
+ :allowed_keys => allowed_keys,
32
+ :allowed_vals => allowed_vals
33
+ })
34
+ end
35
+ end
36
+
37
+ # ------------------------------------------------------------------------
38
+ # Symbolize an array's keys, skpping anything within the allowed list.
39
+ # ------------------------------------------------------------------------
40
+
41
+ def symbolize(allowed_keys: nil, allowed_vals: nil)
42
+ keys = allowed_keys || self.class.allowed[:keys]
43
+
44
+ map do |v|
45
+ v = v.to_sym unless !v.respond_to?(:to_sym) || keys.include?(v.class)
46
+ !v.respond_to?(:symbolize) ? v : v.symbolize({
47
+ :allowed_keys => allowed_keys,
48
+ :allowed_vals => allowed_vals
49
+ })
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ #
56
+
57
+ class Array
58
+ prepend Extras::Array
59
+ class << self
60
+ prepend Extras::Array::ClassMethods
61
+ end
62
+ end
@@ -0,0 +1,125 @@
1
+ # ----------------------------------------------------------------------------
2
+ # Frozen-string-literal: true
3
+ # Copyright: 2015-2016 Jordon Bedwell - MIT License
4
+ # Encoding: utf-8
5
+ # ----------------------------------------------------------------------------
6
+
7
+ module Extras
8
+ module Hash
9
+ module ClassMethods
10
+ def allowed
11
+ @allowed ||= begin
12
+ {
13
+ :keys => [],
14
+ :vals => [::NilClass, ::Hash, ::TrueClass, \
15
+ ::FalseClass, ::Regexp, ::Array, ::Set, ::Fixnum,
16
+ ::Bignum, ::Float]
17
+ }
18
+ end
19
+ end
20
+ end
21
+
22
+ # ------------------------------------------------------------------------
23
+ # Symbolize keys and values of the current hash, skipping allowed objects.
24
+ # ------------------------------------------------------------------------
25
+
26
+ def symbolize(allowed_keys: nil, allowed_vals: nil)
27
+ keys = allowed_keys || self.class.allowed[:keys]
28
+ vals = allowed_vals || self.class.allowed[:vals]
29
+
30
+ each_with_object({}) do |(k, v), h|
31
+ k = k.to_sym unless !k.respond_to?(:to_sym) || keys.include?(k.class)
32
+ v = v.to_sym unless !v.respond_to?(:to_sym) || vals.include?(v.class)
33
+
34
+ h[k] = !v.respond_to?(:symbolize) ? v : v.symbolize({
35
+ :allowed_keys => allowed_keys,
36
+ :allowed_vals => allowed_vals
37
+ })
38
+ end
39
+ end
40
+
41
+ # ------------------------------------------------------------------------
42
+ # Stringify keys and values of the current hash, skipping objects
43
+ # that are allowed to be skipped (leaving them as is, untouched.)
44
+ # ------------------------------------------------------------------------
45
+
46
+ def stringify(allowed_keys: nil, allowed_vals: nil)
47
+ keys = allowed_keys || self.class.allowed[:keys]
48
+ vals = allowed_vals || self.class.allowed[:vals]
49
+
50
+ each_with_object({}) do |(k, v), h|
51
+ v = v.to_s if !v.respond_to?(:stringify) && !vals.include?(v.class)
52
+ k = k.to_s unless keys.include?(k.class)
53
+
54
+ h[k] = !v.respond_to?(:stringify) ? v : v.stringify({
55
+ :allowed_keys => allowed_keys,
56
+ :allowed_vals => allowed_vals
57
+ })
58
+ end
59
+ end
60
+
61
+ # ------------------------------------------------------------------------
62
+ # Stringify the keys of the current hash, skipping allowed objects.
63
+ # ------------------------------------------------------------------------
64
+
65
+ def stringify_keys(allowed: nil)
66
+ allowed ||= self.class.allowed[:keys]
67
+ each_with_object({}) do |(k, v), h|
68
+ k = k.to_s unless allowed.include?(k.class)
69
+ h[k] = !v.respond_to?(:stringify_keys) ? v : v.stringify_keys({
70
+ :allowed => allowed
71
+ })
72
+ end
73
+ end
74
+
75
+ # ------------------------------------------------------------------------
76
+ # Symbolize the keys of the current hash, skipping allowed objects.
77
+ # ------------------------------------------------------------------------
78
+
79
+ def symbolize_keys(allowed: nil)
80
+ allowed ||= self.class.allowed[:keys]
81
+ each_with_object({}) do |(k, v), h|
82
+ k = k.to_sym if k.respond_to?(:to_sym) && !allowed.include?(k.class)
83
+ h[k] = !v.respond_to?(:symbolize_keys) ? v : v.symbolize_keys({
84
+ :allowed => allowed
85
+ })
86
+ end
87
+ end
88
+
89
+ # ------------------------------------------------------------------------
90
+ # Merge hash into hash into hash into hash.
91
+ # ------------------------------------------------------------------------
92
+
93
+ def deep_merge(new_h)
94
+ merge(new_h) do |_, ov, nv|
95
+ if ov.respond_to?(:deep_merge) && nv.respond_to?(:deep_merge)
96
+ then ov.deep_merge(
97
+ nv
98
+ )
99
+
100
+ else
101
+ nv
102
+ end
103
+ end
104
+ end
105
+
106
+ # ------------------------------------------------------------------------
107
+ # Check to see if any of the given keys exist.
108
+ # ------------------------------------------------------------------------
109
+
110
+ def any_key?(*keys)
111
+ keys.any? do |k|
112
+ key?(k)
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ #
119
+
120
+ class Hash
121
+ prepend Extras::Hash
122
+ class << self
123
+ prepend Extras::Hash::ClassMethods
124
+ end
125
+ end
@@ -0,0 +1,59 @@
1
+ # ----------------------------------------------------------------------------
2
+ # Frozen-string-literal: true
3
+ # Copyright: 2015-2016 Jordon Bedwell - MIT License
4
+ # Encoding: utf-8
5
+ # ----------------------------------------------------------------------------
6
+
7
+ require "shellwords"
8
+
9
+ module Extras
10
+ module Shell
11
+
12
+ # ------------------------------------------------------------------------
13
+ # rubocop:disable Metrics/CyclomaticComplexity
14
+ # Escapes a string double checking if it will double escape.
15
+ # rubocop:disable Metrics/PerceivedComplexity
16
+ # Note: This is taken from Ruby 2.3 StdLib.
17
+ # ------------------------------------------------------------------------
18
+
19
+ def escape(str, original: false)
20
+ if original
21
+ return super(
22
+ str
23
+ )
24
+ end
25
+
26
+ if !str || str.empty? || str == '""' || str == "''"
27
+ return '""'
28
+
29
+ elsif str.is_a?(Array)
30
+ str.map do |v|
31
+ escape(v)
32
+ end
33
+
34
+ elsif str.is_a?(Hash)
35
+ str.each do |k, v|
36
+ str[k] = escape(
37
+ v
38
+ )
39
+ end
40
+
41
+ else
42
+ regexp = /((?:\\)?[^A-Za-z0-9_\-.,:\/@\n])/
43
+ str = str.gsub(regexp) { $1.start_with?("\\") ? $1 : "\\#{$1}" }
44
+ str = str.gsub(/\n/, "'\n'")
45
+ str
46
+ end
47
+ end
48
+
49
+ alias shellescape escape
50
+ end
51
+ end
52
+
53
+ #
54
+
55
+ module Shellwords
56
+ class << self
57
+ prepend Extras::Shell
58
+ end
59
+ end
@@ -0,0 +1,36 @@
1
+ # ----------------------------------------------------------------------------
2
+ # Frozen-string-literal: true
3
+ # Copyright: 2015-2016 Jordon Bedwell - MIT License
4
+ # Encoding: utf-8
5
+ # ----------------------------------------------------------------------------
6
+
7
+ require "forwardable/extended"
8
+
9
+ module Extras
10
+ module String
11
+ extend Forwardable::Extended
12
+ rb_delegate :regexp_escape, {
13
+ :alias_of => :escape,
14
+ :to => Regexp,
15
+ :args => [
16
+ :self
17
+ ]
18
+ }
19
+
20
+ # ------------------------------------------------------------------------
21
+ # Note: This method has support for "\char" escaping to prevent splits.
22
+ # Split a string into an array.
23
+ # ------------------------------------------------------------------------
24
+
25
+ def to_a(char: "\s", strip: true)
26
+ escaped = char.regexp_escape
27
+ split(/(?<!\\)#{escaped}/).map do |v|
28
+ strip ? v.gsub(/\\#{escaped}/, char) : v
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ class String
35
+ prepend Extras::String
36
+ end
@@ -0,0 +1,9 @@
1
+ # ----------------------------------------------------------------------------
2
+ # Frozen-string-literal: true
3
+ # Copyright: 2015-2016 Jordon Bedwell - MIT License
4
+ # Encoding: utf-8
5
+ # ----------------------------------------------------------------------------
6
+
7
+ module Extras
8
+ VERSION = "0.1.0"
9
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extras
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jordon Bedwell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: forwardable-extended
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.5'
27
+ description: Add some neat little extras into your Ruby stuff.
28
+ email:
29
+ - jordon@envygeeks.io
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - LICENSE
36
+ - Rakefile
37
+ - lib/extras/all.rb
38
+ - lib/extras/array.rb
39
+ - lib/extras/hash.rb
40
+ - lib/extras/shell.rb
41
+ - lib/extras/string.rb
42
+ - lib/extras/version.rb
43
+ homepage: http://github.com/envygeeks/extras
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.6.2
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Add neat extras into your Ruby stuff.
67
+ test_files: []