Class: HerokuConfigVars::HerokuApp

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
app/models/heroku_config_vars/heroku_app.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) {|_self| ... } ⇒ HerokuApp

Returns a new instance of HerokuApp.

Yields:

  • (_self)

Yield Parameters:



29
30
31
32
33
34
35
36
37
# File 'app/models/heroku_config_vars/heroku_app.rb', line 29

def initialize(attrs={})
  @vars = {}
  @current_vars = {}
  @loaded = false

  self.attributes = attrs

  yield self if block_given?
end

Instance Attribute Details

#current_varsObject (readonly)

Returns the value of attribute current_vars.



10
11
12
# File 'app/models/heroku_config_vars/heroku_app.rb', line 10

def current_vars
  @current_vars
end

#loadedObject (readonly) Also known as: loaded?, persisted?

Returns the value of attribute loaded.



9
10
11
# File 'app/models/heroku_config_vars/heroku_app.rb', line 9

def loaded
  @loaded
end

#varsObject

Returns the value of attribute vars.



7
8
9
# File 'app/models/heroku_config_vars/heroku_app.rb', line 7

def vars
  @vars
end

Class Method Details

.findObject



18
19
20
21
22
23
24
25
26
27
# File 'app/models/heroku_config_vars/heroku_app.rb', line 18

def self.find
  attrs = {
    app_name: ENV['HEROKU_APP_NAME'],
    api_key:  ENV['HEROKU_API_KEY']
  }

  new(attrs) do |app|
    app.load_vars if app.valid?
  end
end

Instance Method Details

#added_varsObject



82
83
84
# File 'app/models/heroku_config_vars/heroku_app.rb', line 82

def added_vars
  vars.except *current_vars.keys
end

#api_keyObject



53
54
55
# File 'app/models/heroku_config_vars/heroku_app.rb', line 53

def api_key
  @api_key ||= @vars['HEROKU_API_KEY'].presence
end

#api_key=(value) ⇒ Object



57
58
59
# File 'app/models/heroku_config_vars/heroku_app.rb', line 57

def api_key=(value)
  @api_key = @vars['HEROKU_API_KEY'] = value
end

#app_nameObject



45
46
47
# File 'app/models/heroku_config_vars/heroku_app.rb', line 45

def app_name
  @app_name ||= @vars['HEROKU_APP_NAME'].presence
end

#app_name=(value) ⇒ Object



49
50
51
# File 'app/models/heroku_config_vars/heroku_app.rb', line 49

def app_name=(value)
  @app_name = @vars['HEROKU_APP_NAME'] = value
end

#attributes=(attrs = {}) ⇒ Object



39
40
41
42
43
# File 'app/models/heroku_config_vars/heroku_app.rb', line 39

def attributes=(attrs={})
  attrs.each do |key, value|
    self.send "#{key}=", value
  end
end

#changesObject



98
99
100
101
102
# File 'app/models/heroku_config_vars/heroku_app.rb', line 98

def changes
  @changes ||= vars.dup.
    delete_if { |k, v| current_vars[k] == v }.
    merge!(current_vars.dup.delete_if { |k, v| vars.has_key?(k) })
end

#idObject



61
62
63
# File 'app/models/heroku_config_vars/heroku_app.rb', line 61

def id
  app_name
end

#kept_varsObject



86
87
88
# File 'app/models/heroku_config_vars/heroku_app.rb', line 86

def kept_vars
  current_vars.slice *vars.keys
end

#load_varsObject



65
66
67
68
69
70
71
72
# File 'app/models/heroku_config_vars/heroku_app.rb', line 65

def load_vars
  catch_heroku_errors do
    Rails.logger.debug 'Loading config'
    @vars = connection.get_config_vars(app_name).body
    @current_vars = @vars.dup.freeze
    @loaded = true
  end
end

#removed_varsObject



78
79
80
# File 'app/models/heroku_config_vars/heroku_app.rb', line 78

def removed_vars
  current_vars ? current_vars.except(*vars.keys) : {}
end

#saveObject



74
75
76
# File 'app/models/heroku_config_vars/heroku_app.rb', line 74

def save
  save_to_heroku and save_to_env if valid?
end

#updated_and_added_varsObject



94
95
96
# File 'app/models/heroku_config_vars/heroku_app.rb', line 94

def updated_and_added_vars
  changes.slice(*vars.keys)
end

#updated_varsObject



90
91
92
# File 'app/models/heroku_config_vars/heroku_app.rb', line 90

def updated_vars
  changes.slice(*kept_vars.keys)
end