From c9c73970154c8271c27a7780c9ede590438aec62 Mon Sep 17 00:00:00 2001 From: delano Date: Fri, 6 Mar 2009 02:56:58 -0500 Subject: [PATCH] Caesars::Hash#to_hash now recursively casts children to ::Hash. Removed adding _values elements (i.e. pollution) --- CHANGES.txt | 13 ++++++------- lib/caesars.rb | 20 +++++++++++++++++--- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index a2847b2..7af0513 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,11 +4,10 @@ CAESAR -- CHANGES #### 0.4.3 (2009-03-??) ############################### * FIX: find_deferred now gracefully handles nil errors -* NEW: Stores list of values for all "complex" attributes -(the ones with children). Available as: NAME_values -* NEW: empty? method in Caesar::Config -* NEW: post processing hook in Caesar::Config#refresh - +* NEW: empty? method in Caesars::Config +* NEW: post processing hook in Caesars::Config#refresh +* NEW: Caesars::Hash#to_hash now recursively casts children +to ::Hash. #### 0.4.2 (2009-03-05) ############################### @@ -23,11 +22,11 @@ food :extra do; end; # => food_extra * CHANGE: Removed bloody method. We now parse blocks immediately. * CHANGE: Renamed virgin method to chill. -* NEW: Caesar::Config class for loading DSLs as config files. +* NEW: Caesars::Config class for loading DSLs as config files. See Example 3. * NEW: Added find_deferred method to automatically jump up the heirarchy when looking for a specific attribute. -* NEW: Added to_hash and [] methods to Caesar::Glass to make it +* NEW: Added to_hash and [] methods to Caesars to make it more hashlike. * FIX: "chilled" attributes weren't available by method name diff --git a/lib/caesars.rb b/lib/caesars.rb index 25eefee..e3241db 100644 --- a/lib/caesars.rb +++ b/lib/caesars.rb @@ -17,6 +17,20 @@ class Hash < ::Hash def method_missing(meth) self[meth] if self.has_key?(meth) end + + # Returns a clone of itself and all children cast as ::Hash objects + def to_hash(hash=self) + target = ::Hash[dup] + hash.keys.each do |key| + if hash[key].is_a? Caesars::Hash + target[key] = hash[key].to_hash + next + end + target[key] = hash[key] + end + target + end + end # An instance of Caesars::Hash which contains the data specified by your DSL @@ -37,7 +51,7 @@ def keys end def to_hash - @caesars_properties + @caesars_properties.to_hash end # Look for an attribute, bubbling up to the parent if it's not found @@ -92,7 +106,7 @@ def method_missing(meth, *args, &b) args << meth if args.empty? args.each do |name| prev = @caesars_pointer - (@caesars_pointer[:"#{meth}_values"] ||= []) << name + #(@caesars_pointer[:"#{meth}_values"] ||= []) << name @caesars_pointer[name] ||= Caesars::Hash.new @caesars_pointer = @caesars_pointer[name] b.call if b @@ -120,7 +134,7 @@ def #{meth}(*names,&b) #{}all = instance_variable_get("@" << #{meth}.to_s) || [] names.each do |name| - (@caesars_pointer[:"#{meth}_values"] ||= []) << name + #(@caesars_pointer[:"#{meth}_values"] ||= []) << name @caesars_pointer[name] = b end