Skip to content

Commit

Permalink
Allow updating of settings through the gui
Browse files Browse the repository at this point in the history
  • Loading branch information
lexun committed Mar 1, 2015
1 parent f505a4a commit 98ade11
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/launchpad/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,29 @@ def initialize
@settings = YAML.load_file PATH
end

# Reads the given attribute from memory.
# @param option [Symbol] the attribute to be accessed from settings.
# @return [String] the value associated with the provided attribute.
def read(option)
settings[option.to_s].to_s
end

# Updates the given value in memory.
# @param option [Symbol] the attribute to be updated.
# @param value [String] the new value to be used.
# @return self
def update(option, value)
settings[option.to_s] = value
end

# Writes the current settings and their and values to disk as yaml.
# @return self
def save
File.open PATH, 'w' do |file|
file.write settings.to_yaml
end

self
end
end
end
5 changes: 5 additions & 0 deletions spec/fixtures/config/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
install: test/install
local_index_path: test/index
remote_index_uri: http://patcher.example.com/index
login_server: http://login.example.com/
port: 12345
34 changes: 34 additions & 0 deletions spec/lib/launchpad/settings_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
require 'spec_helper'

describe Launchpad::Settings do
let(:settings_fixture) { 'spec/fixtures/config/settings.yml' }
let(:settings_path) { 'spec/fixtures/config/current-settings.yml' }

before do
FileUtils.copy settings_fixture, settings_path
stub_const 'Launchpad::Settings::PATH', settings_path
end

after do
File.delete settings_path
end

describe '.read' do
it 'should access saved settings' do
expect(described_class.read :install)
.to eq 'test/install'

expect(described_class.read :login_server)
.to eq 'http://login.example.com/'
end
end

describe '.update' do
it 'should update a given settings value' do
expect { described_class.update :install, 'test/updated' }
.to change { described_class.read :install }
.from('test/install')
.to('test/updated')
end
end

describe '.save' do
before do
described_class.update :install, 'test/updated'
described_class.save
end

it 'should update the yaml file with new values' do
expect(described_class.clone.read :install)
.to eq 'test/updated'
end
end
end

0 comments on commit 98ade11

Please sign in to comment.