Class: Config
- Inherits:
-
Object
- Object
- Config
- Defined in:
- lib/config.rb
Instance Attribute Summary collapse
-
#database_url ⇒ Object
readonly
Returns the value of attribute database_url.
-
#dry_run ⇒ Object
readonly
Returns the value of attribute dry_run.
-
#files_location ⇒ Object
readonly
Returns the value of attribute files_location.
-
#if_backup ⇒ Object
readonly
Returns the value of attribute if_backup.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#org_id ⇒ Object
readonly
Returns the value of attribute org_id.
-
#repo_id ⇒ Object
readonly
Returns the value of attribute repo_id.
-
#threshold ⇒ Object
readonly
Returns the value of attribute threshold.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
- #abort_message(intro) ⇒ Object
- #argv_options ⇒ Object
- #check_values ⇒ Object
- #first_not_nil(*arr) ⇒ Object
-
#initialize(args = {}) ⇒ Config
constructor
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength.
- #set_values(args) ⇒ Object
- #yaml_load(url) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Config
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
7 8 9 10 |
# File 'lib/config.rb', line 7 def initialize(args={}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength set_values(args) check_values end |
Instance Attribute Details
#database_url ⇒ Object (readonly)
Returns the value of attribute database_url.
5 6 7 |
# File 'lib/config.rb', line 5 def database_url @database_url end |
#dry_run ⇒ Object (readonly)
Returns the value of attribute dry_run.
5 6 7 |
# File 'lib/config.rb', line 5 def dry_run @dry_run end |
#files_location ⇒ Object (readonly)
Returns the value of attribute files_location.
5 6 7 |
# File 'lib/config.rb', line 5 def files_location @files_location end |
#if_backup ⇒ Object (readonly)
Returns the value of attribute if_backup.
5 6 7 |
# File 'lib/config.rb', line 5 def if_backup @if_backup end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
5 6 7 |
# File 'lib/config.rb', line 5 def limit @limit end |
#org_id ⇒ Object (readonly)
Returns the value of attribute org_id.
5 6 7 |
# File 'lib/config.rb', line 5 def org_id @org_id end |
#repo_id ⇒ Object (readonly)
Returns the value of attribute repo_id.
5 6 7 |
# File 'lib/config.rb', line 5 def repo_id @repo_id end |
#threshold ⇒ Object (readonly)
Returns the value of attribute threshold.
5 6 7 |
# File 'lib/config.rb', line 5 def threshold @threshold end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
5 6 7 |
# File 'lib/config.rb', line 5 def user_id @user_id end |
Instance Method Details
#abort_message(intro) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/config.rb', line 89 def (intro) "\n#{intro} Example usage:\n"+ "\n $ bin/travis_backup 'postgres://my_database_url' --threshold 6\n" + "\nor using in code:\n" + "\n Backup.new(database_url: 'postgres://my_database_url', threshold: 6)\n" + "\nYou can also set it using environment variables or config/database.yml file.\n" end |
#argv_options ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/config.rb', line 97 def argv_copy = ARGV.clone = {} OptionParser.new do |opt| opt.on('-b', '--backup') { |o| [:if_backup] = o } opt.on('-d', '--dry_run') { |o| [:dry_run] = o } opt.on('-l', '--limit X') { |o| [:limit] = o.to_i } opt.on('-t', '--threshold X') { |o| [:threshold] = o.to_i } opt.on('-f', '--files_location X') { |o| [:files_location] = o } opt.on('-u', '--user_id X') { |o| [:user_id] = o.to_i } opt.on('-r', '--repo_id X') { |o| [:repo_id] = o.to_i } opt.on('-o', '--org_id X') { |o| [:org_id] = o.to_i } end.parse! [:database_url] = ARGV.shift if ARGV[0] argv_copy.each do |arg| ARGV.push(arg) end end |
#check_values ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/config.rb', line 76 def check_values if !@threshold = ("Please provide the threshold argument. Data younger than it will be omitted. " + "Threshold defines number of months from now.") abort end if !@database_url = ("Please provide proper database URL.") abort end end |
#first_not_nil(*arr) ⇒ Object
118 119 120 |
# File 'lib/config.rb', line 118 def first_not_nil(*arr) arr.compact.first end |
#set_values(args) ⇒ Object
12 13 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/config.rb', line 12 def set_values(args) config = yaml_load('config/settings.yml') connection_details = yaml_load('config/database.yml') argv_opts = @if_backup = first_not_nil( args[:if_backup], argv_opts[:if_backup], ENV['IF_BACKUP'], config.dig('backup', 'if_backup'), true ) @dry_run = first_not_nil( args[:dry_run], argv_opts[:dry_run], ENV['BACKUP_DRY_RUN'], config.dig('backup', 'dry_run'), false ) @limit = first_not_nil( args[:limit], argv_opts[:limit], ENV['BACKUP_LIMIT'], config.dig('backup', 'limit'), 1000 ) @threshold = first_not_nil( args[:threshold], argv_opts[:threshold], ENV['BACKUP_THRESHOLD'], config.dig('backup', 'threshold') ) @files_location = first_not_nil( args[:files_location], argv_opts[:files_location], ENV['BACKUP_BACKUP_FILES_LOCATION'], config.dig('backup', 'files_location'), './dump' ) @database_url = first_not_nil( args[:database_url], argv_opts[:database_url], ENV['DATABASE_URL'], connection_details.dig(ENV['RAILS_ENV']) ) @user_id = first_not_nil( args[:user_id], argv_opts[:user_id], ENV['USER_ID'], config.dig('backup', 'user_id') ) @repo_id = first_not_nil( args[:repo_id], argv_opts[:repo_id], ENV['REPO_ID'], config.dig('backup', 'repo_id') ) @org_id = first_not_nil( args[:org_id], argv_opts[:org_id], ENV['ORG_ID'], config.dig('backup', 'org_id') ) end |
#yaml_load(url) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/config.rb', line 122 def yaml_load(url) begin YAML.load(File.open(url)) rescue => e {} end end |