Class: LedgerSync::Util::DotenvUpdator
- Inherits:
-
Object
- Object
- LedgerSync::Util::DotenvUpdator
- Defined in:
- lib/ledger_sync/util/dotenv_updator.rb
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ DotenvUpdator
constructor
A new instance of DotenvUpdator.
-
#update(args = {}) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity.
Constructor Details
#initialize(args = {}) ⇒ DotenvUpdator
Returns a new instance of DotenvUpdator.
10 11 12 |
# File 'lib/ledger_sync/util/dotenv_updator.rb', line 10 def initialize(args = {}) @file_path = args.fetch(:file_path, File.join(Dir.pwd, '.env.local')) end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
8 9 10 |
# File 'lib/ledger_sync/util/dotenv_updator.rb', line 8 def file_path @file_path end |
Instance Method Details
#update(args = {}) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ledger_sync/util/dotenv_updator.rb', line 14 def update(args = {}) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity client = args.fetch(:client) prefix = args.fetch(:prefix, "#{client.class.config.root_key.upcase}_") to_save = client.ledger_attributes_to_save.dup.stringify_keys Tempfile.open(".#{File.basename(file_path)}", File.dirname(file_path)) do |tempfile| if File.file?(file_path) File.open(file_path).each do |line| env_key = line.split('=').first client_method = env_key.split(prefix).last.downcase if line =~ /\A#{prefix}/ && to_save.key?(client_method) env_value = ENV[env_key] new_value = to_save.delete(client_method) tempfile.puts "#{env_key}=#{new_value}" next if env_value == new_value.to_s ENV[env_key] = new_value.to_s tempfile.puts "# #{env_key}=#{env_value} # Updated on #{Time.now}" else tempfile.puts line end end end to_save.each { |k, v| tempfile.puts(["#{prefix}#{k}".upcase, v].map(&:to_s).join('=')) } tempfile.close FileUtils.mv tempfile.path, file_path end Dotenv.load end |