Module: Stomper::Support
- Defined in:
- lib/stomper/support.rb
Overview
Module that stores methods that add supporiting functionality to Stomper and support for Ruby 1.9 and 1.8.7.
Defined Under Namespace
Constant Summary collapse
- RUBY_SUPPORT =
RUBY_VERSION >= '1.9' ? '1.9' : '1.8'
Class Method Summary collapse
-
.constantize(klass) ⇒ Module
Converts a string to the Ruby constant it names.
-
.keys_to_sym(hsh) ⇒ {Symbol => Object}
Duplicates an existing hash while transforming its keys to symbols.
-
.keys_to_sym!(hsh) ⇒ {Symbol => Object}
Replaces the keys of a hash with symbolized versions.
-
.next_serial(prefix = nil) ⇒ Object
Generates the next serial number in a thread-safe manner.
Class Method Details
.constantize(klass) ⇒ Module
Converts a string to the Ruby constant it names. If the klass
parameter is a kind of Module
, this method will return klass
directly.
58 59 60 61 62 63 64 65 |
# File 'lib/stomper/support.rb', line 58 def self.constantize(klass) return klass if klass.is_a?(Module) klass.to_s.split('::').inject(Object) do |const, named| next const if named.empty? const.const_defined?(named) ? const.const_get(named) : const.const_missing(named) end end |
.keys_to_sym(hsh) ⇒ {Symbol => Object}
Duplicates an existing hash while transforming its keys to symbols. The keys must implement the to_sym
method, otherwise an exception will be raised. This method is used internally to convert hashes keyed with Strings.
17 18 19 20 21 22 |
# File 'lib/stomper/support.rb', line 17 def self.keys_to_sym(hsh) hsh.inject({}) do |new_hash, (k,v)| new_hash[k.to_sym] = v new_hash end end |
.keys_to_sym!(hsh) ⇒ {Symbol => Object}
Replaces the keys of a hash with symbolized versions. The keys must implement the to_sym
method, otherwise an exception will be raised. This method is used internally to convert hashes keyed with Strings.
35 36 37 |
# File 'lib/stomper/support.rb', line 35 def self.keys_to_sym!(hsh) hsh.replace(keys_to_sym(hsh)) end |
.next_serial(prefix = nil) ⇒ Object
Generates the next serial number in a thread-safe manner. This method merely initializes an instance variable to 0 if it has not been set, then increments this value and returns its string representation.
42 43 44 45 46 47 48 |
# File 'lib/stomper/support.rb', line 42 def self.next_serial(prefix=nil) Thread.exclusive do @next_serial_sequence ||= 0 @next_serial_sequence += 1 @next_serial_sequence.to_s end end |