Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

init

  • Loading branch information...
commit 196e333468d4243dfa104b8c237c6fbfd567fca5 0 parents
cloudhead authored
20 COPYING
@@ -0,0 +1,20 @@
+Copyright (c) 2009 cloudhead
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4 README
@@ -0,0 +1,4 @@
+Extender
+========
+
+Handy extensions to keep your code clean™
18 lib/extender.rb
@@ -0,0 +1,18 @@
+$:.unshift File.expand_path(File.dirname(__FILE__))
+
+classes = [:object, :fixnum, :hash, :string, :array]
+methods = lambda {|c|
+ Object.const_get(c.capitalize).instance_methods
+}
+
+default = classes.inject({}) do |hash, c|
+ hash[c] = methods.call(c); hash
+end
+
+classes.each do |c|
+ require "extender/#{c}"
+end
+
+extended = classes.inject({}) do |hash, c|
+ hash[c] = methods.call(c) - default[c]; hash
+end
10 lib/extender/array.rb
@@ -0,0 +1,10 @@
+class Array
+ def value
+ unless self.size > 1
+ self[ 0 ]
+ else
+ raise StandardError, 'array has more than one value'
+ end
+ end
+ alias only value
+end
46 lib/extender/fixnum.rb
@@ -0,0 +1,46 @@
+class Fixnum
+ def minutes
+ self * 60
+ end
+
+ def hours
+ self * 3600
+ end
+
+ def days
+ self * 86400
+ end
+
+ def months
+ self.days * 30
+ end
+
+ def weeks
+ self.days * 7
+ end
+
+ def years
+ self.days * 365
+ end
+
+ def val?
+ self.zero?? nil : self
+ end
+
+ # 1 => 1st
+ # 2 => 2nd
+ # 3 => 3rd
+ # ...
+ def ordinal
+ case self % 100
+ when 11..13; "#{self}th"
+ else
+ case self % 10
+ when 1; "#{self}st"
+ when 2; "#{self}nd"
+ when 3; "#{self}rd"
+ else "#{self}th"
+ end
+ end
+ end
+end
8 lib/extender/hash.rb
@@ -0,0 +1,8 @@
+class Hash
+ def symbolize
+ inject({}) do |hash, (key, value)|
+ hash[(key.to_sym rescue key)] = value; hash
+ end
+ end
+ alias sym_keys symbolize
+end
15 lib/extender/object.rb
@@ -0,0 +1,15 @@
+class Object
+ def self.[](const) self.const_get( const ) end
+
+ def val?
+ self.empty?? nil : self if self.respond_to? :empty?
+ end
+
+ def / s
+ if self.respond_to? :to_s
+ File.join self.to_s, s.to_s
+ else
+ method_missing :/, s
+ end
+ end
+end
9 lib/extender/string.rb
@@ -0,0 +1,9 @@
+class String
+ def slugize
+ self.downcase.strip.gsub /[^a-z0-9]+/, '-'
+ end
+
+ def blank?
+ self unless self =~ /^[ \t]*$/
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.