ro_support 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +21 -0
  5. data/lib/ro_support/array.rb +32 -0
  6. data/lib/ro_support/bash.rb +27 -0
  7. data/lib/ro_support/debug.rb +80 -0
  8. data/lib/ro_support/file_actions.rb +40 -0
  9. data/lib/ro_support/git.rb +64 -0
  10. data/lib/ro_support/log.rb +15 -0
  11. data/lib/ro_support/misc/zw.rb +46 -0
  12. data/lib/ro_support/misc.rb +51 -0
  13. data/lib/ro_support/module.rb +10 -0
  14. data/lib/ro_support/spec.rb +46 -0
  15. data/lib/ro_support/ssh.rb +4 -0
  16. data/lib/ro_support/string_handler/common.rb +40 -0
  17. data/lib/ro_support/string_handler.rb +13 -0
  18. data/lib/ro_support/thor.rb +10 -0
  19. data/lib/ro_support/version.rb +3 -0
  20. data/lib/ro_support.rb +23 -0
  21. data/lib/tasks/roro_support_tasks.rake +4 -0
  22. data/spec/fixtures/baidu.html +440 -0
  23. data/spec/fixtures/for_log/ro.log +2 -0
  24. data/spec/fixtures/for_misc_file_utils/create_dir_file/try.rb +0 -0
  25. data/spec/fixtures/sample/try1.rb +3 -0
  26. data/spec/fixtures/sample/try2.rb +3 -0
  27. data/spec/fixtures/sample/try3.rb +3 -0
  28. data/spec/lib/roro_support/array_spec.rb +27 -0
  29. data/spec/lib/roro_support/bash_spec.rb +4 -0
  30. data/spec/lib/roro_support/debug_spec.rb +42 -0
  31. data/spec/lib/roro_support/file_actions_spec.rb +10 -0
  32. data/spec/lib/roro_support/git_spec.rb +23 -0
  33. data/spec/lib/roro_support/log/ro.log +23 -0
  34. data/spec/lib/roro_support/log/roro.log +63 -0
  35. data/spec/lib/roro_support/log_spec.rb +15 -0
  36. data/spec/lib/roro_support/misc/zw_spec.rb +55 -0
  37. data/spec/lib/roro_support/misc_spec.rb +37 -0
  38. data/spec/lib/roro_support/module_spec.rb +15 -0
  39. data/spec/lib/roro_support/string_handler_spec.rb +17 -0
  40. data/spec/lib/roro_support_spec.rb +7 -0
  41. data/spec/spec_helper.rb +29 -0
  42. metadata +201 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ccdfe67cf1f839c8b160b26ecdf42c8a7691c80
