Class: S2D::Configuration::Repository

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/s2d/repository.rb,
lib/s2d/configuration/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRepository

Returns a new instance of Repository.



11
12
13
14
# File 'lib/s2d/repository.rb', line 11

def initialize
  @path = File.join(File.expand_path('~'), FILE_NAME)
  @data = load
end

Instance Attribute Details

#pathObject (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_deviceObject



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

#devicesObject



22
23
24
# File 'lib/s2d/configuration/repository.rb', line 22

def devices
  @data[:devices].keys
end

#empty?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/s2d/repository.rb', line 43

def empty?
  @data == default_structure
end

#loadObject



16
17
18
19
20
# File 'lib/s2d/repository.rb', line 16

def load
  YAML.load_file(@path)
rescue Errno::ENOENT
  default_structure
end

#resetObject



47
48
49
# File 'lib/s2d/repository.rb', line 47

def reset
  self.send(:initialize)
end