lib/sup/util.rb in sup-0.21.0 vs lib/sup/util.rb in sup-0.22.0
- old
+ new
@@ -8,19 +8,10 @@
require 'enumerator'
require 'benchmark'
require 'unicode'
require 'fileutils'
-## time for some monkeypatching!
-class Symbol
- unless method_defined? :to_proc
- def to_proc
- proc { |obj, *args| obj.send(self, *args) }
- end
- end
-end
-
class Lockfile
def gen_lock_id
Hash[
'host' => "#{ Socket.gethostname }",
'pid' => "#{ Process.pid }",
@@ -87,11 +78,11 @@
t = MIME::Types.type_for(bfn).first || MIME::Types.type_for("exe").first
make_attachment IO.read(fn), t.content_type, t.encoding, bfn.to_s
end
def charset
- if header.field?("content-type") && header.fetch("content-type") =~ /charset="?(.*?)"?(;|$)/i
+ if header.field?("content-type") && header.fetch("content-type") =~ /charset\s*=\s*"?(.*?)"?(;|$)/i
$1
end
end
def self.make_attachment payload, mime_type, encoding, filename
@@ -168,17 +159,10 @@
end
end
end
end
-class Range
- ## only valid for integer ranges (unless I guess it's exclusive)
- def size
- last - first + (exclude_end? ? 0 : 1)
- end
-end
-
class Module
def bool_reader *args
args.each { |sym| class_eval %{ def #{sym}?; @#{sym}; end } }
end
def bool_writer *args; attr_writer(*args); end
@@ -196,21 +180,10 @@
}
end
end
class Object
- def ancestors
- ret = []
- klass = self.class
-
- until klass == Object
- ret << klass
- klass = klass.superclass
- end
- ret
- end
-
## "k combinator"
def returning x; yield x; x; end
unless method_defined? :tap
def tap; yield self; self; end
@@ -380,12 +353,10 @@
ret << s
end
# Fix the damn string! make sure it is valid utf-8, then convert to
# user encoding.
- #
- # Not Ruby 1.8 compatible
def fix_encoding!
# first try to encode to utf-8 from whatever current encoding
encode!('UTF-8', :invalid => :replace, :undef => :replace)
# do this anyway in case string is set to be UTF-8, encoding to
@@ -404,12 +375,10 @@
self
end
# transcode the string if original encoding is know
# fix if broken.
- #
- # Not Ruby 1.8 compatible
def transcode to_encoding, from_encoding
begin
encode!(to_encoding, from_encoding, :invalid => :replace, :undef => :replace)
unless valid_encoding?
@@ -472,17 +441,10 @@
end
end
out = out.fix_encoding! # this should now be an utf-8 string of ascii
# compat chars.
end
-
- unless method_defined? :ascii_only?
- def ascii_only?
- size.times { |i| return false if self[i] & 128 != 0 }
- return true
- end
- end
end
class Numeric
def clamp min, max
if self < min
@@ -516,16 +478,10 @@
else
"<#{self}>"
end
end
- unless method_defined?(:ord)
- def ord
- self
- end
- end
-
## hacking the english language
def pluralize s
to_s + " " +
if self == 1
s
@@ -605,14 +561,10 @@
def between startline, endline
select { |l| true if l == startline .. l == endline }
end
end
-unless Object.const_defined? :Enumerator
- Enumerator = Enumerable::Enumerator
-end
-
class Array
def flatten_one_level
inject([]) { |a, e| a + e }
end
@@ -702,35 +654,9 @@
def [] k
@hash[k] ||= @constructor.call(k)
end
defer_all_other_method_calls_to :hash
-end
-
-class OrderedHash < Hash
- alias_method :store, :[]=
- alias_method :each_pair, :each
- attr_reader :keys
-
- def initialize *a
- @keys = []
- a.each { |k, v| self[k] = v }
- end
-
- def []= key, val
- @keys << key unless member?(key)
- super
- end
-
- def values; keys.map { |k| self[k] } end
- def index key; @keys.index key end
-
- def delete key
- @keys.delete key
- super
- end
-
- def each; @keys.each { |k| yield k, self[k] } end
end
## easy thread-safe class for determining who's the "winner" in a race (i.e.
## first person to hit the finish line
class FinishLine