Update connect/disconnect action states.
[kaya.git] / lib / utils.rb
blobb15cc5a03d62cb3e7a568f65c483e821d45fd26d
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 class Object
9   def tap
10     yield self
11     self
12   end
13   
14   def alter(property)
15     value = send(property)
16     yield value
17     send("#{property}=", value)
18   end
20   def metaclass
21     class << self
22       self
23     end
24   end
25   
26   def metaclass_eval(&blk)
27     metaclass.instance_eval(&blk)
28   end
29   
30   def map
31     yield self unless nil?
32   end
33 end
36 module Enumerable
37   def detect_index
38     i = 0
39     each do |item|
40       return i if yield item
41       i += 1
42     end
43     
44     nil
45   end
46 end
48 class Hash
49   def maph
50     { }.tap do |result|
51       each do |key, value|
52         key, value = yield key, value
53         result[key] = value
54       end
55     end
56   end
57 end
59 class String
60   def underscore
61     self.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
62          gsub(/([a-z\d])([A-Z])/,'\1_\2').
63          downcase
64   end
65   
66   def camelize
67     gsub(/_(.)/) {|m| $1.upcase }
68   end
69 end