Class: S2D::Configuration::Repository
- Inherits:
-
Object
- Object
- S2D::Configuration::Repository
- Includes:
- Singleton
- Defined in:
- lib/s2d/repository.rb,
lib/s2d/configuration/repository.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #active_device ⇒ Object
- #active_device=(device) ⇒ Object
- #add_device(device, activate = false) ⇒ Object
- #device(key) ⇒ Object
- #devices ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Repository
constructor
A new instance of Repository.
- #load ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize ⇒ Repository
Returns a new instance of Repository.
11 12 13 14 |
# File 'lib/s2d/repository.rb', line 11 def initialize @path = File.join(File.('~'), FILE_NAME) @data = load end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/s2d/repository.rb', line 9 def path @path end |
Instance Method Details
#active_device ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/s2d/repository.rb', line 28 def active_device default = devices[:default] if default.nil? raise "No default device configured" else device(default) end end |
#active_device=(device) ⇒ Object
38 39 40 41 |
# File 'lib/s2d/repository.rb', line 38 def active_device=(device) devices[:default] = device.name.to_sym write end |
#add_device(device, activate = false) ⇒ Object
22 23 24 25 26 |
# File 'lib/s2d/repository.rb', line 22 def add_device(device, activate = false) @data[:devices][device.name.to_sym] = Mappers::DeviceMapper.to_hash(device) self.active_device = device if activate write end |
#device(key) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/s2d/configuration/repository.rb', line 26 def device(key) selected = @data[:devices][key] if selected.nil? nil else Mappers::DeviceMapper.to_object(selected) end end |
#devices ⇒ Object
22 23 24 |
# File 'lib/s2d/configuration/repository.rb', line 22 def devices @data[:devices].keys end |
#empty? ⇒ Boolean
43 44 45 |
# File 'lib/s2d/repository.rb', line 43 def empty? @data == default_structure end |
#load ⇒ Object
16 17 18 19 20 |
# File 'lib/s2d/repository.rb', line 16 def load YAML.load_file(@path) rescue Errno::ENOENT default_structure end |
#reset ⇒ Object
47 48 49 |
# File 'lib/s2d/repository.rb', line 47 def reset self.send(:initialize) end |