4
+ data.tar.gz: 2a9fa9bf87372a4cea22c55da0d835d13641041d
5
+ SHA512:
6
+ metadata.gz: 5907695c8501a6581d4a801b9e465d553865584164caa1bb71fdd4dd89c60d40bfd900154c9ecf60743534f9dbcdfffdeffeb46aadd69d5e054f8a8240e6ac28
7
+ data.tar.gz: 56f7547893d0ffaa353719a3a08bc9a23191165d7c415f43793e52f531dc121ef00894f7446bd0a2923bd4fc5bd83be59229f124b0edc541de4e1883f00bc382
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = RoroSupport
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RoroSupport'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+ Bundler::GemHelper.install_tasks
17
+
18
+ require 'rspec/core/rake_task'
19
+ RSpec::Core::RakeTask.new
20
+
21
+ task default: :spec
@@ -0,0 +1,32 @@
1
+ module RoSupport
2
+ module Array
3
+ Array.class_eval do
4
+ str = ""
5
+
6
+ def to_args
7
+ str = ""
8
+ self.each do |arg|
9
+ if arg.is_a? String
10
+ arg = preprocess arg
11
+ str << "'#{arg}',"
12
+ else
13
+ str << "#{arg},"
14
+ end
15
+ end
16
+ str.gsub /\,$/, ''
17
+ end
18
+
19
+ def map_with_regexp(regexp)
20
+ arr = self.dup
21
+ arr.each_with_index do |e, idx|
22
+ arr[idx] = yield(e) if e.to_s[regexp]
23
+ end
24
+ end
25
+
26
+ private
27
+ def preprocess(str)
28
+ str.gsub /\'|\"/, ''
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ module RoSupport
2
+ module Bash
3
+ def bash(command)
4
+ result = `#{command}`
5
+ print result
6
+ result
7
+ end
8
+
9
+ def bashes(commands=[])
10
+ if !commands.empty?
11
+ bash(commands.join " && ")
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ #def set(aliases)
17
+ # aliases.each do |short, command|
18
+ # blk = lambda do |*args|
19
+ # eval "`#{command} #{args.join ' '}`"
20
+ # end
21
+ # desc "#{short} *ARGS", "#{command} *ARGS"
22
+ # define_method short, blk
23
+ # end
24
+ #end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,80 @@
1
+ module RoSupport
2
+ module Debug
3
+ # ro is my name, ro_raise mean my raise
4
+ def self.included(base)
5
+ base.send(:alias_method, :ro_raise, :eval)
6
+ base.send(:alias_method, :ro_return, :eval)
7
+ end
8
+
9
+ def err(msg='error', opt={output: []})
10
+ err = [""]
11
+ err << msg
12
+ err << "Instance Variables:"
13
+ instance_variables.each do |ivar|
14
+ err << " #{ivar.to_s}: #{instance_variable_get(ivar)}"
15
+ end
16
+
17
+ print err.join("\n")
18
+
19
+ <<-ERROR
20
+ err = [""]
21
+
22
+ err << ""
23
+ err << "Output Variables:"
24
+ err << ""
25
+ #{opt[:output]}.each do |var|
26
+ if var.is_a?(String)
27
+ err << " \#{var}:\#{eval var}"
28
+ end
29
+ end
30
+
31
+ raise err.join("\n") + "\n\n"
32
+ ERROR
33
+ end
34
+
35
+ def valid(smth, opt={})
36
+ if opt[:valid?].nil?
37
+ if smth.is_a?(Array) or smth.is_a?(Hash)
38
+ is_valid = !smth.empty?
39
+ else
40
+ is_valid = !smth.nil?
41
+ end
42
+
43
+ if is_valid
44
+ return smth
45
+ else
46
+ if opt[:output]
47
+ err('return nil or empty', output: opt[:output])
48
+ else
49
+ err('return nil or empty')
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ # eg: ro_raise ro_error
56
+ #def ro_error(err)
57
+ # <<-RUBY
58
+ # lvars = {}
59
+ # local_variables.each do |local_variable|
60
+ # lvars[local_variable] = eval(local_variable.to_s)
61
+ # end
62
+ #
63
+ # raise "#{err}", lvars: lvars
64
+ # RUBY
65
+ #
66
+ #end
67
+ #
68
+ #def ro_return_valid(smth)
69
+ # <<-RUBY
70
+ # lvars = {}
71
+ # local_variables.each do |local_variable|
72
+ # lvars[local_variable] = eval(local_variable.to_s)
73
+ # smth_var_name = local_variable.to_s if eval(\"local_variable.to_s\") == #{smth}
74
+ # end
75
+ #
76
+ # return_valid smth_var_name, lvars: lvars
77
+ # RUBY
78
+ #end
79
+ end
80
+ end
@@ -0,0 +1,40 @@
1
+ require 'fileutils'
2
+ module RoSupport
3
+ module FileActions
4
+ def create_dir_file(path)
5
+ file = File.basename path
6
+ dir = path.gsub file, ''
7
+ FileUtils.mkdir_p(dir)
8
+ File.new(path, 'w+')
9
+ end
10
+
11
+ def get_tree_in(dir)
12
+
13
+ end
14
+
15
+ #Attentions: module_name must be like file_name
16
+ # eg: module Array, array.rb is right
17
+ # eg: moduel ArrayDSL, array.rb is wrong
18
+ # The File tree is like this:
19
+ #a
20
+ #-b.rb
21
+ #-c.rb
22
+ #a.rb
23
+
24
+ def self.included(base)
25
+ base.extend ClassMethods
26
+ end
27
+
28
+ module ClassMethods
29
+ def autoload_all_files_in(dir)
30
+ Dir[File.join dir, "**"].each do |file|
31
+ if file[/\.rb$/]
32
+ module_name = File.basename(file).gsub(/\.rb/, '').camelize
33
+ autoload module_name.to_sym, file
34
+ autoload module_name.upcase, file
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,64 @@
1
+ # deprecated
2
+ #require 'grit'
3
+ #
4
+ #module RoSupport
5
+ # module Git
6
+
7
+
8
+ #def git(bash, repo_path=Dir.pwd)
9
+ # repo = Grit::Repo.new repo_path
10
+ # ::Grit::Git.git_timeout = 30
11
+ # args = handle(bash)
12
+ # command = args.shift
13
+ # result = eval(bash_format(command, args))
14
+ # print result.join if result.respond_to?(:join)
15
+ #end
16
+ #
17
+ #def git_commit_all(msg)
18
+ # repo = Grit::Repo.new Dir.pwd
19
+ # repo.commit_all(msg)
20
+ #end
21
+ #
22
+ #private
23
+ #def bash_format(command, args)
24
+ # git_str = "repo.git.native '#{command}', {process_info: true}"
25
+ # if args.respond_to?(:each)
26
+ # args.each do |arg|
27
+ # git_str += ", '#{arg}'"
28
+ # end
29
+ # else
30
+ # arg = args
31
+ # git_str += ", '#{arg}'"
32
+ # end
33
+ #
34
+ # git_str
35
+ #end
36
+ #
37
+ #def handle(bash)
38
+ # start_idx, end_idx = nil
39
+ # args = bash.split(' ')
40
+ # args.each_with_index do |e, idx|
41
+ # if e[/'|"/]
42
+ # if start_idx.nil?
43
+ # start_idx = idx
44
+ # else
45
+ # end_idx = idx
46
+ # break
47
+ # end
48
+ # end
49
+ # end
50
+ #
51
+ # if start_idx && end_idx && start_idx != end_idx
52
+ # msg = args[start_idx..end_idx]
53
+ # front = args - msg
54
+ # msg = msg.join(' ')
55
+ # args = front + [msg]
56
+ # end
57
+ #
58
+ # args
59
+ #end
60
+ #
61
+ #def add_arg(git_str, arg)
62
+ #end
63
+ # end
64
+ #end
@@ -0,0 +1,15 @@
1
+ require 'logger'
2
+ module RoSupport
3
+ module Log
4
+ def raise_log(content, log_file='ro.log')
5
+ raise err=content if content.is_a?(Exception)
6
+ if defined?(Rails) && defined?(Rails.root)
7
+ log = Logger.new("#{Rails.root}/log/#{log_file}")
8
+ log.error(content)
9
+ else
10
+ log = Logger.new(File.join Dir.pwd, log_file)
11
+ log.error(content)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ require 'ruby-pinyin'
2
+ module RoSupport
3
+ module Misc
4
+ module Zw
5
+ ::String.class_eval do
6
+ def is_fuhao?
7
+ PinYin.of_string(self).empty?
8
+ end
9
+
10
+ def is_hanzi?
11
+ !PinYin.of_string(self).empty?
12
+ end
13
+
14
+ # example:
15
+ # "1.".is_nox # => true
16
+ # "2逼".is_nox # => false
17
+ def is_no?(x=/\d/)
18
+ str = self[Regexp.new "#{x}."]
19
+ if str && !str.empty?
20
+ mark = str.split('')[-1]
21
+ # is english sign
22
+ mark.is_fuhao? or mark.bytes == 1 ? true : false
23
+ else
24
+ false
25
+ end
26
+ end
27
+
28
+ def has_no?(x=/\d/)
29
+ self.scan(/\d./).each do |no|
30
+ return true if no.is_no? x
31
+ end
32
+ end
33
+
34
+ def split_with_no(x=/\d/)
35
+ nos= self.scan Regexp.new "^#{x}."
36
+ nos = nos.map do |no|
37
+ no if no.is_no?
38
+ end
39
+
40
+ r = eval "Regexp.union /#{nos.compact.join("/,/")}/"
41
+ self.split Regexp.new "(?=^#{r})"
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,51 @@
1
+ require 'active_support/core_ext/string'
2
+ dirname =__FILE__.gsub /\.rb/, ''
3
+
4
+ Dir[File.join dirname, "**"].each do |file|
5
+ require file
6
+ end
7
+
8
+ module RoSupport
9
+ module Misc
10
+ #def set_instance_variable_from(klass, hash = {})
11
+ # hash.each do |var_name, value|
12
+ # var_sym = "@#{var_name}".to_sym
13
+ # klass.instance_variable_set(var_sym, value)
14
+ # end
15
+ #end
16
+ end
17
+
18
+ #module Require
19
+ # attr_accessor :files
20
+ # list all files in dir
21
+
22
+ #def that(dir)
23
+ # dir_load dir
24
+ # self
25
+ #end
26
+
27
+ #def self.all_files_in(dir_path)
28
+ # dirname = dir_path.split('/').last.gsub(/\.rb/, '')
29
+ # Dir[File.expand_path("../#{dirname}/**", dir_path)].each do |file|
30
+ # require file
31
+ # end
32
+ #end
33
+
34
+ private
35
+
36
+ #def dir_load (dir_name)
37
+ # $LOAD_PATH << dir_name
38
+ # Dir["#{dir_name}/**"].each do |file|
39
+ # if Dir["#{file}/**"].length >= 1
40
+ # dirname = file
41
+ # $LOAD_PATH << dirname
42
+ # dir_load dirname
43
+ # else
44
+ # filename = File.basename(file).gsub /\..+/, ''
45
+ # @files[filename.to_sym] = file
46
+ # end
47
+ # end
48
+ #end
49
+ #end
50
+ #end
51
+ end
@@ -0,0 +1,10 @@
1
+ module RoSupport
2
+ module Module
3
+ Module.class_eval do
4
+ def has_module?(sym)
5
+ result = eval("defined? #{name}::#{sym.to_s}")
6
+ return (result.empty?) ? false : true
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,46 @@
1
+ require 'rspec/expectations'
2
+ require 'active_support'
3
+ require File.expand_path('../watir', __FILE__)
4
+
5
+ module RoSupoort
6
+ module RSpec
7
+ include Watir
8
+ extend ::RSpec::Matchers::DSL
9
+
10
+ matcher :have_css do |e|
11
+ match do |a|
12
+ a.css(e)
13
+ end
14
+ end
15
+
16
+ matcher :exist_not_nil do |*es|
17
+ match do |a|
18
+ if es.is_a?(Array)
19
+
20
+ es.each do |e|
21
+ e = ("@" + e.to_s).to_sym
22
+ a.instance_variable_get(e)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ matcher :be_blank do |e|
29
+ match do |a|
30
+ a.blank?
31
+ end
32
+ end
33
+
34
+ matcher :be_diff do |e|
35
+ match do |a|
36
+ a != e
37
+ end
38
+ end
39
+
40
+ matcher :be_empty do |e|
41
+ match do |a|
42
+ a.empty?
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,4 @@
1
+ module RoSupport
2
+ module SSH
3
+ end
4
+ end
@@ -0,0 +1,40 @@
1
+ module RoSupport
2
+ module StringHandler
3
+ module Common
4
+ def blank?
5
+ result = self.gsub /\s/, ''
6
+ result.empty?
7
+ end
8
+
9
+ def uncamelize
10
+ self.split(/(?=[A-Z])/).join("_").downcase
11
+ end
12
+
13
+ def collect(str, sign, filter = nil, &blk)
14
+ new_str_arr = collect_sign(str, sign, filter, &blk)
15
+
16
+ new_str = new_str_arr.join
17
+ end
18
+
19
+ private
20
+ def collect_sign(str, sign, filter = nil, &blk)
21
+ raise 'sign must be a regexp' if !sign.is_a?(Regexp)
22
+ sign = Regexp.new "(?=#{sign.source})"
23
+ filter ||= sign
24
+ new_str_arr = []
25
+
26
+ str.split(sign).each do |line|
27
+ if line[filter]
28
+ if block_given?
29
+ line = yield(line)
30
+ end
31
+
32
+ new_str_arr << line if line
33
+ end
34
+ end
35
+
36
+ new_str_arr
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,13 @@
1
+ require 'active_support/core_ext/string'
2
+
3
+ dirname = __FILE__.gsub /\.rb/, ''
4
+
5
+ Dir[File.join dirname, "**"].each do |file|
6
+ require file
7
+ end
8
+
9
+ module RoSupport
10
+ module StringHandler
11
+ include Common
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module RoSupport
2
+ module Thor
3
+ extend ActiveSupport::Concern
4
+ included do
5
+ ::Thor.send(:defind_method, :banner) do |command, namespace = nil, subcommand = false|
6
+ "#{command.formatted_usage(self, $thor_runner, subcommand)}"
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module RoSupport
2
+ VERSION = "0.0.4"
3
+ end
data/lib/ro_support.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'active_support/core_ext/string'
2
+ require 'thor'
3
+ require 'nokogiri'
4
+
5
+ $LOAD_PATH.unshift File.expand_path("../..", __FILE__)
6
+ # autoload
7
+
8
+ module RoSupport
9
+
10
+ #def self.autoload_modules(*mods)
11
+ # mods.each do |mod|
12
+ # autoload mod, mod.to_s.downcase
13
+ # end
14
+ #end
15
+
16
+ autoload :FileActions, 'lib/ro_support/file_actions'
17
+
18
+ include FileActions
19
+ autoload_all_files_in File.expand_path('../ro_support', __FILE__)
20
+
21
+ end
22
+
23
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :ro_support do
3
+ # # Task goes here
4
+ # end