Mailinglist Archive: yast-commit (1771 mails)

< Previous Next >
[yast-commit] <rest-service> network : - dont read the config file when the model is defined
  • From: Duncan Mac-Vicar P <dmacvicar@xxxxxxx>
  • Date: Fri, 21 Aug 2009 13:39:07 +0200
  • Message-id: <E1MiTqH-0006YO-2o@xxxxxxxxxxxxxxxx>
ref: refs/heads/network
commit 2077feb37186455b82bd7bb7cfaa8decd33c6ca0
Author: Duncan Mac-Vicar P <dmacvicar@xxxxxxx>
Date: Fri Aug 21 13:38:30 2009 +0200

- dont read the config file when the model is defined
- better shared data usage in the classes
---
webservice/app/models/vendor_setting.rb | 3 +-
webservice/lib/yast/config_file.rb | 29 ++++++++++----
webservice/lib/yast/settings_model.rb | 44 ++++++++++-----------
webservice/test/unit/vendor_setting_test.rb | 28 ++++++++-----
webservice/test/unit/yast/config_file_test.rb | 10 +++-
webservice/test/unit/yast/settings_model_test.rb | 28 +++++++------
6 files changed, 82 insertions(+), 60 deletions(-)

diff --git a/webservice/app/models/vendor_setting.rb
b/webservice/app/models/vendor_setting.rb
index 2ed2181..397978a 100644
--- a/webservice/app/models/vendor_setting.rb
+++ b/webservice/app/models/vendor_setting.rb
@@ -17,6 +17,5 @@ require 'yast/settings_model'
# => text
#
class VendorSetting < YaST::SettingsModel
-
- config_name :vendor
+ self.config_name = :vendor
end
diff --git a/webservice/lib/yast/config_file.rb
b/webservice/lib/yast/config_file.rb
index 92f72fb..d7ae388 100644
--- a/webservice/lib/yast/config_file.rb
+++ b/webservice/lib/yast/config_file.rb
@@ -42,22 +42,33 @@ module YaST
#
# otherwise the given path is used
def initialize(name)
+ @data = {}
@file_name = ConfigFile.resolve_file_name(name)
- load_file(@file_name)
+ @loaded = false
end

+ def load_if_needed
+ if not @loaded
+ load_file(@file_name)
+ @loaded = true
+ end
+ end
+
# access a key in the configuration
def [](key)
+ load_if_needed
@data[key]
end

# modifies a key in the configuration
def []=(key, val)
+ load_if_needed
@data[key] = val
end

# iterate over config keys and values
def each
+ load_if_needed
@data.each do |key, val|
yield(key, val)
end
@@ -65,38 +76,40 @@ module YaST

# true if configuration has a given key
def has_key?(name)
+ load_if_needed
@data.has_key?(name)
end

# the file path where this config is operating on
+ # note, this file may not exist at all
def path
@file_name
end

# returns the xml representation if available
def to_xml(options = {})
+ load_if_needed
@data.to_xml(options)
end

def to_json
+ load_if_needed
@data.to_json
end

- # resolves the file name
- # and throws NotFoundError if
- # it can't be resolved
+ # resolves the file name or nil
+ # if it can be resolved
def self.resolve_file_name(name)
file_name = name
if name.is_a?(Symbol)
file_name = File.join(config_default_location, "#{name}.yml")
end
- # simple case first
- raise NotFoundError if not File.exist?(file_name)
file_name
end

# returns the file content
def self.read_file(file_name)
+ raise NotFoundError if not File.exist?(file_name)
File.open(file_name, 'r').read
end

@@ -107,13 +120,13 @@ module YaST

# loads data from a file and returns
# the data
- def self.load_file(file_name)
+ def self.load_file(file_name)
YAML::load(read_file(file_name))
end

# loads data from a file
def load_file(file_name)
- @data = ConfigFile.load_file(file_name)
+ @data.merge!(ConfigFile.load_file(file_name))
end

# access the location constant
diff --git a/webservice/lib/yast/settings_model.rb
b/webservice/lib/yast/settings_model.rb
index a5f15f1..4c30c01 100644
--- a/webservice/lib/yast/settings_model.rb
+++ b/webservice/lib/yast/settings_model.rb
@@ -11,20 +11,28 @@ module YaST
# find the configuration
class SettingsModel

+ class << self
+ attr_accessor :config
+ end
+
+ def self.config_name
+ end
+
+ def self.config_name=(name)
+ self.config = YaST::ConfigFile.new(name)
+ end
+
# initialize a model instance
def initialize(name)
- SettingsModel.init
@name = name
end

def self.path
- SettingsModel.init
- @@config.path
+ self.config.path
end

# find instances of the model
def self.find(what)
- SettingsModel.init
ret = nil
ret = case what
when :all then find_all
@@ -34,7 +42,8 @@ module YaST

def self.find_all
ret = []
- @@config.each do |key,val|
+ return ret if self.config.nil?
+ self.config.each do |key,val|
ret << self.new(key)
end
ret
@@ -42,22 +51,12 @@ module YaST

def self.find_one(id)
ret = nil
- if @@config.has_key?(id.to_s)
+ if self.config.has_key?(id.to_s)
ret = self.new(id.to_s)
end
ret
end

- def self.init
- if not defined?(@@config)
- @@config = YaST::ConfigFile.new(@@config_name)
- end
- end
-
- def self.config_name(name)
- @@config_name = name
- end
-
# setting id, alias for name
def id
name.to_s
@@ -69,25 +68,24 @@ module YaST
end

