Class: Yle::AWS::Role::Accounts
- Inherits:
-
Object
- Object
- Yle::AWS::Role::Accounts
- Defined in:
- lib/yle/aws/role/accounts.rb
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
Instance Method Summary collapse
-
#account_id?(id_or_alias) ⇒ Boolean
Returns truish if the argument looks like an AWS Account ID.
- #alias_matcher(s) ⇒ Object
- #best_matching_account(id_or_alias) ⇒ Object
-
#find(id_or_alias) ⇒ Object
Returns an ‘AccountAlias` that best matches the passed string.
-
#initialize(aliases = nil) ⇒ Accounts
constructor
A new instance of Accounts.
- #to_s ⇒ Object
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
#aliases ⇒ Object (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 account_id?(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 best_matching_account(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 account_id?(id_or_alias) name = aliases.key(id_or_alias) || id_or_alias AccountAlias.new(name, id_or_alias) else name = best_matching_account(id_or_alias) AccountAlias.new(name, aliases[name]) if name end end |
#to_s ⇒ Object
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 |