Module: TopLevel
- Included in:
- Gfunc
- Defined in:
- lib/gfunc/core.rb
Overview
directly in main to make TopLevel: extend TopLevel
Instance Method Summary collapse
- #append_each(array, right_side) ⇒ Object
-
#d_cont(str) ⇒ Object
rails destroy controller …, just like in shell.
-
#d_model(str) ⇒ Object
rails destroy model …, just like in shell.
-
#file_insert(file_path_str, s_or_a_ins = '', linenum = -1) ⇒ Object
‘file_insert` inserts contents of `s_or_a_ins` in `file_path_str` at `linenum`.
-
#file_to_a(file_path_str, delimiter = "\n") ⇒ Object
‘file_to_a` reads file at `file_path_str` and returns array of strings.
-
#g_cont(str) ⇒ Object
rails generate controller …, just like in shell.
-
#g_model(str) ⇒ Object
__ / Rails bash commands made available in Ruby .
-
#get_migration_id(partial_filename) ⇒ Object
finds migration file containing string provided by arg, returns id.
- #has_dot?(str, regex) ⇒ Boolean
- #has_lower?(str) ⇒ Boolean
- #has_regex?(str, regex) ⇒ Boolean
- #has_upper?(str) ⇒ Boolean
- #is_lower?(str) ⇒ Boolean
-
#is_upper?(str) ⇒ Boolean
These are all self-explanatory.
-
#locate_def(str) ⇒ Object
Location the definition of just about anything.
-
#ls_grep?(dir_str, grep_str, dir_parent_sym_or_s = "/") ⇒ Boolean
‘ls_grep?` helps you see if a directory `dir_str` contains a directory or file with a name containing a string provided 2nd arg `grep_str`, The 1st arg is assumed to be a full path unless you provide a 3rd arg.
-
#migration_status ⇒ Object
get migration status as string.
- #prepend_each(array, left_side) ⇒ Object
- #ror_file_exists?(str) ⇒ Boolean
-
#ror_path(str) ⇒ Object
appends string arg with Rails.root.
-
#run_migration_file(partial_filename, direction = :up) ⇒ Object
Runs migration ‘:up` or `:down` set by 2nd arg (defaulted to `:up`) for migration file specified by any part of it’s name as 1st arg (string).
-
#s_to_file(file_path_str, new_content_str = '') ⇒ Object
‘s_to_file` overwrites file at `file_path_str` with `new_content_str`.
- #starts_lower?(str) ⇒ Boolean
- #starts_upper?(str) ⇒ Boolean
-
#table_exists?(str) ⇒ Boolean
arg should be actual table name on db.
-
#tables ⇒ Object
get array of table names in less annoying syntax:.
- #wrap_each(array, left_side, right_side) ⇒ Object
Instance Method Details
#append_each(array, right_side) ⇒ Object
170 171 172 |
# File 'lib/gfunc/core.rb', line 170 def append_each array, right_side array.map { |elem| elem = "#{left_side}#{elem}#{right_side}" } end |
#d_cont(str) ⇒ Object
rails destroy controller …, just like in shell
119 120 121 122 |
# File 'lib/gfunc/core.rb', line 119 def d_cont str # rails destroy controller ..., just like in shell cmd = "rails destroy controller #{str}" puts %x[ #{cmd} ] end |
#d_model(str) ⇒ Object
rails destroy model …, just like in shell
109 110 111 112 |
# File 'lib/gfunc/core.rb', line 109 def d_model str # rails destroy model ..., just like in shell cmd = "rails destroy model #{str}" puts %x[ #{cmd} ] end |
#file_insert(file_path_str, s_or_a_ins = '', linenum = -1) ⇒ Object
‘file_insert` inserts contents of `s_or_a_ins` in `file_path_str` at `linenum`. When `file_path_str` will honor “n” and generate a newline. This way you can enter a large string with lines delimited by your “n”. Alternatively, you can use an array of strings, Each will be a new line. Note: `linenum` starts at 1, NOT 0 and can be negative (-1 is last line)
33 34 35 36 37 38 39 40 41 |
# File 'lib/gfunc/core.rb', line 33 def file_insert file_path_str, s_or_a_ins='', linenum=-1 FileUtils.touch(file_path_str) unless File.exist? file_path_str arr = File.read(file_path_str).lines(separator="\n") linenum-=1 if linenum > 0 s_or_a_ins.each { |e| e << "\n" } if s_or_a_ins.class == Array arr.insert linenum, s_or_a_ins string = arr.join File.open(file_path_str, "w+") { |f| f.write(string) } end |
#file_to_a(file_path_str, delimiter = "\n") ⇒ Object
‘file_to_a` reads file at `file_path_str` and returns array of strings. Each line will be a new element by default since `file_to_a` looks for “n” when breaking up the file into the array but you can override this with any double-quoted character as the optional 2nd arg
47 48 49 |
# File 'lib/gfunc/core.rb', line 47 def file_to_a file_path_str, delimiter="\n" File.read(file_path_str).lines(separator=delimiter) end |
#g_cont(str) ⇒ Object
rails generate controller …, just like in shell
114 115 116 117 |
# File 'lib/gfunc/core.rb', line 114 def g_cont str # rails generate controller ..., just like in shell cmd = "rails generate controller #{str}" puts %x[ #{cmd} ] end |
#g_model(str) ⇒ Object
__
/ Rails bash commands made available in Ruby \
104 105 106 107 |
# File 'lib/gfunc/core.rb', line 104 def g_model str # rails generate model ..., just like in shell cmd = "rails generate model #{str}" puts %x[ #{cmd} ] end |
#get_migration_id(partial_filename) ⇒ Object
finds migration file containing string provided by arg, returns id
128 129 130 131 |
# File 'lib/gfunc/core.rb', line 128 def get_migration_id partial_filename ls_migr_file = "ls #{Rails.root}/db/migrate | grep '#{partial_filename}'" (%x[ #{ls_migr_file} ]).strip.gsub!(/\D/, '') end |
#has_dot?(str, regex) ⇒ Boolean
160 |
# File 'lib/gfunc/core.rb', line 160 def has_dot? str, regex; !!(str =~ /\./); end |
#has_lower?(str) ⇒ Boolean
162 |
# File 'lib/gfunc/core.rb', line 162 def has_lower? str; !!(str =~ /[A-Z]/); end |
#has_regex?(str, regex) ⇒ Boolean
159 |
# File 'lib/gfunc/core.rb', line 159 def has_regex? str, regex; !!(str =~ regex); end |
#has_upper?(str) ⇒ Boolean
161 |
# File 'lib/gfunc/core.rb', line 161 def has_upper? str; !!(str =~ /[A-Z]/); end |
#is_lower?(str) ⇒ Boolean
158 |
# File 'lib/gfunc/core.rb', line 158 def is_lower? str; str == str.downcase; end |
#is_upper?(str) ⇒ Boolean
These are all self-explanatory. all return booleans
157 |
# File 'lib/gfunc/core.rb', line 157 def is_upper? str; str == str.upcase; end |
#locate_def(str) ⇒ Object
Location the definition of just about anything.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/gfunc/core.rb', line 76 def locate_def str has_dot = has_dot? str str_upr = starts_upper? str if has_dot && str_upr splits = str.partition(".") clas = splits.first meth = splits.last eval "clas.method(meth).source_location" elsif !has_dot && !str_upr Object.method(str).source_location elsif code = "#{str}.instance_methods(false).map {|m| #{str}" code << ".instance_method(m).source_location.first}.uniq" eval code end end |
#ls_grep?(dir_str, grep_str, dir_parent_sym_or_s = "/") ⇒ Boolean
‘ls_grep?` helps you see if a directory `dir_str` contains a directory or file with a name containing a string provided 2nd arg `grep_str`, The 1st arg is assumed to be a full path unless you provide a 3rd arg. The 3rd arg can be `:ror` for the root of the current Rails app, `:home` for the user’s home folder or a custom path as a string.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/gfunc/core.rb', line 64 def ls_grep? dir_str, grep_str, dir_parent_sym_or_s="/" dir_str.gsub /^\//, '' # remove starting '/' if found case parent_path when :ror then parent = Rails.root when :home then parent = Dir.home when String then parent = dir_parent_sym_or_s end ls_cmd = "ls #{Rails.root}/#{dir_str} | grep '#{grep_str}'" (%x[ #{ls_cmd} ]) != '' end |
#migration_status ⇒ Object
get migration status as string
141 142 143 144 |
# File 'lib/gfunc/core.rb', line 141 def migration_status stat = "rake db:migrate:status" %x[ #{stat} ] end |
#prepend_each(array, left_side) ⇒ Object
166 167 168 |
# File 'lib/gfunc/core.rb', line 166 def prepend_each array, left_side array.map { |elem| elem = "#{left_side}#{elem}" } end |
#ror_file_exists?(str) ⇒ Boolean
97 98 99 |
# File 'lib/gfunc/core.rb', line 97 def ror_file_exists? str File.exist? "#{Rails.root}#{str}" end |
#ror_path(str) ⇒ Object
appends string arg with Rails.root
93 94 95 |
# File 'lib/gfunc/core.rb', line 93 def ror_path str # appends string arg with Rails.root "#{Rails.root}#{str}" end |
#run_migration_file(partial_filename, direction = :up) ⇒ Object
Runs migration ‘:up` or `:down` set by 2nd arg (defaulted to `:up`) for migration file specified by any part of it’s name as 1st arg (string).
135 136 137 138 |
# File 'lib/gfunc/core.rb', line 135 def run_migration_file partial_filename, direction=:up id = get_migration_id partial_filename "rake db:migrate:#{direction.to_s} VERSION=#{id}" end |
#s_to_file(file_path_str, new_content_str = '') ⇒ Object
‘s_to_file` overwrites file at `file_path_str` with `new_content_str`
52 53 54 |
# File 'lib/gfunc/core.rb', line 52 def s_to_file file_path_str, new_content_str='' File.open(file_path_str, "w+") { |f| f.write(new_content_str) } end |
#starts_lower?(str) ⇒ Boolean
164 |
# File 'lib/gfunc/core.rb', line 164 def starts_lower? str; !!(str.first =~ /[A-Z]/); end |
#starts_upper?(str) ⇒ Boolean
163 |
# File 'lib/gfunc/core.rb', line 163 def starts_upper? str; !!(str.first =~ /[A-Z]/); end |
#table_exists?(str) ⇒ Boolean
arg should be actual table name on db
149 150 151 |
# File 'lib/gfunc/core.rb', line 149 def table_exists? str # arg should be actual table name on db ActiveRecord::Base.connection.tables.include? str end |
#tables ⇒ Object
get array of table names in less annoying syntax:
147 |
# File 'lib/gfunc/core.rb', line 147 def tables; ActiveRecord::Base.connection.tables; end |
#wrap_each(array, left_side, right_side) ⇒ Object
174 175 176 |
# File 'lib/gfunc/core.rb', line 174 def wrap_each array, left_side, right_side array.map { |elem| elem = "#{left_side}#{elem}#{right_side}" } end |