Class: Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/brewer/settings.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(testing = false) ⇒ Settings

Returns a new instance of Settings.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/brewer/settings.rb', line 8

def initialize(testing=false)

  @source = adaptibrew_dir('settings.py')
  @cache_file = brewer_dir('settings.yml')

  @settings = Hash.new

  if !testing
    Adaptibrew.new.clone

    unless cache?
      parse_and_cache
    else
      load_cached_settings
    end

    load_global
  end
end

Instance Attribute Details

#cache_fileObject (readonly)

Returns the value of attribute cache_file.



6
7
8
# File 'lib/brewer/settings.rb', line 6

def cache_file
  @cache_file
end

#settingsObject

Returns the value of attribute settings.



5
6
7
# File 'lib/brewer/settings.rb', line 5

def settings
  @settings
end

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'lib/brewer/settings.rb', line 6

def source
  @source
end

Instance Method Details

#add(setting) ⇒ Object

This will add a new element to the @settings hash AND re-cache the settings



78
79
80
81
82
83
84
# File 'lib/brewer/settings.rb', line 78

def add(setting)
  raise "Setting needs to be a hash" unless setting.is_a? Hash
  setting.each do |key, value|
    @settings[key] = value
  end
  cache
end

#cacheObject

Stores the currents @settings in settings.yml



87
88
89
90
91
92
93
94
95
96
# File 'lib/brewer/settings.rb', line 87

def cache
  create_cache
  store = YAML::Store.new @cache_file
  store.transaction {
    @settings.each do |k, v|
      store[k] = v
    end
  }
  true
end

#cache?Boolean

Checks if there are settings in the cache

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/brewer/settings.rb', line 46

def cache?
  if File.exists?(@cache_file) and File.readlines(@cache_file).grep(/DEBUG/).size > 0
    return true
  end
  false
end

#change(values) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/brewer/settings.rb', line 113

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_cacheObject

This deletes the cache file



99
100
101
102
103
104
# File 'lib/brewer/settings.rb', line 99

def clear_cache
  if File.exists?(@cache_file)
    FileUtils.rm_f @cache_file
  end
  true
end

#create_cacheObject

Creates the cache if there isn’t one already



69
70
71
72
73
74
# File 'lib/brewer/settings.rb', line 69

def create_cache
  unless File.exists?(@cache_file)
    File.open(@cache_file, 'w')
  end
  true
end

#load_cached_settingsObject

Loads cached settings If no cached settings, it returns false



37
38
39
40
41
42
43
# File 'lib/brewer/settings.rb', line 37

def load_cached_settings
  if cache?
    @settings = YAML.load(File.open(@cache_file))
    return true
  end
  false
end

#load_globalObject

This is so that the settings are easier to use in my code and for backwards compatability purposes



108
109
110
111
# File 'lib/brewer/settings.rb', line 108

def load_global
  parse_and_cache
  $settings = @settings
end

#parseObject

Parse the settings from the source file into @settings



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/brewer/settings.rb', line 54

def parse
  File.open(@source, 'r') do |file|
    file.each_line do |line|
      if line.include? "=" and line[0] != "#"
        key, value = line.match(/(.+)=(.+)/).captures
        @settings[key.strip.chomp] = value.strip.chomp
      end
    end
    type_cast
    return true
  end
  false
end

#parse_and_cacheObject

This will create the cache and populate it with settings from settings.py



30
31
32
33
# File 'lib/brewer/settings.rb', line 30

def parse_and_cache
  parse
  cache
end

#type_castObject

This method is r/badcode, i know



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/brewer/settings.rb', line 122

def type_cast
  # Super janky
  change({
    'rimsaddressint' => @settings['rimsaddressint'].to_i,
    'switchaddressint' => @settings['switchaddressint'].to_i,
    'baudrate' => @settings['baudrate'].to_i,
    'timeout' => @settings['timeout'].to_i,
    'spargeToMashRelay' => @settings['spargeToMashRelay'].to_i,
    'spargeRelay' => @settings['spargeRelay'].to_i,
    'rimsToMashRelay' => @settings['rimsToMashRelay'].to_i,
    'pumpRelay' => @settings['pumpRelay'].to_i,
    'DEBUG' => @settings['DEBUG'].to_b,
  })
end