Class: RsUserPolicy::Utilities

Inherits:
Object
  • Object
show all
Defined in:
lib/rs_user_policy/utilities.rb

Class Method Summary collapse

Class Method Details

.generate_compliant_password(size = 12) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ String

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

Parameters:

  • href (String)

    The relative href of the RightScale API resource

Returns:

  • (String)

    The unique ID of the resource



28
29
30
31
# 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, &block) ⇒ Object

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

Parameters:

  • order (Array)

    An array containing keys in the order they should be yielded to the block

  • hash (Hash)

    The hash to operate on in the specified order

  • block (Closure)

    A closure to yield to



39
40
41
42
43
44
# 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, &block) ⇒ Object

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

Parameters:

  • order (Array)

    An array containing values in the order they should be yielded to the block

  • hash (Hash)

    The hash to operate on in the specified order

  • block (Closure)

    A closure to yield to



52
53
54
55
56
57
# 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