mack_ruby_core_extensions 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.
- data/README +3 -0
- data/doc/classes/Array.html +465 -0
- data/doc/classes/Class.html +181 -0
- data/doc/classes/Dir.html +190 -0
- data/doc/classes/Float.html +148 -0
- data/doc/classes/Hash.html +232 -0
- data/doc/classes/Kernel.html +182 -0
- data/doc/classes/Mack.html +111 -0
- data/doc/classes/Mack/Utils.html +111 -0
- data/doc/classes/Mack/Utils/Inflector.html +422 -0
- data/doc/classes/Math.html +188 -0
- data/doc/classes/MethodNotImplemented.html +155 -0
- data/doc/classes/Module.html +203 -0
- data/doc/classes/NilClass.html +151 -0
- data/doc/classes/Object.html +315 -0
- data/doc/classes/String.html +890 -0
- data/doc/classes/Symbol.html +148 -0
- data/doc/created.rid +1 -0
- data/doc/files/README.html +111 -0
- data/doc/files/lib/extensions/array_rb.html +101 -0
- data/doc/files/lib/extensions/class_rb.html +101 -0
- data/doc/files/lib/extensions/dir_rb.html +101 -0
- data/doc/files/lib/extensions/float_rb.html +101 -0
- data/doc/files/lib/extensions/hash_rb.html +101 -0
- data/doc/files/lib/extensions/kernel_rb.html +109 -0
- data/doc/files/lib/extensions/logger_rb.html +101 -0
- data/doc/files/lib/extensions/math_rb.html +101 -0
- data/doc/files/lib/extensions/method_not_implemented_rb.html +108 -0
- data/doc/files/lib/extensions/module_rb.html +101 -0
- data/doc/files/lib/extensions/nil_rb.html +101 -0
- data/doc/files/lib/extensions/object_rb.html +101 -0
- data/doc/files/lib/extensions/string_rb.html +108 -0
- data/doc/files/lib/extensions/symbol_rb.html +101 -0
- data/doc/files/lib/mack_ruby_core_extensions_rb.html +101 -0
- data/doc/files/lib/utils/inflections_rb.html +101 -0
- data/doc/files/lib/utils/inflector_rb.html +108 -0
- data/doc/fr_class_index.html +42 -0
- data/doc/fr_file_index.html +44 -0
- data/doc/fr_method_index.html +92 -0
- data/doc/index.html +24 -0
- data/doc/rdoc-style.css +208 -0
- data/lib/extensions/array.rb +101 -0
- data/lib/extensions/class.rb +19 -0
- data/lib/extensions/dir.rb +25 -0
- data/lib/extensions/float.rb +5 -0
- data/lib/extensions/hash.rb +47 -0
- data/lib/extensions/kernel.rb +30 -0
- data/lib/extensions/logger.rb +0 -0
- data/lib/extensions/math.rb +15 -0
- data/lib/extensions/method_not_implemented.rb +12 -0
- data/lib/extensions/module.rb +29 -0
- data/lib/extensions/nil.rb +8 -0
- data/lib/extensions/object.rb +103 -0
- data/lib/extensions/string.rb +261 -0
- data/lib/extensions/symbol.rb +7 -0
- data/lib/mack_ruby_core_extensions.rb +7 -0
- data/lib/utils/inflections.rb +62 -0
- data/lib/utils/inflector.rb +129 -0
- data/test/extensions/array_test.rb +15 -0
- data/test/extensions/class_test.rb +28 -0
- data/test/extensions/dir_test.rb +15 -0
- data/test/extensions/float_test.rb +15 -0
- data/test/extensions/hash_test.rb +34 -0
- data/test/extensions/kernel_test.rb +15 -0
- data/test/extensions/logger_test.rb +15 -0
- data/test/extensions/math_test.rb +15 -0
- data/test/extensions/method_not_implemented_test.rb +15 -0
- data/test/extensions/module_test.rb +57 -0
- data/test/extensions/nil_test.rb +15 -0
- data/test/extensions/object_test.rb +15 -0
- data/test/extensions/string_test.rb +104 -0
- data/test/extensions/symbol_test.rb +15 -0
- data/test/test_helper.rb +6 -0
- data/test/utils/inflection_test.rb +32 -0
- metadata +133 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
class Class
|
2
|
+
|
3
|
+
def self.new_instance_of(klass_name)
|
4
|
+
mod = Module
|
5
|
+
mod = mod.const_get(klass_name)
|
6
|
+
mod.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def class_is_a?(klass_name)
|
10
|
+
self.ancestors.each do |an|
|
11
|
+
if an == klass_name
|
12
|
+
return true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
return false
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Dir
|
2
|
+
|
3
|
+
|
4
|
+
#creates multiple directories
|
5
|
+
def self.make_dirs(path)
|
6
|
+
dir_names = path.split('/')
|
7
|
+
new_path=""
|
8
|
+
dir_names.each do |x|
|
9
|
+
new_path = new_path << x << '/'
|
10
|
+
if !File.directory?(new_path)
|
11
|
+
Dir.mkdir(new_path)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
#if a directory exists, it deletes everything in it, otherwise it creates the directory
|
18
|
+
def self.prep_dir(path)
|
19
|
+
if File.directory?(path)
|
20
|
+
Dir.foreach(path) {|x| File.delete(path + x) unless File.directory?(path + x)}
|
21
|
+
else
|
22
|
+
Dir.make_dirs(path)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
def join(pair_string, join_string)
|
4
|
+
a = []
|
5
|
+
self.each_pair do |k, v|
|
6
|
+
a << sprintf(pair_string, k, v)
|
7
|
+
end
|
8
|
+
a.join(join_string)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Deletes the key(s) passed in from the hash.
|
12
|
+
def -(ars)
|
13
|
+
[ars].flatten.each {|a| self.delete(a)}
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
# Converts a hash to query string parameters.
|
18
|
+
# An optional boolean escapes the values if true, which is the default.
|
19
|
+
def to_params(escape = true)
|
20
|
+
params = ''
|
21
|
+
stack = []
|
22
|
+
|
23
|
+
each do |k, v|
|
24
|
+
if v.is_a?(Hash)
|
25
|
+
stack << [k,v]
|
26
|
+
else
|
27
|
+
v = Rack::Utils.escape(v) if escape
|
28
|
+
params << "#{k}=#{v}&"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
stack.each do |parent, hash|
|
33
|
+
hash.each do |k, v|
|
34
|
+
if v.is_a?(Hash)
|
35
|
+
stack << ["#{parent}[#{k}]", v]
|
36
|
+
else
|
37
|
+
v = Rack::Utils.escape(v) if escape
|
38
|
+
params << "#{parent}[#{k}]=#{v}&"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
params.chop! # trailing &
|
44
|
+
params
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'pp'
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
module Kernel
|
5
|
+
|
6
|
+
def self.pp_to_s(object)
|
7
|
+
pp_out = StringIO.new
|
8
|
+
PP.pp(object,pp_out)
|
9
|
+
return pp_out.string
|
10
|
+
end
|
11
|
+
|
12
|
+
def retryable(options = {}, &block)
|
13
|
+
opts = { :tries => 1, :on => Exception }.merge(options)
|
14
|
+
|
15
|
+
retries = opts[:tries]
|
16
|
+
retry_exceptions = [opts[:on]].flatten
|
17
|
+
|
18
|
+
x = %{
|
19
|
+
begin
|
20
|
+
return yield
|
21
|
+
rescue #{retry_exceptions.join(", ")} => e
|
22
|
+
retries -= 1
|
23
|
+
retry if retries > 0
|
24
|
+
end
|
25
|
+
}
|
26
|
+
|
27
|
+
eval(x, &block)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
File without changes
|
@@ -0,0 +1,12 @@
|
|
1
|
+
=begin rdoc
|
2
|
+
This exception is thrown if there is a method that has not been implemented that needs to be.
|
3
|
+
=end
|
4
|
+
class MethodNotImplemented < Exception
|
5
|
+
|
6
|
+
def initialize(meth, msg = nil)
|
7
|
+
mess = "#{meth} has not been implemented!\nPlease implement this method!\nIf you do not understand what this method is supposed to do, please consult the RDoc for more information."
|
8
|
+
mess += "\n#{msg}" unless msg.nil?
|
9
|
+
super(mess)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class Module
|
2
|
+
|
3
|
+
# Bulk converts the security level of methods in this Module from one level to another.
|
4
|
+
def convert_security_of_methods(old_level = :public, new_level = :protected)
|
5
|
+
eval("#{old_level}_instance_methods").each{ |meth| self.send(new_level, meth) }
|
6
|
+
self
|
7
|
+
end
|
8
|
+
|
9
|
+
# Includes this module into an Object, and changes all public methods to protected.
|
10
|
+
#
|
11
|
+
# Examples:
|
12
|
+
# module MyCoolUtils
|
13
|
+
# def some_meth
|
14
|
+
# "hi"
|
15
|
+
# end
|
16
|
+
# self.include_safely_into(FooController)
|
17
|
+
# end
|
18
|
+
# or:
|
19
|
+
# MyCoolUtils.include_safely_into(FooController, SomeOtherClass)
|
20
|
+
def include_safely_into(*args)
|
21
|
+
[args].flatten.each do |a|
|
22
|
+
if a.is_a?(String) || a.is_a?(Symbol)
|
23
|
+
a = a.to_s.constantize
|
24
|
+
end
|
25
|
+
a.send(:include, self.convert_security_of_methods)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
class Object
|
2
|
+
|
3
|
+
# This method gets called when a parameter is passed into a named route.
|
4
|
+
# This can be overridden in an Object to provlde custom handling of parameters.
|
5
|
+
def to_param
|
6
|
+
self.to_s
|
7
|
+
end
|
8
|
+
|
9
|
+
def running_time(message = "", logger = nil)
|
10
|
+
s_time = Time.now
|
11
|
+
s = "---Starting at #{s_time}---"
|
12
|
+
puts s
|
13
|
+
logger.info s unless logger.nil?
|
14
|
+
yield if block_given?
|
15
|
+
e_time = Time.now
|
16
|
+
e = "---Ending at #{e_time}---"
|
17
|
+
puts e
|
18
|
+
logger.info e unless logger.nil?
|
19
|
+
secs = e_time - s_time
|
20
|
+
if secs < 60
|
21
|
+
x = "Running time #{secs} seconds."
|
22
|
+
else
|
23
|
+
x = "Running time roughly #{secs/60} minutes [#{secs} seconds]"
|
24
|
+
end
|
25
|
+
x += " [MESSAGE]: #{message}" unless message.blank?
|
26
|
+
puts x
|
27
|
+
logger.info x unless logger.nil?
|
28
|
+
end
|
29
|
+
|
30
|
+
def send_with_chain(methods, *args)
|
31
|
+
obj = self
|
32
|
+
methods = [methods] unless methods.is_a?(Array)
|
33
|
+
methods.each {|m| obj = obj.send(m, *args)}
|
34
|
+
obj
|
35
|
+
end
|
36
|
+
|
37
|
+
=begin
|
38
|
+
ivar_cache allows you to cache the results of the block into an instance variable in a class,
|
39
|
+
and then will serve up that instance variable the next time you call that method again.
|
40
|
+
|
41
|
+
old way:
|
42
|
+
def show_page_link
|
43
|
+
unless @show_page_link # check if instance variable exists
|
44
|
+
# if the instance variable doesn't exist let's do some work and assign it to the instance variable.
|
45
|
+
@show_page_link = link_to("show", some_url(:id => self.id, :foo => bar, etc... => etc))
|
46
|
+
end
|
47
|
+
@show_page_link # now return the instance variable
|
48
|
+
end
|
49
|
+
|
50
|
+
new way:
|
51
|
+
def show_page_link
|
52
|
+
ivar_cache do
|
53
|
+
link_to("show", some_url(:id => self.id, :foo => bar, etc... => etc))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
this does everything the old way did, but it is much cleaner, and a lot less code!
|
58
|
+
in case you're wondering it caches the result, by default, to an instance variable named after the method,
|
59
|
+
so in our above example the instance variable would be name, @show_page_link. this can be overridden like such:
|
60
|
+
|
61
|
+
def show_page_link
|
62
|
+
ivar_cache("foo_var") do
|
63
|
+
link_to("show", some_url(:id => self.id, :foo => bar, etc... => etc))
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
now it will cache it to @foo_var
|
68
|
+
=end
|
69
|
+
def ivar_cache(var_name = nil, &block)
|
70
|
+
if var_name.nil?
|
71
|
+
call = caller[0]
|
72
|
+
var_name = call[(call.index('`')+1)...call.index("'")]
|
73
|
+
end
|
74
|
+
var = instance_variable_get("@#{var_name}")
|
75
|
+
unless var
|
76
|
+
return instance_variable_set("@#{var_name}", yield) if block_given?
|
77
|
+
end
|
78
|
+
instance_variable_get("@#{var_name}")
|
79
|
+
end
|
80
|
+
|
81
|
+
def ivar_cache_clear(var_name = nil, &block)
|
82
|
+
if var_name.nil?
|
83
|
+
call = caller[0]
|
84
|
+
var_name = call[(call.index('`')+1)...call.index("'")]
|
85
|
+
end
|
86
|
+
remove_instance_variable("@#{var_name}") rescue
|
87
|
+
yield if block_given?
|
88
|
+
end
|
89
|
+
|
90
|
+
def namespaces
|
91
|
+
ivar_cache("object_namespaces") do
|
92
|
+
nss = []
|
93
|
+
full_name = self.class.name.to_s
|
94
|
+
ns = full_name.split("::")
|
95
|
+
ns.each do |n|
|
96
|
+
nss << n
|
97
|
+
end
|
98
|
+
nss.pop
|
99
|
+
nss
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
@@ -0,0 +1,261 @@
|
|
1
|
+
require 'digest'
|
2
|
+
class String
|
3
|
+
|
4
|
+
# Camel cases the string.
|
5
|
+
#
|
6
|
+
# Examples:
|
7
|
+
# "user".camelcase # => User
|
8
|
+
# "my_blog".camelcase # => MyBlog
|
9
|
+
# "my/blog".camelcase # => My::Blog
|
10
|
+
def camelcase
|
11
|
+
self.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns a constant of the string.
|
15
|
+
#
|
16
|
+
# Examples:
|
17
|
+
# "User".constantize # => User
|
18
|
+
# "HomeController".constantize # => HomeController
|
19
|
+
# "Mack::Configuration" # => Mack::Configuration
|
20
|
+
def constantize
|
21
|
+
Module.instance_eval("::#{self}")
|
22
|
+
end
|
23
|
+
|
24
|
+
# If the string is empty, this will return true.
|
25
|
+
def blank?
|
26
|
+
self == ""
|
27
|
+
end
|
28
|
+
|
29
|
+
# Maps to Mack::Utils::Inflector.instance.pluralize
|
30
|
+
def plural
|
31
|
+
Mack::Utils::Inflector.instance.pluralize(self)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Maps to Mack::Utils::Inflector.instance.singularize
|
35
|
+
def singular
|
36
|
+
Mack::Utils::Inflector.instance.singularize(self)
|
37
|
+
end
|
38
|
+
|
39
|
+
def linkify(enabled = true, options = {}, &block)
|
40
|
+
text = self.dup
|
41
|
+
m = text.match(/\"([^"]+)\"\:([^:]+\:\S+)/)
|
42
|
+
until m.nil?
|
43
|
+
y = m.to_s
|
44
|
+
t = m[1]
|
45
|
+
url = m[2]
|
46
|
+
|
47
|
+
# The code below handles punctuation or p tags being mistakenly added to the url when the link is at the end of a sentence or body
|
48
|
+
url_punct_match = /\W*[ ]*[\<\/p\>]*$/.match(url)
|
49
|
+
punct = ''
|
50
|
+
if url_punct_match && url_punct_match[0] != ""
|
51
|
+
url.chomp!(url_punct_match[0])
|
52
|
+
punct = url_punct_match[0] unless url_punct_match == "="
|
53
|
+
end
|
54
|
+
|
55
|
+
if block_given?
|
56
|
+
if enabled
|
57
|
+
ret = yield t, url, options
|
58
|
+
text.gsub!(y, ret)
|
59
|
+
else
|
60
|
+
text.gsub!(y, t.to_s)
|
61
|
+
end
|
62
|
+
else
|
63
|
+
if enabled
|
64
|
+
text.gsub!(y, "<a href=\"#{url}\" #{options.join("%s=\"%s\"", " ")}>#{t}</a>#{punct}")# punct places punctuation back into proper place
|
65
|
+
else
|
66
|
+
text.gsub!(y, t.to_s)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
m = text.match(/\"([^"]+)\"\:([^:]+\:\S+)/)
|
70
|
+
end
|
71
|
+
return text
|
72
|
+
end
|
73
|
+
|
74
|
+
def methodize
|
75
|
+
x = self
|
76
|
+
|
77
|
+
# if we get down to a nil or an empty string raise an exception!
|
78
|
+
raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
|
79
|
+
|
80
|
+
# get rid of the big stuff in the front/back
|
81
|
+
x.strip!
|
82
|
+
|
83
|
+
# if we get down to a nil or an empty string raise an exception!
|
84
|
+
raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
|
85
|
+
|
86
|
+
x = x.underscore
|
87
|
+
|
88
|
+
# get rid of spaces and make the _
|
89
|
+
x.gsub!(' ', '_')
|
90
|
+
# get rid of everything that isn't 'safe' a-z, 0-9, ?, !, =, _
|
91
|
+
x.gsub!(/([^ a-zA-Z0-9\_\?\!\=]+)/n, '_')
|
92
|
+
|
93
|
+
# if we get down to a nil or an empty string raise an exception!
|
94
|
+
raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
|
95
|
+
|
96
|
+
# condense multiple 'safe' non a-z chars to just one.
|
97
|
+
# ie. ___ becomes _ !!!! becomes ! etc...
|
98
|
+
[' ', '_', '?', '!', "="].each do |c|
|
99
|
+
x.squeeze!(c)
|
100
|
+
end
|
101
|
+
|
102
|
+
# if we get down to a nil or an empty string raise an exception!
|
103
|
+
raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
|
104
|
+
|
105
|
+
#down case the whole thing
|
106
|
+
x.downcase!
|
107
|
+
|
108
|
+
# get rid of any characters at the beginning that aren't a-z
|
109
|
+
while !x.match(/^[a-z]/)
|
110
|
+
x.slice!(0)
|
111
|
+
|
112
|
+
# if we get down to a nil or an empty string raise an exception!
|
113
|
+
raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
|
114
|
+
end
|
115
|
+
|
116
|
+
# let's trim this bad boy down a bit now that we've cleaned it up, somewhat.
|
117
|
+
# we should do this before cleaning up the end character, because it's possible to end up with a
|
118
|
+
# bad char at the end if you trim too late.
|
119
|
+
x = x[0..100] if x.length > 100
|
120
|
+
|
121
|
+
# get rid of any characters at the end that aren't safe
|
122
|
+
while !x.match(/[a-z0-9\?\!\=]$/)
|
123
|
+
x.slice!(x.length - 1)
|
124
|
+
# if we get down to a nil or an empty string raise an exception!
|
125
|
+
raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
|
126
|
+
end
|
127
|
+
|
128
|
+
# if we get down to a nil or an empty string raise an exception!
|
129
|
+
raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
|
130
|
+
|
131
|
+
# let's get rid of characters that don't belong in the 'middle' of the method.
|
132
|
+
orig_middle = x[1..(x.length - 2)]
|
133
|
+
n_middle = orig_middle.dup
|
134
|
+
|
135
|
+
['?', '!', "="].each do |c|
|
136
|
+
n_middle.gsub!(c, "_")
|
137
|
+
end
|
138
|
+
|
139
|
+
# the previous gsub can leave us with multiple underscores that need cleaning up.
|
140
|
+
n_middle.squeeze!("_")
|
141
|
+
|
142
|
+
x.gsub!(orig_middle, n_middle)
|
143
|
+
x.gsub!("_=", "=")
|
144
|
+
x
|
145
|
+
end
|
146
|
+
|
147
|
+
def underscore
|
148
|
+
camel_cased_word = self.dup
|
149
|
+
camel_cased_word.to_s.gsub(/::/, '/').
|
150
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
151
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
152
|
+
tr("-", "_").
|
153
|
+
downcase
|
154
|
+
end
|
155
|
+
|
156
|
+
def starts_with?(x)
|
157
|
+
self.match(/^#{x}/) ? true : false
|
158
|
+
end
|
159
|
+
|
160
|
+
def ends_with?(x)
|
161
|
+
self.match(/#{x}$/) ? true : false
|
162
|
+
end
|
163
|
+
|
164
|
+
# Returns "is" or "are" based on the number, i.e. "i=6; There #{isare(i)} #{i} topics here."
|
165
|
+
def self.pluralize_word(num, vals = ["is", "are"])
|
166
|
+
return vals[0] if num.to_i==1
|
167
|
+
return vals[1]
|
168
|
+
end
|
169
|
+
|
170
|
+
def remove(val)
|
171
|
+
gsub(val, '')
|
172
|
+
end
|
173
|
+
|
174
|
+
def remove!(val)
|
175
|
+
gsub!(val, '')
|
176
|
+
end
|
177
|
+
|
178
|
+
# makes long strings of unbroken characters wrap inside elements (hopefully! tested in Safari, Firefox, and IE for Windows)
|
179
|
+
# For documentation about this kind of workaround to this browser compatibility problem please see the following URL's:
|
180
|
+
# http://www.quirksmode.org/oddsandends/wbr.html
|
181
|
+
# http://krijnhoetmer.nl/stuff/css/word-wrap-break-word/
|
182
|
+
# note: if the txt has spaces, this will only try to break up runs of non-space characters longer than "every" characters
|
183
|
+
def breakify(every = 30)
|
184
|
+
every = 1 if every < 1
|
185
|
+
text = self
|
186
|
+
textile_regex = /\"([^\"]+)\":([^:]*:[\/\/]{0,1}[^ ]*)/
|
187
|
+
long_regex = /\S{#{every},}/
|
188
|
+
brokentxt = text.gsub(long_regex) do |longword|
|
189
|
+
if longword =~ textile_regex #if the longword is a textile link...ignore and recheck for the link text only
|
190
|
+
textile_link = textile_regex.match(longword)[0]
|
191
|
+
link_text = textile_regex.match(longword)[1]
|
192
|
+
longword = link_text
|
193
|
+
if longword =~ long_regex #link text is long...allow break
|
194
|
+
textile_link.scan(/.{1,#{every}}/).join("<wbr/>")
|
195
|
+
else
|
196
|
+
textile_link #the url is what triggered the match...so leave it alone
|
197
|
+
end
|
198
|
+
else
|
199
|
+
longword.scan(/.{1,#{every}}/).join("<wbr/>") #no textile link matched
|
200
|
+
end
|
201
|
+
end
|
202
|
+
#text = %Q[<span style='word-wrap:break-word;wbr:after{content:"\\00200B"}'>#{brokentxt}</span>]
|
203
|
+
#brokentxt.gsub("<wbr/>", "<br />")
|
204
|
+
brokentxt.gsub("<wbr/>", " ")
|
205
|
+
end
|
206
|
+
|
207
|
+
def breakify!(every = 30)
|
208
|
+
self.replace(self.breakify(every))
|
209
|
+
end
|
210
|
+
|
211
|
+
def hexdigest
|
212
|
+
Digest::SHA1.hexdigest(self.downcase)
|
213
|
+
end
|
214
|
+
|
215
|
+
def hexdigest!
|
216
|
+
self.replace(self.hexdigest)
|
217
|
+
end
|
218
|
+
|
219
|
+
def self.randomize(length = 10)
|
220
|
+
chars = ("A".."H").to_a + ("J".."N").to_a + ("P".."T").to_a + ("W".."Z").to_a + ("3".."9").to_a
|
221
|
+
newpass = ""
|
222
|
+
1.upto(length) { |i| newpass << chars[rand(chars.size-1)] }
|
223
|
+
return newpass.upcase
|
224
|
+
end
|
225
|
+
|
226
|
+
def truncate(length = 30, truncate_string = "...")
|
227
|
+
if self.nil? then return end
|
228
|
+
l = length - truncate_string.length
|
229
|
+
if $KCODE == "NONE"
|
230
|
+
self.length > length ? self[0...l] + truncate_string : self
|
231
|
+
else
|
232
|
+
chars = self.split(//)
|
233
|
+
chars.length > length ? chars[0...l].join + truncate_string : self
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
def truncate!(length = 30, truncate_string = "...")
|
238
|
+
self.replace(self.truncate(length, truncate_string))
|
239
|
+
end
|
240
|
+
|
241
|
+
def capitalize_all_words
|
242
|
+
self.gsub(/\b\w/) {|s| s.upcase}
|
243
|
+
end
|
244
|
+
|
245
|
+
def capitalize_all_words!
|
246
|
+
self.replace(self.capitalize_all_words)
|
247
|
+
end
|
248
|
+
|
249
|
+
# keep adding on to this whenever it becomes apparent that unsafe strings
|
250
|
+
# could get passed through into the database
|
251
|
+
def sql_safe_str
|
252
|
+
if !self.nil?
|
253
|
+
self.gsub(/[\']/, "\'\'")
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
def sql_safe_str!
|
258
|
+
self.replace(self.sql_safe_str)
|
259
|
+
end
|
260
|
+
|
261
|
+
end
|