class RsUserPolicy::Utilities

Public Class Methods

generate_compliant_password(size=12) click to toggle source
# File lib/rs_user_policy/utilities.rb, line 59
def self.generate_compliant_password(size=12)
  compliant = nil
  password = ''
  until compliant
    chars = (
      ('a'..'z').to_a +
      ('A'..'Z').to_a +
      ('0'..'9').to_a +
      ["!","@","#","$","%","^","&","*","(",")","-","_","=","+"]
    ) - %w(i o 0 1 l 0)
    password = (1..size).collect{|a| chars[rand(chars.size)] }.join
    compliant = password =~ /^(?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@\#\$%\^&\*\(\)\-_=\+]).+$/
  end
  password
end
id_from_href(href) click to toggle source

Uses a regex to parse a RightScale API resource id from it’s relative href

@param [String] href The relative href of the RightScale API resource @return [String] The unique ID of the resource

# File lib/rs_user_policy/utilities.rb, line 28
def self.id_from_href(href)
  matches = /.*\/([a-zA-Z0-9\-]*)/.match(href)
  matches[1] || nil
end
yield_on_keys_in_order(order, hash) { |k,v| ... } click to toggle source

Operates on the key/value pairs in a hash in the order specified in ‘order’ followed by any key/value pairs not specified in the order

@param [Array] order An array containing keys in the order they should be yielded to the block @param [Hash] hash The hash to operate on in the specified order @param [Closure] block A closure to yield to

# File lib/rs_user_policy/utilities.rb, line 39
def self.yield_on_keys_in_order(order, hash, &block)
  order.each do |key|
    hash.select{|k,v| k == key}.each{|k,v| yield k,v }
  end
  hash.select{|k,v| !order.include?(k)}.each{|k,v| yield k,v}
end
yield_on_values_in_order(order, hash) { |k,v| ... } click to toggle source

Operates on the key/value pairs in a hash in the order specified in ‘order’ followed by any key/value pairs not specified in the order

@param [Array] order An array containing values in the order they should be yielded to the block @param [Hash] hash The hash to operate on in the specified order @param [Closure] block A closure to yield to

# File lib/rs_user_policy/utilities.rb, line 52
def self.yield_on_values_in_order(order, hash, &block)
  order.each do |value|
    hash.select{|k,v| v == value}.each{|k,v| yield k,v }
  end
  hash.select{|k,v| !order.include?(v) }.each{|k,v| yield k,v }
end