pandarus 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/pandarus/model_base.rb +15 -8
- data/lib/pandarus/version.rb +1 -1
- metadata +1 -1
data/lib/pandarus/model_base.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'active_support'
|
2
|
-
|
2
|
+
require 'active_support/inflector'
|
3
|
+
require 'date'
|
4
|
+
|
3
5
|
module Pandarus
|
4
6
|
class ModelBase
|
5
7
|
def initialize(attributes = {})
|
@@ -12,15 +14,15 @@ module Pandarus
|
|
12
14
|
end
|
13
15
|
end
|
14
16
|
end
|
15
|
-
|
17
|
+
|
16
18
|
def attr(name)
|
17
19
|
self.class.attribute_map[name.to_sym]
|
18
20
|
end
|
19
|
-
|
21
|
+
|
20
22
|
def has_attr?(name)
|
21
23
|
self.class.attribute_map.has_key? name.to_sym
|
22
24
|
end
|
23
|
-
|
25
|
+
|
24
26
|
def inspect
|
25
27
|
Hash[
|
26
28
|
self.class.attribute_map.keys.map do |key|
|
@@ -28,19 +30,24 @@ module Pandarus
|
|
28
30
|
end
|
29
31
|
].inspect.sub(/^\{/,"<#{self.class} ").sub(/\}$/,'>')
|
30
32
|
end
|
31
|
-
|
33
|
+
|
32
34
|
def assign(attr_name, value)
|
33
35
|
props = attr(attr_name)
|
34
36
|
if props[:type]
|
35
37
|
if props[:container]
|
36
38
|
value = value.map{ |v| props[:type].constantize.new(v) }
|
37
|
-
|
38
|
-
|
39
|
+
elsif !value.nil?
|
40
|
+
klass = props[:type].constantize # e.g. "Date"
|
41
|
+
if klass.respond_to?(:parse)
|
42
|
+
value = klass.parse(value)
|
43
|
+
else
|
44
|
+
value = klass.new(value)
|
45
|
+
end
|
39
46
|
end
|
40
47
|
end
|
41
48
|
instance_variable_set("@#{attr_name}", value)
|
42
49
|
end
|
43
|
-
|
50
|
+
|
44
51
|
def to_body
|
45
52
|
body = {}
|
46
53
|
self.class.attribute_map.each_pair do |key, props|
|
data/lib/pandarus/version.rb
CHANGED