Commit 0b1977c2a17e7e5555a52f7340bfddcd8c1c05a5

Authored by Rafael Manzo
Committed by João M. M. da Silva
1 parent 3bfcb826

[Mezuro] Kalibro exception classes created

plugins/mezuro/lib/kalibro/errors/record_not_found.rb 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +class Kalibro::Errors::RecordNotFound < Kalibro::Errors::Standard
  2 +end
0 3 \ No newline at end of file
... ...
plugins/mezuro/lib/kalibro/errors/standard.rb 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +#Inspired on:
  2 +#https://github.com/rails/rails/blob/master/activerecord/lib/active_record/errors.rb
  3 +class Kalibro::Errors::Standard < StandardError
  4 +end
0 5 \ No newline at end of file
... ...
plugins/mezuro/lib/kalibro/model.rb
... ... @@ -40,8 +40,8 @@ class Kalibro::Model
40 40  
41 41 def self.to_object value
42 42 value.kind_of?(Hash) ? new(value) : value
43   - end
44   -
  43 + end
  44 +
45 45 def self.create(attributes={})
46 46 new_model = new attributes
47 47 new_model.save
... ... @@ -52,7 +52,7 @@ class Kalibro::Model
52 52 if(exists?(id))
53 53 new request(find_action, id_params(id))["#{class_name.underscore}".to_sym]
54 54 else
55   - nil
  55 + raise Errors::RecordNotFound
56 56 end
57 57 end
58 58  
... ... @@ -77,7 +77,7 @@ class Kalibro::Model
77 77 def self.exists?(id)
78 78 request(exists_action, id_params(id))[:exists]
79 79 end
80   -
  80 +
81 81 protected
82 82  
83 83 def fields
... ... @@ -88,7 +88,7 @@ class Kalibro::Model
88 88 return value if value.nil?
89 89 return value.collect { |element| convert_to_hash(element) } if value.is_a?(Array)
90 90 return value.to_hash if value.is_a?(Kalibro::Model)
91   - return self.class.date_with_milliseconds(value) if value.is_a?(DateTime)
  91 + return self.class.date_with_milliseconds(value) if value.is_a?(DateTime)
92 92 return 'INF' if value.is_a?(Float) and value.infinite? == 1
93 93 return '-INF' if value.is_a?(Float) and value.infinite? == -1
94 94 value
... ... @@ -109,32 +109,32 @@ class Kalibro::Model
109 109 def self.is_valid?(field)
110 110 field.to_s[0] != '@' and field != :attributes! and (field.to_s =~ /xsi/).nil?
111 111 end
112   -
  112 +
113 113 def self.date_with_milliseconds(date)
114 114 milliseconds = "." + (date.sec_fraction * 60 * 60 * 24 * 1000).to_s
115 115 date.to_s[0..18] + milliseconds + date.to_s[19..-1]
116 116 end
117   -
  117 +
118 118 def instance_class_name
119 119 self.class.name.gsub(/Kalibro::/,"")
120 120 end
121   -
  121 +
122 122 def self.endpoint
123 123 class_name
124 124 end
125   -
  125 +
126 126 def save_action
127 127 "save_#{instance_class_name.underscore}".to_sym
128 128 end
129   -
  129 +
130 130 def save_params
131 131 {instance_class_name.underscore.to_sym => self.to_hash}
132 132 end
133   -
  133 +
134 134 def destroy_action
135 135 "delete_#{instance_class_name.underscore}".to_sym
136 136 end
137   -
  137 +
138 138 def destroy_params
139 139 {"#{instance_class_name.underscore}_id".to_sym => self.id}
140 140 end
... ... @@ -142,11 +142,11 @@ class Kalibro::Model
142 142 def self.class_name
143 143 self.name.gsub(/Kalibro::/,"")
144 144 end
145   -
  145 +
146 146 def self.exists_action
147 147 "#{class_name.underscore}_exists".to_sym
148 148 end
149   -
  149 +
150 150 def self.id_params(id)
151 151 {"#{class_name.underscore}_id".to_sym => id}
152 152 end
... ...