lib/volt/models/model_hash_behaviour.rb in volt-0.8.8 vs lib/volt/models/model_hash_behaviour.rb in volt-0.8.9

- old
+ new

@@ -16,10 +16,14 @@ def nil? attributes.nil? end + def empty? + !attributes || attributes.size == 0 + end + def false? attributes.false? end def true? @@ -34,17 +38,25 @@ attributes.clear @persistor.removed(nil) if @persistor end + def each_with_object(*args, &block) + return (@attributes || {}).each_with_object(*args, &block) + end + # Convert the model to a hash all of the way down. def to_h hash = {} - attributes.each_pair do |key, value| - hash[key] = deep_unwrap(value) - end + if empty? + return nil + else + attributes.each_pair do |key, value| + hash[key] = deep_unwrap(value) + end - return hash + return hash + end end end