Class: HerokuCLI::PG::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/heroku_cli/pg/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#infoObject (readonly)

Returns the value of attribute info.



4
5
6
# File 'lib/heroku_cli/pg/database.rb', line 4

def info
  @info
end

#pgObject (readonly)

Returns the value of attribute pg.



5
6
7
# File 'lib/heroku_cli/pg/database.rb', line 5

def pg
  @pg
end

#url_namesObject (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

Returns:

  • (Boolean)


86
87
88
# File 'lib/heroku_cli/pg/database.rb', line 86

def available?
  status == 'Available'
end

#behind?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/heroku_cli/pg/database.rb', line 90

def behind?
  behind_by.positive?
end

#behind_byObject



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

#createdObject



41
42
43
# File 'lib/heroku_cli/pg/database.rb', line 41

def created
  info['Created']
end

#data_sizeObject



37
38
39
# File 'lib/heroku_cli/pg/database.rb', line 37

def data_size
  info['Data Size']
end

#follower?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/heroku_cli/pg/database.rb', line 82

def follower?
  info.key?('Following')
end

#followersObject



58
59
60
# File 'lib/heroku_cli/pg/database.rb', line 58

def followers
  info['Followers']
end

#followingObject



70
71
72
# File 'lib/heroku_cli/pg/database.rb', line 70

def following
  info['Following'].strip
end

#fork?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/heroku_cli/pg/database.rb', line 78

def fork?
  !(main? || follower?) && forked_from&.any?
end

#forked_fromObject



66
67
68
# File 'lib/heroku_cli/pg/database.rb', line 66

def forked_from
  info['Forked From']&.split(',')&.map(&:strip)
end

#forksObject



62
63
64
# File 'lib/heroku_cli/pg/database.rb', line 62

def forks
  info['Forks']&.split(',')&.map(&:strip)
end

#main?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/heroku_cli/pg/database.rb', line 74

def main?
  @url_names.include?('DATABASE_URL')
end

#nameObject



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

#planObject



33
34
35
# File 'lib/heroku_cli/pg/database.rb', line 33

def plan
  info['Plan'].gsub(' ', '-').downcase
end

#regionObject



98
99
100
# File 'lib/heroku_cli/pg/database.rb', line 98

def region
  info['Region']
end

#reloadObject



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_nameObject



102
103
104
# File 'lib/heroku_cli/pg/database.rb', line 102

def resource_name
  info['Add-on']
end

#statusObject



46
47
48
# File 'lib/heroku_cli/pg/database.rb', line 46

def status
  info['Status']
end

#tablesObject



50
51
52
# File 'lib/heroku_cli/pg/database.rb', line 50

def tables
  info['Tables'].match(/(\d+)/)[0]&.to_i || 0
end

#to_sObject



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_nameObject



17
18
19
# File 'lib/heroku_cli/pg/database.rb', line 17

def url_name
  @url_names.first
end

#versionObject



54
55
56
# File 'lib/heroku_cli/pg/database.rb', line 54

def version
  Gem::Version.new(info['PG Version'])
end