Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Supports to_a

  • Loading branch information...
commit c17a6e75b990e5261141cec22dcd3627a4797804 1 parent e02e622
Delano Mandelbaum authored
Showing with 36 additions and 5 deletions.
  1. +14 −1 README.rdoc
  2. +16 −3 lib/storable.rb
  3. +6 −1 try/10_basic_usage_tryouts.rb
15 README.rdoc
View
@@ -25,7 +25,20 @@ Marshal Ruby classes into and out of multiple formats (yaml, json, csv, tsv)
puts mac2.environment # => "stage"
puts mac2.position.class # => Fixnum
+== Sensitive Fields
+ require 'storable'
+
+ class Calc < Storable
+ field :one
+ field :two
+ field :three
+ sensitive_fields :three
+ end
+
+ calc = Calc.new 1, 2, 3
+ calc.to_s
+
== Storing Procs
Storable can also marshal Proc objects to and from their actual source code.
@@ -49,7 +62,7 @@ Storable can also marshal Proc objects to and from their actual source code.
mat2 = Maths.from_json dump
mat2.call :calculate # => 6.0
-
+
Anything is possible when you keep your mind open and you use Ruby.
19 lib/storable.rb
View
@@ -53,9 +53,9 @@ class << self
# Passes along fields to inherited classes
def self.inherited(obj)
unless Storable == self
- obj.sensitive_fields = self.sensitive_fields
- obj.field_names = self.field_names
- obj.field_types = self.field_types
+ obj.sensitive_fields = self.sensitive_fields.clone
+ obj.field_names = self.field_names.clone
+ obj.field_types = self.field_types.clone
end
end
@@ -283,6 +283,7 @@ def from_hash(from={})
self.postprocess
self
end
+
# Return the object data as a hash
# +with_titles+ is ignored.
def to_hash
@@ -299,6 +300,18 @@ def to_hash
tmp
end
+ def to_a
+ field_names.collect do |fname|
+ next if self.class.sensitive_field?(fname)
+ v = self.send(fname)
+ v = process(fname, v) if has_processor?(fname)
+ if Array === v
+ v = v.collect { |v2| v2.kind_of?(Storable) ? v2.to_a : v2 }
+ end
+ v
+ end
+ end
+
def to_json(*from, &blk)
hash = to_hash
if YAJL_LOADED # set by Storable
7 try/10_basic_usage_tryouts.rb
View
@@ -25,7 +25,12 @@ class D < A
a = A.new "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_a
+a = A.new "string", 1, Time.parse("2010-03-04 23:00"), true
+a.to_a
+#=> ["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_json b.to_json
Please sign in to comment.
Something went wrong with that request. Please try again.