Commit 7e9306373b0958d87ba6750d4dbeda54787659b7

Authored by Alessandro Palmeira + Caio Salgado
Committed by Paulo Meireles
1 parent fb81a867

[Mezuro] Moved create and save methods to model.rb

plugins/mezuro/lib/kalibro/configuration.rb
@@ -22,20 +22,12 @@ class Kalibro::Configuration < Kalibro::Model @@ -22,20 +22,12 @@ class Kalibro::Configuration < Kalibro::Model
22 new request("Configuration", :get_configuration, {:configuration_name => configuration_name})[:configuration] 22 new request("Configuration", :get_configuration, {:configuration_name => configuration_name})[:configuration]
23 end 23 end
24 24
25 - def self.create(configuration_content)  
26 - new ({  
27 - :name => configuration_content.name,  
28 - :description => configuration_content.description  
29 - }).save  
30 - end  
31 -  
32 - def save  
33 - begin  
34 - self.class.request("Configuration", :save_configuration, {:configuration => to_hash})  
35 - true  
36 - rescue Exception => error  
37 - false  
38 - end 25 + def self.create(content)
  26 + attributes = {
  27 + :name => content.name,
  28 + :description => content.description
  29 + }
  30 + super attributes
39 end 31 end
40 32
41 def self.all_names 33 def self.all_names
plugins/mezuro/lib/kalibro/metric_configuration.rb
@@ -48,17 +48,6 @@ class Kalibro::MetricConfiguration < Kalibro::Model @@ -48,17 +48,6 @@ class Kalibro::MetricConfiguration < Kalibro::Model
48 metric_configuration 48 metric_configuration
49 end 49 end
50 50
51 - def save  
52 - begin  
53 - self.class.request("MetricConfiguration", :save_metric_configuration, {  
54 - :metric_configuration => to_hash,  
55 - :configuration_name => configuration_name})  
56 - true  
57 - rescue Exception => error  
58 - false  
59 - end  
60 - end  
61 -  
62 def destroy 51 def destroy
63 self.class.request("MetricConfiguration", :remove_metric_configuration, { 52 self.class.request("MetricConfiguration", :remove_metric_configuration, {
64 :configuration_name => configuration_name, 53 :configuration_name => configuration_name,
@@ -75,5 +64,9 @@ class Kalibro::MetricConfiguration < Kalibro::Model @@ -75,5 +64,9 @@ class Kalibro::MetricConfiguration < Kalibro::Model
75 def native?(value) 64 def native?(value)
76 value.has_key?(:origin) ? true : false 65 value.has_key?(:origin) ? true : false
77 end 66 end
78 - 67 +
  68 + def save_params
  69 + {:metric_configuration => to_hash, :configuration_name => configuration_name}
  70 + end
  71 +
79 end 72 end
plugins/mezuro/lib/kalibro/model.rb
@@ -35,6 +35,19 @@ class Kalibro::Model @@ -35,6 +35,19 @@ class Kalibro::Model
35 def self.to_object value 35 def self.to_object value
36 value.kind_of?(Hash) ? new(value) : value 36 value.kind_of?(Hash) ? new(value) : value
37 end 37 end
  38 +
  39 + def self.create(attributes={})
  40 + new(attributes).save
  41 + end
  42 +
  43 + def save
  44 + begin
  45 + self.class.request(save_endpoint, save_action, save_params)
  46 + true
  47 + rescue Exception => error
  48 + false
  49 + end
  50 + end
38 51
39 protected 52 protected
40 53
@@ -72,5 +85,20 @@ class Kalibro::Model @@ -72,5 +85,20 @@ class Kalibro::Model
72 milliseconds = "." + (date.sec_fraction * 60 * 60 * 24 * 1000).to_s 85 milliseconds = "." + (date.sec_fraction * 60 * 60 * 24 * 1000).to_s
73 date.to_s[0..18] + milliseconds + date.to_s[19..-1] 86 date.to_s[0..18] + milliseconds + date.to_s[19..-1]
74 end 87 end
75 - 88 +
  89 + def class_name
  90 + self.class.name.gsub(/Kalibro::/,"")
  91 + end
  92 +
  93 + def save_endpoint
  94 + class_name
  95 + end
  96 +
  97 + def save_action
  98 + "save_#{class_name.underscore}".to_sym
  99 + end
  100 +
  101 + def save_params
  102 + {class_name.underscore.to_sym => self.to_hash}
  103 + end
76 end 104 end
plugins/mezuro/lib/kalibro/project.rb
@@ -11,7 +11,7 @@ class Kalibro::Project < Kalibro::Model @@ -11,7 +11,7 @@ class Kalibro::Project < Kalibro::Model
11 end 11 end
12 12
13 def self.create(content) 13 def self.create(content)
14 - new({ 14 + attributes = {
15 :name => content.name, 15 :name => content.name,
16 :license => content.license, 16 :license => content.license,
17 :description => content.description, 17 :description => content.description,
@@ -20,22 +20,14 @@ class Kalibro::Project < Kalibro::Model @@ -20,22 +20,14 @@ class Kalibro::Project < Kalibro::Model
20 :address => content.repository_url 20 :address => content.repository_url
21 }, 21 },
22 :configuration_name => content.configuration_name 22 :configuration_name => content.configuration_name
23 - }).save 23 + }
  24 + super attributes
24 end 25 end
25 26
26 def destroy 27 def destroy
27 self.class.request("Project", :remove_project, {:project_name => name}) 28 self.class.request("Project", :remove_project, {:project_name => name})
28 end 29 end
29 30
30 - def save  
31 - begin  
32 - self.class.request("Project", :save_project, {:project => to_hash})  
33 - true  
34 - rescue Exception => error  
35 - false  
36 - end  
37 - end  
38 -  
39 def repository=(value) 31 def repository=(value)
40 @repository = Kalibro::Repository.to_object value 32 @repository = Kalibro::Repository.to_object value
41 end 33 end