lib/ruote/util/misc.rb in ruote-2.3.0.1 vs lib/ruote/util/misc.rb in ruote-2.3.0.2
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2005-2012, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2005-2013, John Mettraux, jmettraux@gmail.com
#
# 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
@@ -197,8 +197,58 @@
# turns the argument into a string and "comma splits" it.
#
def self.comma_split(o)
o.is_a?(Array) ? o : o.to_s.split(/\s*,\s*/).collect { |e| e.strip }
+ end
+
+ # A bit like #inspect but produces a tighter output (ambiguous to machines).
+ #
+ def self.insp(o, opts={})
+
+ case o
+ when nil
+ 'nil'
+ when Hash
+ trim = opts[:trim] || []
+ '{' +
+ o.reject { |k, v|
+ v.nil? && trim.include?(k.to_s)
+ }.collect { |k, v|
+ "#{k}: #{insp(v)}"
+ }.join(', ') +
+ '}'
+ when Array
+ '[' + o.collect { |e| insp(e) }.join(', ') + ']'
+ when String
+ o.match(/\s/) ? o.inspect : o
+ else
+ o.inspect
+ end
+ end
+
+ def self.pps(o, w=79)
+
+ PP.pp(o, StringIO.new, w).string
+ end
+
+ #--
+ # [de]camelize
+ #++
+
+ # Our own quick camelize implementation (no need to require active support).
+ #
+ def self.camelize(s, first_up=false)
+
+ s = s.capitalize if first_up
+
+ s.gsub(/(_.)/) { |x| x[1, 1].upcase }
+ end
+
+ # Quick decamelize implementation.
+ #
+ def self.decamelize(s)
+
+ s.gsub(/(.)([A-Z])/, '\1_\2').downcase
end
end