Class: Yle::AWS::Role::Accounts

Inherits:
Object
  • Object
show all
Defined in:
lib/yle/aws/role/accounts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aliases = nil) ⇒ Accounts



9
10
11
# File 'lib/yle/aws/role/accounts.rb', line 9

def initialize(aliases = nil)
  @aliases = aliases || {}
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



7
8
9
# File 'lib/yle/aws/role/accounts.rb', line 7

def aliases
  @aliases
end

Instance Method Details

#account_id?(id_or_alias) ⇒ Boolean

Returns truish if the argument looks like an AWS Account ID



32
33
34
# File 'lib/yle/aws/role/accounts.rb', line 32

def (id_or_alias)
  id_or_alias =~ /^\d{12}$/
end

#alias_matcher(s) ⇒ Object



41
42
43
44
# File 'lib/yle/aws/role/accounts.rb', line 41

def alias_matcher(s)
  pattern = s.gsub(/([^^])(?=[^$])/, '\1.*')
  Regexp.new(pattern)
end

#best_matching_account(id_or_alias) ⇒ Object



36
37
38
39
# File 'lib/yle/aws/role/accounts.rb', line 36

def (id_or_alias)
  matcher = alias_matcher(id_or_alias)
  aliases.keys.select { |key| matcher =~ key }.min_by(&:length)
end

#find(id_or_alias) ⇒ Object

Returns an ‘AccountAlias` that best matches the passed string



14
15
16
17
18
19
20
21
22
# File 'lib/yle/aws/role/accounts.rb', line 14

def find(id_or_alias)
  if (id_or_alias)
    name = aliases.key(id_or_alias) || id_or_alias
    AccountAlias.new(name, id_or_alias)
  else
    name = (id_or_alias)
    AccountAlias.new(name, aliases[name]) if name
  end
end

#to_sObject



24
25
26
27
28
29
# File 'lib/yle/aws/role/accounts.rb', line 24

def to_s
  max_name_length = aliases.keys.map(&:length).max
  aliases.sort.map do |name, id|
    "#{name.ljust(max_name_length)}\t#{id}"
  end.join("\n")
end