Class: Brewer::Settings
- Inherits:
-
Object
- Object
- Brewer::Settings
- Defined in:
- lib/brewer/settings.rb
Instance Attribute Summary collapse
-
#cache_file ⇒ Object
readonly
Returns the value of attribute cache_file.
-
#settings ⇒ Object
Returns the value of attribute settings.
-
#source_file ⇒ Object
readonly
Returns the value of attribute source_file.
Instance Method Summary collapse
-
#add(setting) ⇒ Object
This will add a new element to the @settings hash AND re-cache the settings.
-
#cache ⇒ Object
Stores the currents @settings in settings.yml.
-
#cache? ⇒ Boolean
Checks if there are settings in the cache.
- #change(values) ⇒ Object
-
#clear_cache ⇒ Object
This deletes the cache file.
-
#create_cache ⇒ Object
Creates the cache if there isn’t one already.
-
#initialize(source_file: false, cache_file: false) ⇒ Settings
constructor
A new instance of Settings.
-
#load_cached_settings ⇒ Object
Loads cached settings If no cached settings, it returns false.
-
#load_global ⇒ Object
This is so that the settings are easier to use in my code and for backwards compatability purposes.
-
#parse ⇒ Object
Parse the settings from @source_file into @settings.
Constructor Details
#initialize(source_file: false, cache_file: false) ⇒ Settings
Returns a new instance of Settings.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/brewer/settings.rb', line 7 def initialize(source_file: false, cache_file: false) @settings = {} @source_file = source_file ? adaptibrew_dir(source_file) : adaptibrew_dir("print_settings.py") @cache_file = cache_file ? brewer_dir(cache_file) : brewer_dir("settings.yml") if cache? load_cached_settings else parse cache end load_global end |
Instance Attribute Details
#cache_file ⇒ Object (readonly)
Returns the value of attribute cache_file.
5 6 7 |
# File 'lib/brewer/settings.rb', line 5 def cache_file @cache_file end |
#settings ⇒ Object
Returns the value of attribute settings.
4 5 6 |
# File 'lib/brewer/settings.rb', line 4 def settings @settings end |
#source_file ⇒ Object (readonly)
Returns the value of attribute source_file.
5 6 7 |
# File 'lib/brewer/settings.rb', line 5 def source_file @source_file end |
Instance Method Details
#add(setting) ⇒ Object
This will add a new element to the @settings hash AND re-cache the settings
66 67 68 69 70 71 72 |
# File 'lib/brewer/settings.rb', line 66 def add(setting) raise "Setting needs to be a hash" unless setting.is_a? Hash setting.each do |key, value| @settings[key] = value end true end |
#cache ⇒ Object
Stores the currents @settings in settings.yml
48 49 50 51 52 |
# File 'lib/brewer/settings.rb', line 48 def cache raise "Settings cache could not be created. Check permissions on ~/.brewer/" unless create_cache store(@settings) true end |
#cache? ⇒ Boolean
Checks if there are settings in the cache
24 25 26 27 28 29 |
# File 'lib/brewer/settings.rb', line 24 def cache? if File.exists?(@cache_file) and File.readlines(@cache_file).grep(/DEBUG/).size > 0 return true end false end |
#change(values) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/brewer/settings.rb', line 90 def change(values) raise "Values to change must be a hash" unless values.is_a? Hash values.each do |k, v| @settings[k] = v end return true end |
#clear_cache ⇒ Object
This deletes the cache file
75 76 77 78 79 80 |
# File 'lib/brewer/settings.rb', line 75 def clear_cache if File.exists?(@cache_file) FileUtils.rm_f @cache_file end true end |
#create_cache ⇒ Object
Creates the cache if there isn’t one already
42 43 44 45 |
# File 'lib/brewer/settings.rb', line 42 def create_cache File.open(@cache_file, 'w') true end |
#load_cached_settings ⇒ Object
Loads cached settings If no cached settings, it returns false
56 57 58 59 60 61 62 |
# File 'lib/brewer/settings.rb', line 56 def load_cached_settings if cache? @settings = YAML.load(File.open(@cache_file)) return true end false end |
#load_global ⇒ Object
This is so that the settings are easier to use in my code and for backwards compatability purposes
84 85 86 87 88 |
# File 'lib/brewer/settings.rb', line 84 def load_global raise "settings instance variable does not exist yet. Run Settings#parse first" unless !@settings.empty? type_cast $settings = @settings end |
#parse ⇒ Object
Parse the settings from @source_file into @settings
32 33 34 35 36 37 38 39 |
# File 'lib/brewer/settings.rb', line 32 def parse settings_file_output = `python #{@source_file}`.chomp settings_file_output.split("\n").each do |setting| key, value = setting.match(/(.+)=(.+)/).captures @settings[key.strip.chomp] = value.strip.chomp end true end |