Mailinglist Archive: yast-commit (1771 mails)

< Previous Next >
[yast-commit] <rest-service> network : various fixes and improve xml generation
  • From: Duncan Mac-Vicar P <dmacvicar@xxxxxxx>
  • Date: Fri, 21 Aug 2009 13:39:07 +0200
  • Message-id: <E1MiTqG-0006Y4-RR@xxxxxxxxxxxxxxxx>
ref: refs/heads/network
commit adbcd48f80aeda5bbd451c477b9c9fcc40d6fe80
Author: Duncan Mac-Vicar P <dmacvicar@xxxxxxx>
Date: Wed Aug 19 18:12:03 2009 +0200

various fixes and improve xml generation
---
webservice/app/models/vendor_setting.rb | 1 -
webservice/config/routes.rb | 4 +-
webservice/lib/yast/config_file.rb | 8 ++-
webservice/lib/yast/settings_model.rb | 61 ++++++++++++++++-----
webservice/test/unit/yast/settings_model_test.rb | 42 +++++++++++++++-
5 files changed, 96 insertions(+), 20 deletions(-)

diff --git a/webservice/app/models/vendor_setting.rb
b/webservice/app/models/vendor_setting.rb
index 950d08e..2ed2181 100644
--- a/webservice/app/models/vendor_setting.rb
+++ b/webservice/app/models/vendor_setting.rb
@@ -19,5 +19,4 @@ require 'yast/settings_model'
class VendorSetting < YaST::SettingsModel

config_name :vendor
-
end
diff --git a/webservice/config/routes.rb b/webservice/config/routes.rb
index 94ac35a..be16af4 100644
--- a/webservice/config/routes.rb
+++ b/webservice/config/routes.rb
@@ -2,7 +2,9 @@
ActionController::Routing::Routes.draw do |map|
map.resource :session
map.resources :resources
- map.resources :permissions, :requirements => {:id => /.*/}
+ map.resources :permissions, :requirements => {:id => /.*/}
+ map.resources :vendor_settings
+
# login uses POST for both
map.login "/login.:format", :controller => 'sessions', :action => 'create'
map.logout "/logout.:format", :controller => 'sessions', :action => 'destroy'
diff --git a/webservice/lib/yast/config_file.rb
b/webservice/lib/yast/config_file.rb
index 391f7ee..92f72fb 100644
--- a/webservice/lib/yast/config_file.rb
+++ b/webservice/lib/yast/config_file.rb
@@ -74,8 +74,12 @@ module YaST
end

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

# resolves the file name
diff --git a/webservice/lib/yast/settings_model.rb
b/webservice/lib/yast/settings_model.rb
index 126e331..a5f15f1 100644
--- a/webservice/lib/yast/settings_model.rb
+++ b/webservice/lib/yast/settings_model.rb
@@ -12,10 +12,9 @@ module YaST
class SettingsModel

# initialize a model instance
- def initialize(name, value)
+ def initialize(name)
SettingsModel.init
@name = name
- @value = value
end

def self.path
@@ -27,15 +26,28 @@ module YaST
def self.find(what)
SettingsModel.init
ret = nil
- if what == :all
- ret = []
- @@config.each do |key,val|
- ret << self.new(key, val)
- end
+ ret = case what
+ when :all then find_all
+ else find_one(what)
end
- ret
end

+ def self.find_all
+ ret = []
+ @@config.each do |key,val|
+ ret << self.new(key)
+ end
+ ret
+ end
+
+ def self.find_one(id)
+ ret = nil
+ if @@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)
@@ -48,16 +60,16 @@ module YaST

# setting id, alias for name
def id
- name
+ name.to_s
end

# setting name
def name
- @name
+ @name.to_s
end

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

def self.method_missing(name)
@@ -68,11 +80,30 @@ module YaST
end
raise NoMethodError.new("undefined method `#{name}' for
#{self.class.to_s}:Class")
end
-
- def to_xml
- tag_name = self.class.to_s.underscore
- { :name => name, :value => value }.to_xml(:root => tag_name)
+
+ def self.to_xml
+ tag_name = self.to_s.underscore
+ @@config.to_xml(:root => tag_name)
+ end
+
+ def self.to_json
+ @@config.to_json
+ end
+
+ def to_xml(options = {})
+ fixed_value = value
+ # quick fix in case the value is an array
+ if value.is_a?(Array)
+ # convert something like [a,b,c] into [{ :foo => a }, { :foo => b },
...]
+ fixed_value = value.map { |x| { name.singularize => x } }
+ end
+ { :name => name, :value => fixed_value }.to_xml({:root =>
self.class.to_s.underscore}.merge(options))
end
+
+ def to_json
+ @@config[name].to_json
+ end
+
end

end
diff --git a/webservice/test/unit/yast/settings_model_test.rb
b/webservice/test/unit/yast/settings_model_test.rb
index e7b8f48..cd220f3 100644
--- a/webservice/test/unit/yast/settings_model_test.rb
+++ b/webservice/test/unit/yast/settings_model_test.rb
@@ -38,7 +38,8 @@ class SettingsModelTest < ActiveSupport::TestCase
assert_equal(300, MonitorSetting.width)
assert_instance_of(Array, MonitorSetting.outputs)

- setting = settings.find{ |x| x.name == "height" }
+ setting = MonitorSetting.find(:height)
+ assert_equal(100, setting.value)

xml = <<DONE
<?xml version="1.0" encoding="UTF-8"?>
@@ -48,6 +49,45 @@ class SettingsModelTest < ActiveSupport::TestCase
</monitor-setting>
DONE
assert_equal(xml, setting.to_xml)
+
+ assert_equal("100", setting.to_json)
+
+xml = <<END
+<?xml version="1.0" encoding="UTF-8"?>
+<monitor-settings type="array">
+ <monitor-setting>
+ <value>vga</value>
+ <name>mode</name>
+ </monitor-setting>
+ <monitor-setting>
+ <value type="integer">100</value>
+ <name>height</name>
+ </monitor-setting>
+ <monitor-setting>
+ <value type="integer">40</value>
+ <name>frequency</name>
+ </monitor-setting>
+ <monitor-setting>
+ <value type="array">
+ <value>
+ <output>dvi</output>
+ </value>
+ <value>
+ <output>analog</output>
+ </value>
+ </value>
+ <name>outputs</name>
+ </monitor-setting>
+ <monitor-setting>
+ <value type="integer">300</value>
+ <name>width</name>
+ </monitor-setting>
+</monitor-settings>
+END
+
+ assert_equal(xml, settings.to_xml)
+
+ #assert_equal("d", settings.to_json)

end

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

< Previous Next >
This Thread
  • No further messages