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

Modules: Ruby1_8, Ruby1_9

Constant Summary collapse

RUBY_SUPPORT =
RUBY_VERSION >= '1.9' ? '1.9' : '1.8'

Class Method Summary collapse

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.

Examples:

Stomper::Support.constantize('Stomper::Frame') #=> Stomper::Frame
Stomper::Support.constantize('This::Constant::DoesNotExist) #=> raises NameError
Stomper::Support.constantize(Symbol) #=> Symbol 

Parameters:

  • klass (String, Module)

Returns:

  • (Module)


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.

Examples:

hash = { '10' => nil, 'key2' => [3, 5, 8, 13, 21], :other => :value }
Stomper::Helpers::Hash.keys_to_sym(hash) #=> { :'10' => nil, :key2 => [3, 5, 8, 13, 21], :other => :value }
hash #=> { '10' => nil, 'key2' => [3, 5, 8, 13, 21], :other => :value }

Parameters:

  • hsh ({Object => Object})

    The hash to convert. It’s keys must respond to to_sym.

Returns:

  • ({Symbol => Object})


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.

Examples:

hash = { '10' => nil, 'key2' => [3, 5, 8, 13, 21], :other => :value }
Stomper::Helpers::Hash.keys_to_sym!(hash) #=> { :'10' => nil, :key2 => [3, 5, 8, 13, 21], :other => :value }
hash #=> { :'10' => nil, :key2 => [3, 5, 8, 13, 21], :other => :value }

Parameters:

  • hsh ({Object => Object})

    The hash to convert. It’s keys must respond to to_sym.

Returns:

  • ({Symbol => Object})


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