def value
- @@config[name.to_s]
+ self.class.config[name.to_s]
end

def self.method_missing(name)
- SettingsModel.init
# look if config has a key
- if @@config.has_key?(name.to_s)
- return @@config[name.to_s]
+ if self.config.has_key?(name.to_s)
+ return self.config[name.to_s]
end
raise NoMethodError.new("undefined method `#{name}' for
#{self.class.to_s}:Class")
end

def self.to_xml
tag_name = self.to_s.underscore
- @@config.to_xml(:root => tag_name)
+ self.config.to_xml(:root => tag_name)
end

def self.to_json
- @@config.to_json
+ self.config.to_json
end

def to_xml(options = {})
@@ -101,7 +99,7 @@ module YaST
end

def to_json
- @@config[name].to_json
+ self.class.config[name].to_json
end

end
diff --git a/webservice/test/unit/vendor_setting_test.rb
b/webservice/test/unit/vendor_setting_test.rb
index b57251f..2c29925 100644
--- a/webservice/test/unit/vendor_setting_test.rb
+++ b/webservice/test/unit/vendor_setting_test.rb
@@ -1,12 +1,16 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'vendor_setting'
+require 'pp'

def test_data(name)
File.join(File.dirname(__FILE__), 'yast', 'data', name)
end

-# example config file
-$config_data =<<EOF
+class VendorSettingTest < ActiveSupport::TestCase
+
+ def setup
+ # example config file
+ @config_data =<<EOF
bug_url: http://www.bugs.com
packages:
- mydb-server
@@ -20,23 +24,25 @@ eula:
clicking it
EOF

-class VendorSettingTest < ActiveSupport::TestCase
-
- def setup
-
YaST::ConfigFile.stubs(:resolve_file_name).with(:vendor).returns('/foo/vendor.yml')
-
YaST::ConfigFile.stubs(:read_file).with('/foo/vendor.yml').returns($config_data)
+
YaST::ConfigFile.stubs(:read_file).with('/etc/YaST2/vendor.yml').returns(@config_data)
end

+ def teardown
+ end
+
def test_config
settings = VendorSetting.find(:all)
- # look for one setting
- setting = settings.find{ |x| x.name == 'packages'}
- assert_instance_of(Array, setting.value)
- assert_equal("packages", setting.name)
+
assert_equal(4, settings.size)
+
assert_equal("http://www.bugs.com";, VendorSetting.bug_url)
assert_instance_of(Array, VendorSetting.services)
assert_equal(2, VendorSetting.services.size)
+
+ # look for one setting
+ setting = VendorSetting.find('packages')
+ assert_instance_of(Array, setting.value)
+ assert_equal("packages", setting.name)
end

end
diff --git a/webservice/test/unit/yast/config_file_test.rb
b/webservice/test/unit/yast/config_file_test.rb
index ad16683..7132b19 100644
--- a/webservice/test/unit/yast/config_file_test.rb
+++ b/webservice/test/unit/yast/config_file_test.rb
@@ -19,13 +19,17 @@ class ConfigFileTest < ActiveSupport::TestCase
assert_equal("This is an evil eula that will make you think twice before
clicking it", config['appliance']['eula'])

# now try to load an non-existing resource
- assert_raise YaST::ConfigFile::NotFoundError do
- YaST::ConfigFile.new(:whatever)
+ # this should be ok
+ config = nil
+ assert_nothing_raised do
+ config = YaST::ConfigFile.new(:whatever)
end
+ # and the path should be pointed to the right file
+ assert_equal(test_data('config/whatever.yml'), config.path)

# now try to load an non-existing file
assert_raise YaST::ConfigFile::NotFoundError do
- YaST::ConfigFile.new(test_data('config/whatever.yml'))
+ YaST::ConfigFile.load_file(test_data('config/whatever.yml'))
end

#assert_instance_of Hash, config
diff --git a/webservice/test/unit/yast/settings_model_test.rb
b/webservice/test/unit/yast/settings_model_test.rb
index cd220f3..9eea8f8 100644
--- a/webservice/test/unit/yast/settings_model_test.rb
+++ b/webservice/test/unit/yast/settings_model_test.rb
@@ -6,8 +6,16 @@ def test_data(name)
File.join(File.dirname(__FILE__), "data", name)
end

-# example config file
-$config_data =<<EOF
+# example setting model
+class MonitorSetting < YaST::SettingsModel
+ self.config_name = :monitor
+end
+
+class SettingsModelTest < ActiveSupport::TestCase
+
+ def setup
+ # example config file
+ @config_data =<<EOF
height: 100
width: 300
frequency: 40
@@ -17,20 +25,14 @@ outputs:
- analog
EOF

-# example setting model
-class MonitorSetting < YaST::SettingsModel
- config_name :monitor
-end
-
-class SettingsModelTest < ActiveSupport::TestCase
-
- def setup
-
YaST::ConfigFile.stubs(:resolve_file_name).with(:monitor).returns('/foo/monitor.yml')
-
YaST::ConfigFile.stubs(:read_file).with('/foo/monitor.yml').returns($config_data)
+
YaST::ConfigFile.stubs(:read_file).with('/etc/YaST2/monitor.yml').returns(@config_data)
end

+ def teardown
+ end
+
def test_model
- assert_equal('/foo/monitor.yml', MonitorSetting.path)
+ assert_equal('/etc/YaST2/monitor.yml', MonitorSetting.path)
settings = MonitorSetting.find(:all)
assert_equal(5, settings.size)

--
To unsubscribe, e-mail: yast-commit+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-commit+help@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages