Permalink
Browse files

from_array fixes; updated tryout

  • Loading branch information...
1 parent ae83c92 commit d03cab59e5138de702d3318432107240e067635b @delano committed Dec 24, 2010
Showing with 18 additions and 12 deletions.
  1. +6 −3 lib/storable.rb
  2. +12 −9 try/10_basic_usage_tryouts.rb
View
@@ -203,12 +203,15 @@ def self.from_hash(from={})
end
end
- def self.from_array(from=[])
+ def self.from_array(*from)
+ from = from.flatten.compact
return nil if !from || from.empty?
- (self.class.field_names || []).each_with_index do |n,index|
+ me = new
+ (self.field_names || []).each_with_index do |n,index|
break if index >= from.size
- self.send("#{n}=", from[index])
+ me.send("#{n}=", from[index])
end
+ me
end
def call(fname)
@@ -1,38 +1,41 @@
require 'storable'
-class A < Storable
+class ::A < Storable
field :one => String
field :two => Integer
field :three => Time
field :four => Boolean
end
-class B < Storable
+class ::B < Storable
field :one => String
field :two
field :three => Time
end
-class C < Storable
+class ::C < Storable
field :calc => Proc
end
-class D < A
+class ::D < A
field :five
sensitive_fields :five
end
-
+## Has instance methods
+A.instance_methods(false).collect(&:to_s).sort
+#=> ["four", "four=", "one", "one=", "three", "three=", "two", "two="]
+
## "Storable objects have a default initialize method"
-a = A.new "string", 1, Time.parse("2010-03-04 23:00"), true
+a = A.from_array "string", 1, Time.parse("2010-03-04 23:00"), true
[a.one, a.two, a.three, a.four]
#=> ["string", 1, Time.parse("2010-03-04 23:00"), true]
## Supports to_array
-a = A.new "string", 1, Time.parse("2010-03-04 23:00"), true
+a = A.from_array "string", 1, Time.parse("2010-03-04 23:00"), true
a.to_array
#=> ["string", 1, Time.parse("2010-03-04 23:00"), true]
## "Field types are optional"
-b = B.new "string", 1, Time.parse("2010-03-04 23:00")
+b = B.from_array "string", 1, Time.parse("2010-03-04 23:00")
b = B.from_json b.to_json
[b.one, b.two, b.three]
#=> ["string", 1, Time.parse("2010-03-04 23:00")]
@@ -41,7 +44,7 @@ class D < A
calc = Proc.new do
2+2
end
-c = C.new calc
+c = C.from_array calc
c= C.from_json c.to_json
c.calc.call
#=> 4

0 comments on commit d03cab5

Please sign in to comment.