Class: HerokuCLI::PG::Database
- Inherits:
-
Object
- Object
- HerokuCLI::PG::Database
- Defined in:
- lib/heroku_cli/pg/database.rb
Instance Attribute Summary collapse
-
#info ⇒ Object
readonly
Returns the value of attribute info.
-
#pg ⇒ Object
readonly
Returns the value of attribute pg.
-
#url_names ⇒ Object
readonly
Returns the value of attribute url_names.
Instance Method Summary collapse
- #available? ⇒ Boolean
- #behind? ⇒ Boolean
- #behind_by ⇒ Object
- #created ⇒ Object
- #data_size ⇒ Object
- #follower? ⇒ Boolean
- #followers ⇒ Object
- #following ⇒ Object
- #fork? ⇒ Boolean
- #forked_from ⇒ Object
- #forks ⇒ Object
-
#initialize(info, pg = nil) ⇒ Database
constructor
A new instance of Database.
- #main? ⇒ Boolean
- #name ⇒ Object
- #parse_info(info) ⇒ Object
- #plan ⇒ Object
- #region ⇒ Object
- #reload ⇒ Object
- #resource_name ⇒ Object
- #status ⇒ Object
- #tables ⇒ Object
- #to_s ⇒ Object
- #url_name ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(info, pg = nil) ⇒ Database
Returns a new instance of Database.
7 8 9 10 11 |
# File 'lib/heroku_cli/pg/database.rb', line 7 def initialize(info, pg = nil) @pg = pg parse_info(info) end |
Instance Attribute Details
#info ⇒ Object (readonly)
Returns the value of attribute info.
4 5 6 |
# File 'lib/heroku_cli/pg/database.rb', line 4 def info @info end |
#pg ⇒ Object (readonly)
Returns the value of attribute pg.
5 6 7 |
# File 'lib/heroku_cli/pg/database.rb', line 5 def pg @pg end |
#url_names ⇒ Object (readonly)
Returns the value of attribute url_names.
4 5 6 |
# File 'lib/heroku_cli/pg/database.rb', line 4 def url_names @url_names end |
Instance Method Details
#available? ⇒ Boolean
86 87 88 |
# File 'lib/heroku_cli/pg/database.rb', line 86 def available? status == 'Available' end |
#behind? ⇒ Boolean
90 91 92 |
# File 'lib/heroku_cli/pg/database.rb', line 90 def behind? behind_by.positive? end |
#behind_by ⇒ Object
94 95 96 |
# File 'lib/heroku_cli/pg/database.rb', line 94 def behind_by info['Behind By']&.match(/(\d+)/)&.to_s&.to_i || 0 end |
#created ⇒ Object
41 42 43 |
# File 'lib/heroku_cli/pg/database.rb', line 41 def created info['Created'] end |
#data_size ⇒ Object
37 38 39 |
# File 'lib/heroku_cli/pg/database.rb', line 37 def data_size info['Data Size'] end |
#follower? ⇒ Boolean
82 83 84 |
# File 'lib/heroku_cli/pg/database.rb', line 82 def follower? info.key?('Following') end |
#followers ⇒ Object
58 59 60 |
# File 'lib/heroku_cli/pg/database.rb', line 58 def followers info['Followers'] end |
#following ⇒ Object
70 71 72 |
# File 'lib/heroku_cli/pg/database.rb', line 70 def following info['Following'].strip end |
#fork? ⇒ Boolean
78 79 80 |
# File 'lib/heroku_cli/pg/database.rb', line 78 def fork? !(main? || follower?) && forked_from&.any? end |
#forked_from ⇒ Object
66 67 68 |
# File 'lib/heroku_cli/pg/database.rb', line 66 def forked_from info['Forked From']&.split(',')&.map(&:strip) end |
#forks ⇒ Object
62 63 64 |
# File 'lib/heroku_cli/pg/database.rb', line 62 def forks info['Forks']&.split(',')&.map(&:strip) end |
#main? ⇒ Boolean
74 75 76 |
# File 'lib/heroku_cli/pg/database.rb', line 74 def main? @url_names.include?('DATABASE_URL') end |
#name ⇒ Object
29 30 31 |
# File 'lib/heroku_cli/pg/database.rb', line 29 def name @name ||= url_name.gsub(/_URL\z/, '') end |
#parse_info(info) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/heroku_cli/pg/database.rb', line 106 def parse_info(info) info = info.split("\n") if info.is_a?(String) @url_names = info.shift @url_names = @url_names.sub('=== ','').split(',').map(&:strip) @info = {} info.each do |line| next if line.strip.empty? k = line.split(':')[0]&.strip v = line.split(':')[1..-1]&.join(':')&.strip next if k.nil? || v.nil? @info[k] = v end end |
#plan ⇒ Object
33 34 35 |
# File 'lib/heroku_cli/pg/database.rb', line 33 def plan info['Plan'].gsub(' ', '-').downcase end |
#region ⇒ Object
98 99 100 |
# File 'lib/heroku_cli/pg/database.rb', line 98 def region info['Region'] end |
#reload ⇒ Object
13 14 15 |
# File 'lib/heroku_cli/pg/database.rb', line 13 def reload parse_info(pg.heroku("pg:info #{url_name}")) unless pg.nil? end |
#resource_name ⇒ Object
102 103 104 |
# File 'lib/heroku_cli/pg/database.rb', line 102 def resource_name info['Add-on'] end |
#status ⇒ Object
46 47 48 |
# File 'lib/heroku_cli/pg/database.rb', line 46 def status info['Status'] end |
#tables ⇒ Object
50 51 52 |
# File 'lib/heroku_cli/pg/database.rb', line 50 def tables info['Tables'].match(/(\d+)/)[0]&.to_i || 0 end |
#to_s ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/heroku_cli/pg/database.rb', line 21 def to_s result = ["=== #{@url_names.join(', ')}"] result.concat(['']) result.concat(info.map { |k, v| format("%-22.22s %s", "#{k}:", v)}) result.concat(['']) result.join("\n") end |
#url_name ⇒ Object
17 18 19 |
# File 'lib/heroku_cli/pg/database.rb', line 17 def url_name @url_names.first end |
#version ⇒ Object
54 55 56 |
# File 'lib/heroku_cli/pg/database.rb', line 54 def version Gem::Version.new(info['PG Version']) end |