gfunc 0.0.1

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
+ SHA1:
3
+ metadata.gz: 41abed3f16168da10e1cff6a5a742c966d148151
4
+ data.tar.gz: caec5a9be63b308114f47627ae50709dd709b882
5
+ SHA512:
6
+ metadata.gz: 73e1d9f975f84c7c7dbf31772ff971d316a46e4944b3f930571217061a78648a819dad69441876e462445ddfef8b2a0343a396e6be6f72755ee5438998b9efdc
7
+ data.tar.gz: f2f7a74e9b5b151d80e5c3a77775fc3f96c91d9eb2a31d4e99a6e11530b5c9e9857894bed63d752f02f24324888735deffa77f2304c676f05b76fafa0a7a6e1d
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
data/bin/gfunc ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'gfunc'
data/lib/gfunc.rb ADDED
@@ -0,0 +1,13 @@
1
+
2
+ require_relative './gfunc/core.rb'
3
+
4
+ =begin working document the user can put...
5
+
6
+ require 'gfunc' # normal way to gain access to gem
7
+
8
+ or...
9
+
10
+ require 'gfunc/core' # version that requires scoping like:
11
+ Gfunc.method 'arg' # The scoping would look like this
12
+
13
+ =end
data/lib/gfunc/core.rb ADDED
@@ -0,0 +1,197 @@
1
+
2
+ ######--------------------------------------------######
3
+ ###### By Jeff Russ https://github.com/Jeff-Russ ######
4
+ ######--------------------------------------------######
5
+
6
+ $gfunc_info = <<-msg
7
+ ------------------------------------------------------
8
+ Gfunc is a collection of utilities by Jeff-Russ
9
+
10
+ provided as top-level methods: require 'gfunc'
11
+ or as Gfunc class methods: require 'gfunc/core'
12
+
13
+ With the first way, methods are called directly, like
14
+ functions. The second way requires: Gfunc.methodname
15
+
16
+ Display all methods in irb: Gfunc.puts_methods
17
+ Display (heavily commented) code: Gfunc.source
18
+ Display this message again: puts $gfunc_info
19
+
20
+ More info: https://github.com/Jeff-Russ/gfunc-gem
21
+ ------------------------------------------------------
22
+ msg
23
+
24
+ module TopLevel # directly in main to make TopLevel: extend TopLevel
25
+ # __________________________________________________________________
26
+ # / File i/o methods \
27
+
28
+ # `file_insert` inserts contents of `s_or_a_ins` in `file_path_str` at
29
+ # `linenum`. When `file_path_str` will honor "\n" and generate a newline.
30
+ # This way you can enter a large string with lines delimited by your "\n".
31
+ # Alternatively, you can use an array of strings, Each will be a new line.
32
+ # Note: `linenum` starts at 1, NOT 0 and can be negative (-1 is last line)
33
+ def file_insert file_path_str, s_or_a_ins='', linenum=-1
34
+ FileUtils.touch(file_path_str) unless File.exist? file_path_str
35
+ arr = File.read(file_path_str).lines(separator="\n")
36
+ linenum-=1 if linenum > 0
37
+ s_or_a_ins.each { |e| e << "\n" } if s_or_a_ins.class == Array
38
+ arr.insert linenum, s_or_a_ins
39
+ string = arr.join
40
+ File.open(file_path_str, "w+") { |f| f.write(string) }
41
+ end
42
+
43
+ # `file_to_a` reads file at `file_path_str` and returns array of strings.
44
+ # Each line will be a new element by default since `file_to_a`
45
+ # looks for "\n" when breaking up the file into the array but you can
46
+ # override this with any double-quoted character as the optional 2nd arg
47
+ def file_to_a file_path_str, delimiter="\n"
48
+ File.read(file_path_str).lines(separator=delimiter)
49
+ end
50
+
51
+ # `s_to_file` overwrites file at `file_path_str` with `new_content_str`
52
+ def s_to_file file_path_str, new_content_str=''
53
+ File.open(file_path_str, "w+") { |f| f.write(new_content_str) }
54
+ end
55
+
56
+ # ___________________________________________________________________
57
+ # / Other file related methods \
58
+
59
+ # `ls_grep?` helps you see if a directory `dir_str` contains a directory
60
+ # or file with a name containing a string provided 2nd arg `grep_str`,
61
+ # The 1st arg is assumed to be a full path unless you provide a 3rd arg.
62
+ # The 3rd arg can be `:ror` for the root of the current Rails app,
63
+ # `:home` for the user's home folder or a custom path as a string.
64
+ def ls_grep? dir_str, grep_str, dir_parent_sym_or_s="/"
65
+ dir_str.gsub /^\//, '' # remove starting '/' if found
66
+ case parent_path
67
+ when :ror then parent = Rails.root
68
+ when :home then parent = Dir.home
69
+ when String then parent = dir_parent_sym_or_s
70
+ end
71
+ ls_cmd = "ls #{Rails.root}/#{dir_str} | grep '#{grep_str}'"
72
+ (%x[ #{ls_cmd} ]) != ''
73
+ end
74
+
75
+ # Location the definition of just about anything.
76
+ def locate_def str
77
+ has_dot = has_dot? str
78
+ str_upr = starts_upper? str
79
+ if has_dot && str_upr
80
+ splits = str.partition(".")
81
+ clas = splits.first
82
+ meth = splits.last
83
+ eval "clas.method(meth).source_location"
84
+ elsif !has_dot && !str_upr
85
+ Object.method(str).source_location
86
+ elsif
87
+ code = "#{str}.instance_methods(false).map {|m| #{str}"
88
+ code << ".instance_method(m).source_location.first}.uniq"
89
+ eval code
90
+ end
91
+ end
92
+
93
+ def ror_path str # appends string arg with Rails.root
94
+ "#{Rails.root}#{str}"
95
+ end
96
+
97
+ def ror_file_exists? str
98
+ File.exist? "#{Rails.root}#{str}"
99
+ end
100
+
101
+ # __________________________________________________________________
102
+ # / Rails bash commands made available in Ruby \
103
+
104
+ def g_model str # rails generate model ..., just like in shell
105
+ cmd = "rails generate model #{str}"
106
+ puts %x[ #{cmd} ]
107
+ end
108
+
109
+ def d_model str # rails destroy model ..., just like in shell
110
+ cmd = "rails destroy model #{str}"
111
+ puts %x[ #{cmd} ]
112
+ end
113
+
114
+ def g_cont str # rails generate controller ..., just like in shell
115
+ cmd = "rails generate controller #{str}"
116
+ puts %x[ #{cmd} ]
117
+ end
118
+
119
+ def d_cont str # rails destroy controller ..., just like in shell
120
+ cmd = "rails destroy controller #{str}"
121
+ puts %x[ #{cmd} ]
122
+ end
123
+
124
+ # __________________________________________________________________
125
+ # / Rails ActiveRecord shortcuts \
126
+
127
+ # finds migration file containing string provided by arg, returns id
128
+ def get_migration_id partial_filename
129
+ ls_migr_file = "ls #{Rails.root}/db/migrate | grep '#{partial_filename}'"
130
+ (%x[ #{ls_migr_file} ]).strip.gsub!(/\D/, '')
131
+ end
132
+
133
+ # Runs migration `:up` or `:down` set by 2nd arg (defaulted to `:up`)
134
+ # for migration file specified by any part of it's name as 1st arg (string).
135
+ def run_migration_file partial_filename, direction=:up
136
+ id = get_migration_id partial_filename
137
+ "rake db:migrate:#{direction.to_s} VERSION=#{id}"
138
+ end
139
+
140
+ # get migration status as string
141
+ def migration_status
142
+ stat = "rake db:migrate:status"
143
+ %x[ #{stat} ]
144
+ end
145
+
146
+ # get array of table names in less annoying syntax:
147
+ def tables; ActiveRecord::Base.connection.tables; end
148
+
149
+ def table_exists? str # arg should be actual table name on db
150
+ ActiveRecord::Base.connection.tables.include? str
151
+ end
152
+
153
+ # _____________________________________________________________
154
+ # / String Manipulation and Evaluation \
155
+
156
+ # These are all self-explanatory. all return booleans
157
+ def is_upper? str; str == str.upcase; end
158
+ def is_lower? str; str == str.downcase; end
159
+ def has_regex? str, regex; !!(str =~ regex); end
160
+ def has_dot? str, regex; !!(str =~ /\./); end
161
+ def has_upper? str; !!(str =~ /[A-Z]/); end
162
+ def has_lower? str; !!(str =~ /[A-Z]/); end
163
+ def starts_upper? str; !!(str.first =~ /[A-Z]/); end
164
+ def starts_lower? str; !!(str.first =~ /[A-Z]/); end
165
+
166
+ def prepend_each array, left_side
167
+ array.map { |elem| elem = "#{left_side}#{elem}" }
168
+ end
169
+
170
+ def append_each array, right_side
171
+ array.map { |elem| elem = "#{left_side}#{elem}#{right_side}" }
172
+ end
173
+
174
+ def wrap_each array, left_side, right_side
175
+ array.map { |elem| elem = "#{left_side}#{elem}#{right_side}" }
176
+ end
177
+ end
178
+
179
+ # An optional wrapper around TopLevel's methods
180
+ # to make them namespaced and Class methods
181
+ module Gfunc
182
+ class << self # DO NOT change to module!
183
+ include TopLevel
184
+ end
185
+ def self.source return_string=false
186
+ unless return_string
187
+ puts File.read(__FILE__)
188
+ else return File.read(__FILE__)
189
+ end
190
+ end
191
+ def self.puts_methods
192
+ puts TopLevel.instance_methods
193
+ end
194
+
195
+ end
196
+
197
+
@@ -0,0 +1,16 @@
1
+ require 'test/unit'
2
+ require 'debug'
3
+
4
+ class GfuncTest < Test::Unit::TestCase
5
+ def test_english_hello
6
+ assert_equal "hello world", Gfunc.hi("english")
7
+ end
8
+
9
+ def test_any_hello
10
+ assert_equal "hello world", Gfunc.hi("ruby")
11
+ end
12
+
13
+ def test_spanish_hello
14
+ assert_equal "hola mundo", Gfunc.hi("spanish")
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gfunc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Russ
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A collection of utilities optionally provided as top-level methods
14
+ email: jeffreylynnruss@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - Rakefile
20
+ - bin/gfunc
21
+ - lib/gfunc.rb
22
+ - lib/gfunc/core.rb
23
+ - test/test_gfunc.rb
24
+ homepage: https://github.com/Jeff-Russ/gfunc-gem
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message: "------------------------------------------------------\nGfunc
29
+ is a collection of utilities by Jeff-Russ\n\nprovided as top-level methods: require
30
+ 'gfunc'\nor as Gfunc class methods: require 'gfunc/core'\n\nWith the first
31
+ way, methods are called directly, like \nfunctions. The second way requires: Gfunc.methodname\n\nDisplay
32
+ all methods in irb: Gfunc.puts_methods\nDisplay (heavily commented) code:
33
+ Gfunc.source\nDisplay this message again: puts $gfunc_info\n\nMore info: https://github.com/Jeff-Russ/gfunc-gem\n------------------------------------------------------\n"
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.4.5.1
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: gfunc
53
+ test_files:
54
+ - test/test_gfunc.rb