yuki 0.1.1 → 0.1.2
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/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/yuki.rb +34 -20
- data/yuki.rb +1 -1
- metadata +2 -2
data/Rakefile
CHANGED
@@ -16,5 +16,5 @@ begin
|
|
16
16
|
end
|
17
17
|
Jeweler::GemcutterTasks.new
|
18
18
|
rescue LoadError
|
19
|
-
puts "Jeweler not available. Install it with: sudo gem install
|
20
|
-
end
|
19
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
20
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/lib/yuki.rb
CHANGED
@@ -14,7 +14,9 @@ module Yuki
|
|
14
14
|
def self.included(base)
|
15
15
|
base.send :include, InstanceMethods
|
16
16
|
base.class_eval { @store = nil }
|
17
|
-
base.
|
17
|
+
unless base.respond_to?(:__new__)
|
18
|
+
base.instance_eval { alias :__new__ :new }
|
19
|
+
end
|
18
20
|
base.extend ClassMethods
|
19
21
|
base.extend Validations
|
20
22
|
base.instance_eval {
|
@@ -22,7 +24,7 @@ module Yuki
|
|
22
24
|
has :pk
|
23
25
|
}
|
24
26
|
end
|
25
|
-
|
27
|
+
|
26
28
|
module Callbacks
|
27
29
|
def before_save(); end
|
28
30
|
def after_save(); end
|
@@ -53,11 +55,16 @@ module Yuki
|
|
53
55
|
# Redefines #new method in order to build the object
|
54
56
|
# from a hash. Assumes a constructor that takes a hash or a
|
55
57
|
# no-args constructor
|
56
|
-
def new(
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
def new(*args)
|
59
|
+
if(args.first.is_a? Hash)
|
60
|
+
hash = args.first
|
61
|
+
begin
|
62
|
+
__new__(*args).from_hash(hash)
|
63
|
+
rescue Exception => e
|
64
|
+
__new__.from_hash(hash)
|
65
|
+
end
|
66
|
+
else
|
67
|
+
super
|
61
68
|
end
|
62
69
|
end
|
63
70
|
|
@@ -89,14 +96,15 @@ module Yuki
|
|
89
96
|
# Gets an instance by key
|
90
97
|
def get(key)
|
91
98
|
val = db[key]
|
92
|
-
build(val)
|
99
|
+
build(val)
|
93
100
|
end
|
94
101
|
|
95
|
-
# Updates an instance by key the
|
102
|
+
# Updates an instance by key the
|
103
|
+
# the given attrs hash
|
96
104
|
def put(key, attrs)
|
97
105
|
db[key] = attrs
|
98
106
|
val = db[key]
|
99
|
-
build(val)
|
107
|
+
build(val)
|
100
108
|
end
|
101
109
|
|
102
110
|
# An object Type descriminator
|
@@ -140,13 +148,21 @@ module Yuki
|
|
140
148
|
|
141
149
|
# Builds one or more instance's of the class from
|
142
150
|
# a hash or array of hashes
|
143
|
-
def build(
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
151
|
+
def build(hash_or_hashes)
|
152
|
+
if hash_or_hashes
|
153
|
+
if hash_or_hashes.is_a? Array
|
154
|
+
hash_or_hashes.flatten.inject([]) do |list, hash|
|
155
|
+
type = hash[type_desc] || self.to_s.split('::').last
|
156
|
+
cls = resolve(type)
|
157
|
+
list << cls.new(hash) if cls
|
158
|
+
list
|
159
|
+
end
|
160
|
+
else
|
161
|
+
type = hash_or_hashes[type_desc] || self.to_s.split('::').last
|
162
|
+
cls = resolve(type)
|
163
|
+
cls.new(hash_or_hashes) if cls
|
164
|
+
end
|
165
|
+
end
|
150
166
|
end
|
151
167
|
|
152
168
|
# Resolves a class given a string or hash
|
@@ -164,7 +180,6 @@ module Yuki
|
|
164
180
|
obj.const_get(const)
|
165
181
|
} unless cls_def.to_s.strip.empty?
|
166
182
|
rescue NameError => e
|
167
|
-
puts "given #{cls_def} got #{e.inspect}"
|
168
183
|
raise e
|
169
184
|
end
|
170
185
|
end
|
@@ -271,7 +286,6 @@ module Yuki
|
|
271
286
|
def from_hash(h)
|
272
287
|
type = { self.class.type_desc => self.class.to_s }
|
273
288
|
h.merge!(type) unless h.include? self.class.type_desc
|
274
|
-
|
275
289
|
h.each { |k, v|
|
276
290
|
if attr_defined?(k)
|
277
291
|
self[k.to_s] = v
|
@@ -290,7 +304,7 @@ module Yuki
|
|
290
304
|
|
291
305
|
# specifies the object 'type' to serialize
|
292
306
|
def type
|
293
|
-
self['type'] || self.class
|
307
|
+
self['type'] || self.class.to_s
|
294
308
|
end
|
295
309
|
|
296
310
|
protected
|
data/yuki.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
$:
|
1
|
+
$: < File.join(File.dirname(__FILE__), 'lib')
|
2
2
|
require 'yuki'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yuki
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- softprops
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-18 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|