Module: Simrb
- Defined in:
- lib/simrb/comd.rb,
lib/simrb/base.rb,
lib/simrb/info.rb,
lib/simrb/tool_start.rb
Overview
this file stores the base info of this project of the software
Defined Under Namespace
Modules: Stool Classes: Scommand
Constant Summary collapse
- Info =
{ :name => 'simrb', :created => '2014-01-01', :alias_name => '3s', :version => '1.0.7', :author => 'Linyu Deng', :email => '[email protected]', :homepage => 'https://github.com/simrb/help-gem', :license => 'MIT', :description => 'This is a command helper for simrb', :summary => 'A utility of simrb', }
Class Method Summary collapse
- .addslash(path) ⇒ Object
- .help(args = []) ⇒ Object
-
.input_format(args = []) ⇒ Object
format the input argument from an array to two item, first item is orgin array, last is an hash option.
-
.module_load ⇒ Object
return an hash that stores the modules that consists of local directory and repository.
- .p(args, action = nil) ⇒ Object
-
.path_write(path, content = "", mode = "a+") ⇒ Object
initialize a path whatever it is a dir or file.
- .random(size = 12) ⇒ Object
- .root_dir_force ⇒ Object
- .yaml_read(path) ⇒ Object
- .yaml_write(path, data) ⇒ Object
Class Method Details
.addslash(path) ⇒ Object
225 226 227 |
# File 'lib/simrb/base.rb', line 225 def addslash path path[-1] == '/' ? path : "#{path}/" end |
.help(args = []) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/simrb/base.rb', line 201 def help args = [] res = [] i = 0 docs_key = {} docs_val = {} Sdocs.each do | key, val | docs_key[i] = key docs_val[i] = val i = i + 1 end if args.empty? res << 'Please select the number before the list to see detials' docs_key.each do | i, key | res << "#{i.to_s}, #{key}" end else args.each do | i | res << (docs_val.include?(i.to_i) ? docs_val[i.to_i] : 'no document') end end Simrb.p res end |
.input_format(args = []) ⇒ Object
format the input argument from an array to two item, first item is orgin array, last is an hash option
Example
args, opts = Simrb.input_format [“test”, “test2”, “–test”, “–name=test2”, “-n=test3”]
the above is same as
args, opts = Simrb.input_format [“–test”, “test”, “test2”, “–name=test2”, “-n=test3”] the options that starts with “-” you can write any positions of argument
output
args = [“test”, “test2”] opts = true, name: test2, n:test3
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/simrb/base.rb', line 178 def input_format args = [] resa = [] # return an array resh = {} # return an hash unless args.empty? args.each do | item | if item[0] == "-" new_item = item.split("-").uniq.last if new_item.index "=" key, val = new_item.split "=" resh[key.to_sym] = val else resh[new_item.to_sym] = true end else resa << item end end end [resa, resh] end |
.module_load ⇒ Object
return an hash that stores the modules that consists of local directory and repository
Example
Simrb.module_load
assuming the :requrie_module option of Scfg file that is set as ‘system’, and two modules demo, demo2 in your local directory of project, so, the result as below
{ ‘system’ => ‘/your_repo_dir/system’, ‘demo’ => ‘/your_project_dir/modules/demo’, ‘demo2’ => ‘/your_project_dir/modules/demo2’, }
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/simrb/base.rb', line 67 def module_load module_dirs = {} module_ds = {} # load modules from local repository Spath[:repo_dirs].map{|m| m + '*'}.each do | dir | Dir[dir].each do | path | name = path.split("/").last module_dirs[name] = File.(path) if Scfg[:module_require].include? name end end # load modules from project directory Dir["#{Spath[:module]}*"].each do | path | name = path.split("/").last module_dirs[name] = File.("#{Spath[:module]}#{name}") unless Scfg[:module_disable].include? name end # load the info of module module_dirs.each do | name, path | path = "#{path}#{Spath[:modinfo]}" res = Simrb.yaml_read path if res[0] and res[0]["name"] and res[0]["name"] == name module_ds[name] = (res[0]["order"] || 99) else Simrb.p "Loading error of module info, please check #{path}", :exit end end # sort module by order field of module res = {} module_ds = module_ds.sort_by { |k, v| v } module_ds.each do | name, order | res[name] = module_dirs[name] end res end |
.p(args, action = nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/simrb/base.rb', line 28 def p args, action = nil res = "" if args.class.to_s == 'Array' res = args.join("\n") elsif args.class.to_s == 'Hash' args.each do | k, v | if action == :write res << "#{k.to_s.ljust(7)} -- #{v}\n" else res << "#{k.to_s.ljust(15)} => #{v}\n" end end res = res.chomp "\n" else res = args.to_s end # puts "="*30 + "\n" + res + "\n" + "="*30 puts res exit if action == :exit end |
.path_write(path, content = "", mode = "a+") ⇒ Object
initialize a path whatever it is a dir or file
Example
new a file, or with the additional content
path_write “myfile” path_write “myfile”, “file content” path_write “myfile”, “file content”, “w+”
create the dir
path_write “newdir/” path_write “newdir/newdir2/”
create the dir and file at the same time
path_write “newdir/newdir2/myfile”
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/simrb/base.rb', line 124 def path_write path, content = "", mode = "a+" if File.exist?(path) File.open(path, mode){|f| f.write content} unless path[-1] == '/' else arrs = path.split('/') count = arrs.count - 1 prve = "" if arrs[0] == "" arrs.delete("") prve = "/" end (0..count).each do | i | sub_path = prve + arrs[0..i].join("/") unless File.exist? sub_path sub_path == path ? File.open(path, mode){|f| f.write content} : Dir.mkdir(sub_path) end end end end |
.random(size = 12) ⇒ Object
229 230 231 232 |
# File 'lib/simrb/base.rb', line 229 def random size = 12 charset = ('a'..'z').to_a + ('0'..'9').to_a + ('A'..'Z').to_a (0...size).map{charset.to_a[rand(charset.size)]}.join end |
.root_dir_force ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/simrb/base.rb', line 146 def root_dir_force # check the required module is that existing Scfg[:module_require].each do | name | unless Smods.keys.include? name puts "Warning: the required module #{name} is not existing, wherever in local dir or repository" end end # check the file that is necessary to be loaded unless File.exist?(Sroot + Scfg[:name]) Simrb.p "Current command only allow to be used under root directory of project", :exit end end |
.yaml_read(path) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/simrb/base.rb', line 14 def yaml_read path require 'yaml' YAML.load_file path rescue [] end |
.yaml_write(path, data) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/simrb/base.rb', line 21 def yaml_write path, data require "yaml" File.open(path, 'w+') do | f | f.write data.to_yaml end end |