lib/alfa/support.rb in alfa-0.0.4.pre vs lib/alfa/support.rb in alfa-0.0.5.pre

- old
+ new

@@ -29,18 +29,22 @@ end module Support extend self - def capitalize_name(arg) + def camelcase_name(arg) arg.to_s.split('/').last.split('_').map(&:capitalize).join end - def parse_arguments(*arguments) - return arguments[0..-2], arguments.last if arguments.last.is_a?(Hash) - return arguments, {} + def underscore_name(arg) + arg.to_s.split('/').last.scan(/[A-Z][a-z]*|[a-z]+/).map(&:downcase).join('_') end + + def args_kwargs(*args) + return args[0..-2], args.last if args.last.is_a?(Hash) + return args, {} + end end end @@ -94,7 +98,38 @@ def symbolize_keys! keys.each do |key| self[(key.to_sym rescue key) || key] = delete(key) end self + end + + + def delete!(*keys) + keys.each{|key| self.delete(key)} + self + end + + + def except(*keys) + self.dup.delete!(*keys) + end +end + + +class String + # PHP's two argument version of strtr + def strtr(replace_pairs) + keys = replace_pairs.map {|a, b| a } + values = replace_pairs.map {|a, b| b } + self.gsub( + /(#{keys.map{|a| Regexp.quote(a) }.join( ')|(' )})/ + ) { |match| values[keys.index(match)] } + end + + def strtr!(replace_pairs) + keys = replace_pairs.map {|a, b| a } + values = replace_pairs.map {|a, b| b } + self.gsub!( + /(#{keys.map{|a| Regexp.quote(a) }.join( ')|(' )})/ + ) { |match| values[keys.index(match)] } end end