Class: MultiMovingsign::Settings

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

Overview

Settings wrapper (reads/saves from/to YAML file via Settings.load / #dump)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Settings

Returns a new instance of Settings.



19
20
21
# File 'lib/multi_movingsign/settings.rb', line 19

def initialize(hash = {})
  self.mash = Hashie::Mash.new hash
end

Instance Attribute Details

#mashObject

Returns the value of attribute mash.



8
9
10
# File 'lib/multi_movingsign/settings.rb', line 8

def mash
  @mash
end

Class Method Details

.default_settings_pathObject

Default path for the settings YAML file



51
52
53
# File 'lib/multi_movingsign/settings.rb', line 51

def self.default_settings_path
  File.join(ENV['HOME'], '.multi_movingsign.yml')
end

.load(path) ⇒ Object

Constructs a new MultiMovingsign::Settings instance from settings saves in the specified YAML file



11
12
13
14
15
16
17
# File 'lib/multi_movingsign/settings.rb', line 11

def self.load(path)
  if File.exists? path
    self.new YAML.load(File.read(path))
  else
    self.new({})
  end
end

Instance Method Details

#dump(path) ⇒ Object

Serializes (dumps) the settings into the specified YAML file



44
45
46
47
48
# File 'lib/multi_movingsign/settings.rb', line 44

def dump(path)
  File.open(path, 'w') do |f|
    f.write(self.mash.to_hash.to_yaml)
  end
end

#sign_paths=(paths) ⇒ Object

Sets the list of conifgured MultiMovingsign::Signs via an array of serial port paths

Examples:

settings.sign_paths = ['/dev/ttyUSB0', '/dev/ttyUSB1']


39
40
41
# File 'lib/multi_movingsign/settings.rb', line 39

def sign_paths=(paths)
  self.mash.signs = paths.map { |path| {'path' => path} }
end

#signsObject

Returns an array of the configured MultiMovingsign::Signs



24
25
26
27
28
# File 'lib/multi_movingsign/settings.rb', line 24

def signs
  self.mash.signs ||= []

  self.mash.signs.map { |hash| Sign.load(hash) }
end

#signs?Boolean

true if any signs are configured

Returns:

  • (Boolean)


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

def signs?
  ! self.signs.empty?
end