Permalink
Browse files

* CHANGE: to_hash and to_array will set field values if the object isn't

  frozen and the field has a processor.
  • Loading branch information...
1 parent 7dd6bbb commit cab40d20b6cca3168f4775141187455b7f9433aa @delano committed May 20, 2011
Showing with 25 additions and 15 deletions.
  1. +6 −1 CHANGES.txt
  2. +19 −14 lib/storable.rb
View
@@ -2,7 +2,12 @@ STORABLE, CHANGES
* TODO: https://github.com/delano/storable/pull/2
* TODO: Handle nested hashes and arrays.
-* TODO: to_xml, see: http://codeforpeople.com/lib/ruby/xx/xx-2.0.0/README
+
+
+#### 0.8.7 (2010-05-20) #############################
+
+* CHANGE: to_hash and to_array will set field values if the object isn't
+ frozen and the field has a processor.
#### 0.8.6 (2010-12-31) #############################
View
@@ -145,15 +145,15 @@ def sensitive!
# Returns an array of field names defined by self.field
def field_names
- self.class.field_names
+ self.class.field_names #|| self.class.ancestors.first.field_names
end
# Returns an array of field types defined by self.field. Fields that did
# not receive a type are set to nil.
def field_types
- self.class.field_types
+ self.class.field_types #|| self.class.ancestors.first.field_types
end
def sensitive_fields
- self.class.sensitive_fields
+ self.class.sensitive_fields #|| self.class.ancestors.first.sensitive_fields
end
# Dump the object data to the given format.
@@ -298,14 +298,19 @@ def from_hash(from={})
def to_hash
preprocess if respond_to? :preprocess
tmp = USE_ORDERED_HASH ? Storable::OrderedHash.new : {}
- field_names.each do |fname|
- next if sensitive? && 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_hash : v2 }
+ if field_names
+ field_names.each do |fname|
+ next if sensitive? && self.class.sensitive_field?(fname)
+ v = self.send(fname)
+ if has_processor?(fname)
+ v = process(fname, v)
+ self.send("#{fname}=", v) unless frozen?
+ end
+ if Array === v
+ v = v.collect { |v2| v2.kind_of?(Storable) ? v2.to_hash : v2 }
+ end
+ tmp[fname] = v.kind_of?(Storable) ? v.to_hash : v
end
- tmp[fname] = v.kind_of?(Storable) ? v.to_hash : v
end
tmp
end
@@ -316,7 +321,10 @@ def to_array
fields.collect do |fname|
next if sensitive? && self.class.sensitive_field?(fname)
v = self.send(fname)
- v = process(fname, v) if has_processor?(fname)
+ if has_processor?(fname)
+ v = process(fname, v)
+ self.send("#{fname}=", v) unless frozen?
+ end
if Array === v
v = v.collect { |v2| v2.kind_of?(Storable) ? v2.to_a : v2 }
end
@@ -329,9 +337,6 @@ def to_json(*from, &blk)
hash = to_hash
if YAJL_LOADED # set by Storable
ret = Yajl::Encoder.encode(hash)
- #raise "DELANO"
- #ret.force_encoding("ISO-8859-1")
- #p [:to, ret.encoding.name] if ret.respond_to?(:encoding)
ret
elsif JSON_LOADED
JSON.generate(hash, *from, &blk)

0 comments on commit cab40d2

Please sign in to comment.