mpatch 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/Gemfile +2 -0
- data/README.md +4 -0
- data/Rakefile +1 -0
- data/VERSION +1 -0
- data/files.rb +24 -0
- data/lib/mpatch/active_support/str2duck.rb +90 -0
- data/lib/mpatch/array.rb +149 -0
- data/lib/mpatch/boolean.rb +15 -0
- data/lib/mpatch/class.rb +92 -0
- data/lib/mpatch/exception.rb +0 -0
- data/lib/mpatch/file.rb +71 -0
- data/lib/mpatch/hash.rb +109 -0
- data/lib/mpatch/integer.rb +12 -0
- data/lib/mpatch/kernel.rb +9 -0
- data/lib/mpatch/module.rb +16 -0
- data/lib/mpatch/object.rb +275 -0
- data/lib/mpatch/proc.rb +27 -0
- data/lib/mpatch/process.rb +14 -0
- data/lib/mpatch/random.rb +39 -0
- data/lib/mpatch/string.rb +78 -0
- data/lib/mpatch/yml.rb +11 -0
- data/lib/mpatch.rb +6 -0
- data/mpatch.gemspec +22 -0
- metadata +69 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YjkwMmJkYTBlZjA5YWU2MmIwZGU0ZGMzYTE2YTgxNTRmY2M1NjMzNQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MjhjMTNlMjVmZjk5ZTlkOTc0MDkzMTU4NTZkNThmY2QxOTMwMTllYQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
OTdhZmY2NGI1M2JkODUxOTQ3NGFhZjAyZDZkMzM4ODY1NzcwNzA2ZWZmNmRh
|
10
|
+
NjJhNDc2MjdjOTAyZGY4MGZmYzliZDU0N2U2MjQ5ZjA1ZTcxMzdlMjk0ODhk
|
11
|
+
ZmZiY2RkNjJkMjA4MjcyZjk2NTJlZjMyYzk1NTc0OTk0OGY4OTA=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZmRmMmIzMDBiMWQ5MTI5YzZjMmE3Njc5Y2Q4NWMzZWI2MTBiOWJkOThiYmRm
|
14
|
+
YmNhZmMzNGQ1MjY1NTYxZWZhNzczMmVhZmMxYzZhYzY1NDk5ZTI5ZDBjYzRi
|
15
|
+
MDY1ZjEyZDJiMTJlZTVmM2Y0MDEyYWE3NTFjYjVmNjFmZjYzMDc=
|
data/Gemfile
ADDED
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join "bundler","gem_tasks"
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/files.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
### Get Files from dir
|
2
|
+
begin
|
3
|
+
|
4
|
+
files_to_be_loaded = %w[version.rb]
|
5
|
+
|
6
|
+
SpecFiles= Array.new
|
7
|
+
|
8
|
+
Dir[File.expand_path(File.join(File.dirname(__FILE__),"**","*"))].sort.uniq.each do |one_file_name|
|
9
|
+
one_file_name = File.expand_path one_file_name
|
10
|
+
file_name = one_file_name[(File.expand_path(File.dirname(__FILE__)).to_s.length+1)..(one_file_name.length-1)]
|
11
|
+
|
12
|
+
if !one_file_name.include?("pkg")
|
13
|
+
if !File.directory? file_name
|
14
|
+
SpecFiles.push file_name
|
15
|
+
STDOUT.puts file_name if $DEBUG
|
16
|
+
if files_to_be_loaded.include? one_file_name.split(File::SEPARATOR).last
|
17
|
+
load one_file_name
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require File.join 'active_support','time'
|
2
|
+
|
3
|
+
class String
|
4
|
+
|
5
|
+
def duck
|
6
|
+
|
7
|
+
begin
|
8
|
+
if self == self.to_f.to_s
|
9
|
+
return self.to_f
|
10
|
+
end
|
11
|
+
rescue NoMethodError
|
12
|
+
end
|
13
|
+
|
14
|
+
begin
|
15
|
+
if self == self.to_i.to_s
|
16
|
+
return self.to_i
|
17
|
+
end
|
18
|
+
rescue NoMethodError
|
19
|
+
end
|
20
|
+
|
21
|
+
begin
|
22
|
+
if self.gsub(":","") == self.to_datetime.to_s.gsub(
|
23
|
+
"T"," ").gsub("+"," +").gsub(":","")
|
24
|
+
return self.to_datetime
|
25
|
+
end
|
26
|
+
rescue Exception
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
if self == self.to_datetime.to_s
|
31
|
+
return self.to_datetime
|
32
|
+
end
|
33
|
+
rescue Exception
|
34
|
+
end
|
35
|
+
|
36
|
+
begin
|
37
|
+
if self == self.to_date.to_s
|
38
|
+
return self.to_date
|
39
|
+
end
|
40
|
+
rescue Exception
|
41
|
+
end
|
42
|
+
|
43
|
+
begin
|
44
|
+
if self == "true"
|
45
|
+
return true
|
46
|
+
end
|
47
|
+
rescue NoMethodError
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
begin
|
52
|
+
if self == "false"
|
53
|
+
return false
|
54
|
+
end
|
55
|
+
rescue NoMethodError
|
56
|
+
end
|
57
|
+
|
58
|
+
begin
|
59
|
+
string_input= self
|
60
|
+
contain= nil
|
61
|
+
["[", "]","^","$","*","/"].each do |one_sym|
|
62
|
+
if string_input.include? one_sym
|
63
|
+
contain ||= true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
if contain == true
|
67
|
+
string_regexp= Regexp.new(string_input).inspect#.gsub('\\','')
|
68
|
+
string_regexp= string_regexp[1..(string_regexp.length-2)]
|
69
|
+
if string_input == string_regexp
|
70
|
+
return Regexp.new(string_input)
|
71
|
+
else
|
72
|
+
raise ArgumentError,"invalid input string"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
rescue ArgumentError
|
76
|
+
end
|
77
|
+
|
78
|
+
begin
|
79
|
+
if self == self.to_s.to_s
|
80
|
+
return self.to_s
|
81
|
+
end
|
82
|
+
rescue NoMethodError
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
alias :to_duck :duck
|
88
|
+
alias :to_d :duck
|
89
|
+
|
90
|
+
end
|
data/lib/mpatch/array.rb
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
# remove arguments or array of
|
4
|
+
# parameters from the main array
|
5
|
+
def trim(*args)
|
6
|
+
|
7
|
+
args.dup.each do |one_element|
|
8
|
+
if one_element.class == Array
|
9
|
+
args.delete_at(args.index(one_element))
|
10
|
+
args= args+one_element
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
delete_array= Array.new
|
15
|
+
args.each do |one_element|
|
16
|
+
index= self.index(one_element)
|
17
|
+
unless index.nil?
|
18
|
+
delete_array.push index
|
19
|
+
self.delete_at(index)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
return self
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
# return index of the target element
|
28
|
+
def index_of(target_element)
|
29
|
+
array = self
|
30
|
+
hash = Hash[array.map.with_index.to_a]
|
31
|
+
return hash[target_element]
|
32
|
+
end
|
33
|
+
|
34
|
+
# remove n. element from the end
|
35
|
+
# and return a new object
|
36
|
+
def pinch n=1
|
37
|
+
return self[0..(self.count-(n+1))]
|
38
|
+
end
|
39
|
+
|
40
|
+
# remove n. element from the end
|
41
|
+
# and return the original object
|
42
|
+
def pinch! n=1
|
43
|
+
n.times do
|
44
|
+
self.pop
|
45
|
+
end
|
46
|
+
return self
|
47
|
+
end
|
48
|
+
|
49
|
+
# return boolean by other array
|
50
|
+
# all element included or
|
51
|
+
# not in the target array
|
52
|
+
def contain?(oth_array)#anothere array
|
53
|
+
(oth_array & self) == oth_array
|
54
|
+
end
|
55
|
+
|
56
|
+
# return boolean by other array
|
57
|
+
# if any element included from
|
58
|
+
# the oth_array, return a true
|
59
|
+
def contain_any_of?(oth_array)
|
60
|
+
oth_array.each do |element|
|
61
|
+
if self.include? element
|
62
|
+
return true
|
63
|
+
end
|
64
|
+
end
|
65
|
+
return false
|
66
|
+
end
|
67
|
+
|
68
|
+
alias :contains_any_of? :contain_any_of?
|
69
|
+
alias :has_any_of? :contain_any_of?
|
70
|
+
|
71
|
+
# do safe transpose
|
72
|
+
def safe_transpose
|
73
|
+
result = []
|
74
|
+
max_size = self.max { |a,b| a.size <=> b.size }.size
|
75
|
+
max_size.times do |i|
|
76
|
+
result[i] = Array.new(self.first.size)
|
77
|
+
self.each_with_index { |r,j| result[i][j] = r[i] }
|
78
|
+
end
|
79
|
+
result
|
80
|
+
end
|
81
|
+
|
82
|
+
alias :contains? :contain?
|
83
|
+
|
84
|
+
# return boolean
|
85
|
+
# if any element class is equal to th given class
|
86
|
+
# return a true , else false
|
87
|
+
def contain_element_of_class?(class_name)
|
88
|
+
target_array= self.map{|e| e.class }.uniq
|
89
|
+
if class_name.class != Class
|
90
|
+
raise ArgumentError, "Argument must be a Class!"
|
91
|
+
end
|
92
|
+
if target_array.include? class_name
|
93
|
+
return true
|
94
|
+
end
|
95
|
+
return false
|
96
|
+
end
|
97
|
+
|
98
|
+
alias :contains_element_of_class? :contain_element_of_class?
|
99
|
+
alias :has_element_of_class? :contain_element_of_class?
|
100
|
+
|
101
|
+
# generate params structure from array
|
102
|
+
# *args => [:opts,:args]
|
103
|
+
def params_separation
|
104
|
+
|
105
|
+
options= self.map { |element|
|
106
|
+
if element.class == Hash
|
107
|
+
element
|
108
|
+
end
|
109
|
+
}.uniq - [ nil ]
|
110
|
+
#options.each{|e| self.delete(e) }
|
111
|
+
arguments= self.dup - options
|
112
|
+
options= Hash[*options]
|
113
|
+
|
114
|
+
return [options,arguments]
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
alias :separate_params :params_separation
|
119
|
+
alias :process_params :params_separation
|
120
|
+
|
121
|
+
# generate params structure from array
|
122
|
+
# return_array
|
123
|
+
def extract_class! class_name
|
124
|
+
|
125
|
+
if class_name.class != Class
|
126
|
+
raise ArgumentError, "parameter must be a class name"
|
127
|
+
end
|
128
|
+
|
129
|
+
return_value= self.map { |element|
|
130
|
+
if element.class == class_name
|
131
|
+
element
|
132
|
+
end
|
133
|
+
}.uniq - [ nil ]
|
134
|
+
return_value.each{|e| self.delete(e) }
|
135
|
+
|
136
|
+
return return_value
|
137
|
+
|
138
|
+
end
|
139
|
+
alias :cut_class! :extract_class!
|
140
|
+
|
141
|
+
# generate params structure from array
|
142
|
+
# *args - options {}
|
143
|
+
def extract_options!
|
144
|
+
options= self.extract_class! Hash
|
145
|
+
return Hash[*options]
|
146
|
+
end
|
147
|
+
alias :extract_hash! :extract_options!
|
148
|
+
|
149
|
+
end
|
data/lib/mpatch/class.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
class Class
|
2
|
+
|
3
|
+
# get singleton methods to target class without super class methods
|
4
|
+
def class_methods
|
5
|
+
self.methods - Object.methods
|
6
|
+
end
|
7
|
+
|
8
|
+
# bind a singleton method to a class object
|
9
|
+
def create_class_method(method,&block)
|
10
|
+
self.class_eval do
|
11
|
+
define_singleton_method method do |*args|
|
12
|
+
block.call *args
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# create an instance method
|
18
|
+
def create_instance_method(method,&block)
|
19
|
+
self.class_eval do
|
20
|
+
define_method method do |*args|
|
21
|
+
block.call *args
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Iterates over all subclasses (direct and indirect)
|
27
|
+
def each_subclass
|
28
|
+
ObjectSpace.each_object(Class) { | candidate |
|
29
|
+
begin
|
30
|
+
yield candidate if candidate < self
|
31
|
+
rescue ArgumentError
|
32
|
+
# comparison of Class with Class failed (ArgumentError)
|
33
|
+
end
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns an Array of subclasses (direct and indirect)
|
38
|
+
def subclasses_all
|
39
|
+
ret = []
|
40
|
+
each_subclass {|c| ret.push c}
|
41
|
+
ret
|
42
|
+
end
|
43
|
+
|
44
|
+
alias :all_subclasses :subclasses_all
|
45
|
+
|
46
|
+
# Returns an Array of direct subclasses
|
47
|
+
def subclasses
|
48
|
+
ret = []
|
49
|
+
each_subclass {|c| ret.push(c) if c.superclass == self }
|
50
|
+
ret
|
51
|
+
end
|
52
|
+
alias :subclass :subclasses
|
53
|
+
|
54
|
+
# create singleton attribute
|
55
|
+
def class_attr_accessor(name)
|
56
|
+
|
57
|
+
### GET
|
58
|
+
begin
|
59
|
+
define_method name do
|
60
|
+
class_variable_get "@@#{name}"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
### SET
|
65
|
+
begin
|
66
|
+
define_method "#{name}=" do |new_val|
|
67
|
+
class_variable_set "@@#{name}", new_val
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
# create class instance attribute
|
74
|
+
def instance_attr_accessor(name)
|
75
|
+
|
76
|
+
### GET
|
77
|
+
begin
|
78
|
+
define_method name do
|
79
|
+
instance_variable_get "@#{name}"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
### SET
|
84
|
+
begin
|
85
|
+
define_method "#{name}=" do |new_val|
|
86
|
+
instance_variable_set "@#{name}", new_val
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
File without changes
|
data/lib/mpatch/file.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
class File
|
2
|
+
|
3
|
+
# create a file, if not exsist create file, and dir if needed
|
4
|
+
def self.create(route_name ,filemod="w",string_data=String.new)
|
5
|
+
begin
|
6
|
+
|
7
|
+
#file_name generate
|
8
|
+
if !route_name.to_s.split(File::SEPARATOR).last.nil? || route_name.to_s.split(File::SEPARATOR).last != ''
|
9
|
+
file_name = route_name.to_s.split(File::SEPARATOR).last
|
10
|
+
else
|
11
|
+
file_name = nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
#path_way
|
15
|
+
begin
|
16
|
+
raise ArgumentError, "missing route_name: #{route_name}" if route_name.nil?
|
17
|
+
path = File.expand_path(route_name).to_s.split(File::SEPARATOR)
|
18
|
+
path = path - [File.expand_path(route_name).to_s.split(File::SEPARATOR).last]
|
19
|
+
path.shift
|
20
|
+
end
|
21
|
+
|
22
|
+
#job
|
23
|
+
begin
|
24
|
+
if !Dir.exists?(File::SEPARATOR+path.join(File::SEPARATOR))
|
25
|
+
|
26
|
+
at_now = File::SEPARATOR
|
27
|
+
path.each do |dir_to_be_checked|
|
28
|
+
|
29
|
+
at_now += "#{dir_to_be_checked+File::SEPARATOR}"
|
30
|
+
Dir.mkdir(at_now) if !Dir.exists?(at_now)
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# write data
|
37
|
+
begin
|
38
|
+
full_path = "#{File::SEPARATOR+path.join(File::SEPARATOR)+File::SEPARATOR}#{file_name}"
|
39
|
+
if File.exist? full_path
|
40
|
+
File.open(full_path,filemod).write string_data
|
41
|
+
else
|
42
|
+
File.new(full_path,filemod).write string_data
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
rescue Exception => ex
|
47
|
+
puts ex
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# start read the file object on each line
|
52
|
+
# optionable an integer value to start read line at
|
53
|
+
# compatible with mac (i am linux user, so not tested)
|
54
|
+
def each_line_from(start_at=1,&block)
|
55
|
+
unless [Integer,Fixnum,Bignum].include?(start_at.class)
|
56
|
+
raise ArgumentError, "invalid line index"
|
57
|
+
end
|
58
|
+
begin
|
59
|
+
line_num= 1
|
60
|
+
text= self.read
|
61
|
+
text.gsub!(/\r\n?/, "\n")
|
62
|
+
text.each_line do |*line|
|
63
|
+
if line_num >= start_at
|
64
|
+
block.call #*line
|
65
|
+
end
|
66
|
+
line_num += 1
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
data/lib/mpatch/hash.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
# remove elements by keys,
|
4
|
+
# array of keys,
|
5
|
+
# hashTags,
|
6
|
+
# strings
|
7
|
+
def trim(*args)
|
8
|
+
|
9
|
+
args.each do |one_element|
|
10
|
+
case true
|
11
|
+
|
12
|
+
when one_element.class == Hash
|
13
|
+
begin
|
14
|
+
one_element.each do |key,value|
|
15
|
+
if self[key] == value
|
16
|
+
self.delete(key)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
when one_element.class == Array
|
22
|
+
begin
|
23
|
+
one_element.each do |one_key|
|
24
|
+
self.delete(one_key)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
when one_element.class == Symbol,
|
29
|
+
one_element.class == String
|
30
|
+
begin
|
31
|
+
self.delete(one_element)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
return self
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
#pass single or array of keys, which will be removed, returning the remaining hash
|
41
|
+
def remove!(*keys)
|
42
|
+
keys.each{|key| self.delete(key) }
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
#non-destructive version
|
47
|
+
def remove(*keys)
|
48
|
+
self.dup.remove!(*keys)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns a new hash with +self+ and +other_hash+ merged recursively.
|
52
|
+
#
|
53
|
+
# h1 = {:x => {:y => [4,5,6]}, :z => [7,8,9]}
|
54
|
+
# h2 = {:x => {:y => [7,8,9]}, :z => "xyz"}
|
55
|
+
#
|
56
|
+
# h1.deep_merge(h2) #=> { :x => {:y => [7, 8, 9]}, :z => "xyz" }
|
57
|
+
# h2.deep_merge(h1) #=> { :x => {:y => [4, 5, 6]}, :z => [7, 8, 9] }
|
58
|
+
def deep_merge(other_hash)
|
59
|
+
dup.deep_merge!(other_hash)
|
60
|
+
end unless method_defined? :deep_merge
|
61
|
+
|
62
|
+
alias :+ :deep_merge
|
63
|
+
|
64
|
+
# Same as +deep_merge+, but modifies +self+.
|
65
|
+
def deep_merge!(other_hash)
|
66
|
+
other_hash.each_pair do |k,v|
|
67
|
+
tv = self[k]
|
68
|
+
self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
|
69
|
+
end
|
70
|
+
self
|
71
|
+
end unless method_defined? :deep_merge!
|
72
|
+
|
73
|
+
# return bool that does the sub hash all element include the hash who call this or not
|
74
|
+
def deep_include?(sub_hash)
|
75
|
+
sub_hash.keys.all? do |key|
|
76
|
+
self.has_key?(key) && if sub_hash[key].is_a?(Hash)
|
77
|
+
self[key].is_a?(Hash) && self[key].deep_include?(sub_hash[key])
|
78
|
+
else
|
79
|
+
self[key] == sub_hash[key]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end unless method_defined? :deep_include?
|
83
|
+
|
84
|
+
# map hash will work just alike map but instead of an array it will return a hash obj
|
85
|
+
#
|
86
|
+
# {:hello=> "world",:world => "hello"}.map_hash{|k,v| [ k , 123] }
|
87
|
+
# #> {:hello=>123, :world=>123}
|
88
|
+
#
|
89
|
+
# {:hello=> "world",:world => "hello"}.map_hash{|k,v| { k => 123 } }
|
90
|
+
# #> {:hello=>123, :world=>123}
|
91
|
+
#
|
92
|
+
def map_hash &block
|
93
|
+
|
94
|
+
tmp_hash= Hash.new
|
95
|
+
map_hash_obj= self.map &block
|
96
|
+
map_hash_obj.each do |hash|
|
97
|
+
|
98
|
+
if hash.class <= Array
|
99
|
+
hash= Hash[*hash]
|
100
|
+
end
|
101
|
+
|
102
|
+
tmp_hash.deep_merge!(hash)
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
return tmp_hash
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class Module
|
2
|
+
|
3
|
+
# return the module objects direct sub modules
|
4
|
+
def modules
|
5
|
+
constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == Module}
|
6
|
+
end
|
7
|
+
|
8
|
+
# return the module objects direct sub modules
|
9
|
+
def classes
|
10
|
+
constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == Class}
|
11
|
+
end
|
12
|
+
|
13
|
+
alias :submodules :modules
|
14
|
+
alias :subclasses :classes
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,275 @@
|
|
1
|
+
class Object
|
2
|
+
|
3
|
+
# basic validations for those who fear the DUCK!
|
4
|
+
def must_be class_name
|
5
|
+
if class_name.class == Class
|
6
|
+
begin
|
7
|
+
if self.class != class_name
|
8
|
+
raise ArgumentError, "invalid parameter given: #{self}"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
else
|
12
|
+
begin
|
13
|
+
if self != class_name
|
14
|
+
raise ArgumentError, "invalid parameter given: #{self}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
return self
|
19
|
+
end unless method_defined? :must_be
|
20
|
+
|
21
|
+
# return the object Binding
|
22
|
+
def binding?
|
23
|
+
return binding
|
24
|
+
end
|
25
|
+
|
26
|
+
alias :get_binding :binding?
|
27
|
+
|
28
|
+
# The hidden singleton lurks behind everyone
|
29
|
+
def metaclass; class << self; self; end; end
|
30
|
+
|
31
|
+
# extend the metaclass with an instance eval
|
32
|
+
def meta_eval &blk; metaclass.instance_eval &blk; end
|
33
|
+
|
34
|
+
# Adds methods to a metaclass
|
35
|
+
def meta_def name, &blk
|
36
|
+
meta_eval { define_method name, &blk }
|
37
|
+
end
|
38
|
+
|
39
|
+
# Defines an instance method within a class
|
40
|
+
def class_def name, &blk
|
41
|
+
class_eval { define_method name, &blk }
|
42
|
+
end
|
43
|
+
|
44
|
+
# constantize object
|
45
|
+
def constantize
|
46
|
+
camel_cased_word= self.to_s
|
47
|
+
names = camel_cased_word.split('::')
|
48
|
+
names.shift if names.empty? || names.first.empty?
|
49
|
+
|
50
|
+
constant = Object
|
51
|
+
names.each do |name|
|
52
|
+
constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
|
53
|
+
end
|
54
|
+
constant
|
55
|
+
end unless method_defined? :constantize
|
56
|
+
|
57
|
+
# find and replace object in object
|
58
|
+
def find_and_replace(input,*params)
|
59
|
+
params=Hash[*params]
|
60
|
+
# some default values
|
61
|
+
begin
|
62
|
+
#None!
|
63
|
+
end
|
64
|
+
# Do the find and replace
|
65
|
+
begin
|
66
|
+
|
67
|
+
if input.class == Array
|
68
|
+
input.count.times do |counter|
|
69
|
+
params.each do |key,value|
|
70
|
+
input[counter]= input[counter].gsub(key,value)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
elsif input.class == String
|
74
|
+
params.each do |key,value|
|
75
|
+
input= input.gsub(key,value)
|
76
|
+
end
|
77
|
+
elsif input.class == Integer
|
78
|
+
params.each do |key,value|
|
79
|
+
input= input.to_s.gsub(key,value).to_i
|
80
|
+
end
|
81
|
+
elsif input.class == Float
|
82
|
+
params.each do |key,value|
|
83
|
+
input= input.to_s.gsub(key,value).to_f
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# return value
|
88
|
+
return input
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# each for any object
|
93
|
+
def each_universal(&block)
|
94
|
+
case self.class.to_s.downcase
|
95
|
+
when "hash"
|
96
|
+
self.each do |key,value|
|
97
|
+
block.call(key,value)
|
98
|
+
end
|
99
|
+
when "array"
|
100
|
+
self.each do |one_element|
|
101
|
+
block.call(self.index(one_element),one_element)
|
102
|
+
end
|
103
|
+
else
|
104
|
+
block.call nil,self
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# map an object => experiment
|
109
|
+
def map_object(symbol_key="$type")
|
110
|
+
|
111
|
+
stage = self
|
112
|
+
do_later = Hash.new
|
113
|
+
samples = Hash.new
|
114
|
+
relations = Hash.new
|
115
|
+
main_object = nil
|
116
|
+
|
117
|
+
loop do
|
118
|
+
|
119
|
+
# processing
|
120
|
+
begin
|
121
|
+
|
122
|
+
tmp_key = String.new
|
123
|
+
tmp_hash= Hash.new
|
124
|
+
stage.each_universal do |key,value|
|
125
|
+
|
126
|
+
if value.class.to_s.downcase == "string"
|
127
|
+
|
128
|
+
if key== symbol_key
|
129
|
+
tmp_key= value
|
130
|
+
main_object ||= value
|
131
|
+
else
|
132
|
+
tmp_hash[key]= value
|
133
|
+
end
|
134
|
+
|
135
|
+
else
|
136
|
+
|
137
|
+
value.each_universal do |key_sub,value_sub|
|
138
|
+
if key_sub == symbol_key && tmp_key != String.new
|
139
|
+
child_property = Hash.new
|
140
|
+
child_property['type']= value_sub
|
141
|
+
child_property['class']= value.class.to_s
|
142
|
+
relations[tmp_key]= child_property
|
143
|
+
end
|
144
|
+
do_later[key_sub]= value_sub
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
if tmp_key != String.new && tmp_hash != Hash.new
|
151
|
+
samples[tmp_key]=tmp_hash
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
# finish
|
157
|
+
begin
|
158
|
+
break if do_later == Hash.new
|
159
|
+
stage= do_later
|
160
|
+
do_later = Hash.new
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
return {:samples => samples,
|
166
|
+
:relations => relations,
|
167
|
+
:main => main_object
|
168
|
+
}
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
# is class?
|
173
|
+
def class?
|
174
|
+
self.class == Class
|
175
|
+
end
|
176
|
+
|
177
|
+
# convert class instance instance variables into a hash object
|
178
|
+
def convert_to_hash
|
179
|
+
|
180
|
+
unless self.class.class == Class
|
181
|
+
raise NoMethodError, "undefined method `to_hash' for #{self.inspect}"
|
182
|
+
end
|
183
|
+
|
184
|
+
tmp_hash= Hash.new()
|
185
|
+
self.instance_variables.each do|var|
|
186
|
+
tmp_hash[var.to_s.delete("@")] = self.instance_variable_get(var)
|
187
|
+
end
|
188
|
+
|
189
|
+
return tmp_hash
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
# this will check that the class is
|
194
|
+
# defined or not in the runtime memory
|
195
|
+
def class_exists?
|
196
|
+
klass = Module.const_get(self)
|
197
|
+
return klass.is_a?(Class)
|
198
|
+
rescue NameError
|
199
|
+
return false
|
200
|
+
end
|
201
|
+
|
202
|
+
# This will convert a symbol or string and format to be a valid
|
203
|
+
# constant name and create from it a class with instance attribute accessors
|
204
|
+
# Best use is when you get raw data in string from external source
|
205
|
+
# and you want make them class objects
|
206
|
+
#
|
207
|
+
# :hello_world.to_class(:test)
|
208
|
+
# HelloWorld.to_class(:sup)
|
209
|
+
# hw_var = HelloWorld.new
|
210
|
+
# hw_var.sup = "Fine thanks!"
|
211
|
+
# hw_var.test = 5
|
212
|
+
#
|
213
|
+
# puts hw_var.test
|
214
|
+
#
|
215
|
+
# #> produce 5 :Integer
|
216
|
+
#
|
217
|
+
# you can also use this formats
|
218
|
+
# :HelloWorld , "hello.world",
|
219
|
+
# "hello/world", "Hello::World",
|
220
|
+
# "hello:world"...
|
221
|
+
def to_class(*attributes)
|
222
|
+
|
223
|
+
unless self.class == Symbol || self.class == String || self.class == Class
|
224
|
+
raise ArgumentError, "object must be symbol or string to make able build class to it"
|
225
|
+
end
|
226
|
+
|
227
|
+
class_name= self.to_s
|
228
|
+
|
229
|
+
unless self.class == Class
|
230
|
+
|
231
|
+
class_name= class_name[0].upcase+class_name[1..class_name.length]
|
232
|
+
%w[ _ . : / ].each do |one_sym|
|
233
|
+
|
234
|
+
loop do
|
235
|
+
index_nmb= class_name.index(one_sym)
|
236
|
+
break if index_nmb.nil?
|
237
|
+
class_name[index_nmb..index_nmb+1]= class_name[index_nmb+1].upcase
|
238
|
+
end
|
239
|
+
|
240
|
+
end
|
241
|
+
|
242
|
+
end
|
243
|
+
|
244
|
+
create_attribute = Proc.new do |*args|
|
245
|
+
|
246
|
+
end
|
247
|
+
|
248
|
+
unless class_name.class_exists?
|
249
|
+
|
250
|
+
Object.const_set(
|
251
|
+
class_name,
|
252
|
+
Class.new
|
253
|
+
)
|
254
|
+
|
255
|
+
end
|
256
|
+
|
257
|
+
|
258
|
+
class_name.constantize.class_eval do
|
259
|
+
attributes.each do |one_attribute|
|
260
|
+
attr_accessor one_attribute.to_s.to_sym
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
|
265
|
+
|
266
|
+
return true
|
267
|
+
|
268
|
+
end
|
269
|
+
|
270
|
+
alias :create_attributes :to_class
|
271
|
+
alias :map_sample :map_object
|
272
|
+
alias :each_univ :each_universal
|
273
|
+
alias :fnr :find_and_replace
|
274
|
+
|
275
|
+
end
|
data/lib/mpatch/proc.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
class Proc
|
2
|
+
|
3
|
+
# sugar syntax for proc * operator
|
4
|
+
# a = ->(x){x+1}
|
5
|
+
# b = ->(x){x*10}
|
6
|
+
# c = b*a
|
7
|
+
# c.call(1) #=> 20
|
8
|
+
def *(other)
|
9
|
+
Proc.new { |*args| self[*other[*args]] }
|
10
|
+
end unless method_defined? :*
|
11
|
+
|
12
|
+
def call_with_binding(bind, *args)
|
13
|
+
Bindless.new([bind]).run_proc(self, *args)
|
14
|
+
end
|
15
|
+
|
16
|
+
def call_with_obj(obj, *args)
|
17
|
+
m = nil
|
18
|
+
p = self
|
19
|
+
Object.class_eval do
|
20
|
+
define_method :a_temp_method_name, &p
|
21
|
+
m = instance_method :a_temp_method_name
|
22
|
+
remove_method :a_temp_method_name
|
23
|
+
end
|
24
|
+
m.bind(obj).call(*args)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Process
|
2
|
+
def self.daemonize
|
3
|
+
File.create Application.pid,'a+'
|
4
|
+
File.create Application.log,'a+'
|
5
|
+
File.create Application.daemon_stderr,'a+'
|
6
|
+
Daemon.start fork,
|
7
|
+
Application.pid,
|
8
|
+
Application.log,
|
9
|
+
Application.daemon_stderr
|
10
|
+
end
|
11
|
+
def self.stop
|
12
|
+
Daemon.stop
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class RND
|
2
|
+
class << self
|
3
|
+
def string(length= 7,amount=1,hyphen= " ")
|
4
|
+
amount_container= Array.new
|
5
|
+
amount.times do
|
6
|
+
mrg= String.new
|
7
|
+
mrg= (0...length).map{ ('a'..'z').to_a[rand(26)] }.join
|
8
|
+
amount_container.push mrg
|
9
|
+
end
|
10
|
+
return amount_container.join(hyphen)
|
11
|
+
end
|
12
|
+
def integer(length= 3)
|
13
|
+
Random.rand(length)
|
14
|
+
end
|
15
|
+
def boolean
|
16
|
+
rand(2) == 1
|
17
|
+
end
|
18
|
+
def time from = Time.at(1114924812), to = Time.now
|
19
|
+
rand(from..to)
|
20
|
+
end
|
21
|
+
def date from = Time.at(1114924812), to = Time.now
|
22
|
+
rand(from..to).to_date
|
23
|
+
end
|
24
|
+
def datetime from = Time.at(1114924812), to = Time.now
|
25
|
+
rand(from..to).to_datetime
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# alias in Random from RND
|
31
|
+
begin
|
32
|
+
(RND.singleton_methods-Object.singleton_methods).each do |one_method_sym|
|
33
|
+
Random.class_eval do
|
34
|
+
define_singleton_method one_method_sym do |*args|
|
35
|
+
RND.__send__(one_method_sym,*args)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
# Find string in othere string
|
4
|
+
def positions(oth_string)
|
5
|
+
|
6
|
+
special_chrs=%w[# _ & < > @ $ . , -]+[*(0..9)]+[*("A".."Z")]+[*("a".."z")]
|
7
|
+
loop do
|
8
|
+
if oth_string.include? special_chrs[0]
|
9
|
+
special_chrs.shift
|
10
|
+
else
|
11
|
+
break
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
string=self
|
16
|
+
return_array = Array.new
|
17
|
+
loop do
|
18
|
+
break if string.index(oth_string).nil?
|
19
|
+
range_value= ((string.index(oth_string))..(string.index(oth_string)+oth_string.length-1))
|
20
|
+
return_array.push range_value
|
21
|
+
[*range_value].each do |one_index|
|
22
|
+
string[one_index]= special_chrs[0]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# return value
|
27
|
+
return return_array
|
28
|
+
end
|
29
|
+
|
30
|
+
# Standard in rails. See official documentation
|
31
|
+
# [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
|
32
|
+
def camelize(first_letter = :upper)
|
33
|
+
if first_letter == :upper
|
34
|
+
gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
35
|
+
else
|
36
|
+
self[0..0].downcase + camelize[1..-1]
|
37
|
+
end
|
38
|
+
end unless method_defined? :camelize
|
39
|
+
|
40
|
+
# Standard in rails. See official documentation
|
41
|
+
# [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
|
42
|
+
def dasherize
|
43
|
+
gsub(/_/, '-')
|
44
|
+
end unless method_defined? :dasherize
|
45
|
+
|
46
|
+
# Standard in rails. See official documentation
|
47
|
+
# [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
|
48
|
+
def demodulize
|
49
|
+
gsub(/^.*::/, '')
|
50
|
+
end unless method_defined? :demodulize
|
51
|
+
|
52
|
+
# Standard in rails. See official documentation
|
53
|
+
# [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
|
54
|
+
def underscore
|
55
|
+
gsub(/::/, '/').
|
56
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
57
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
58
|
+
tr("-", "_").
|
59
|
+
downcase
|
60
|
+
end unless method_defined? :underscore
|
61
|
+
|
62
|
+
# Check that instance of String is start with an upper case or not
|
63
|
+
def capitalized?
|
64
|
+
self.match(/^[[:upper:]]/) ? true : false
|
65
|
+
end
|
66
|
+
|
67
|
+
# return the number how often the str is with in the self
|
68
|
+
# by default with \b regex border
|
69
|
+
def frequency(str)
|
70
|
+
begin
|
71
|
+
if str.class == String
|
72
|
+
str= '\b'+str+'\b'
|
73
|
+
end
|
74
|
+
end
|
75
|
+
self.scan(/#{str}/).count
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
data/lib/mpatch/yml.rb
ADDED
data/lib/mpatch.rb
ADDED
data/mpatch.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__),"files.rb"))
|
4
|
+
|
5
|
+
### Specification for the new Gem
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
|
8
|
+
spec.name = "mpatch"
|
9
|
+
spec.version = File.open(File.join(File.dirname(__FILE__),"VERSION")).read.split("\n")[0].chomp.gsub(' ','')
|
10
|
+
spec.authors = ["Adam Luzsi"]
|
11
|
+
spec.email = ["adamluzsi@gmail.com"]
|
12
|
+
spec.description = %q{This is a collection of my Ruby monkey patches for making easer to use the basic classes}
|
13
|
+
spec.summary = %q{collection of methods for extend basic classes}
|
14
|
+
spec.homepage = "https://github.com/adamluzsi/mpatch"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = SpecFiles
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mpatch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Luzsi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-10 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: This is a collection of my Ruby monkey patches for making easer to use
|
14
|
+
the basic classes
|
15
|
+
email:
|
16
|
+
- adamluzsi@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- Gemfile
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- VERSION
|
25
|
+
- files.rb
|
26
|
+
- lib/mpatch.rb
|
27
|
+
- lib/mpatch/active_support/str2duck.rb
|
28
|
+
- lib/mpatch/array.rb
|
29
|
+
- lib/mpatch/boolean.rb
|
30
|
+
- lib/mpatch/class.rb
|
31
|
+
- lib/mpatch/exception.rb
|
32
|
+
- lib/mpatch/file.rb
|
33
|
+
- lib/mpatch/hash.rb
|
34
|
+
- lib/mpatch/integer.rb
|
35
|
+
- lib/mpatch/kernel.rb
|
36
|
+
- lib/mpatch/module.rb
|
37
|
+
- lib/mpatch/object.rb
|
38
|
+
- lib/mpatch/proc.rb
|
39
|
+
- lib/mpatch/process.rb
|
40
|
+
- lib/mpatch/random.rb
|
41
|
+
- lib/mpatch/string.rb
|
42
|
+
- lib/mpatch/yml.rb
|
43
|
+
- mpatch.gemspec
|
44
|
+
homepage: https://github.com/adamluzsi/mpatch
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata: {}
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 2.2.1
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: collection of methods for extend basic classes
|
68
|
+
test_files: []
|
69
|
+
has_rdoc:
|