Skip to content

Commit

Permalink
Caesars::Hash#to_hash now recursively casts children to ::Hash. Remov…
Browse files Browse the repository at this point in the history
…ed adding _values elements (i.e. pollution)
  • Loading branch information
delano committed Mar 6, 2009
1 parent d9064cb commit c9c7397
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
13 changes: 6 additions & 7 deletions CHANGES.txt
Expand Up @@ -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) ###############################

Expand All @@ -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

Expand Down
20 changes: 17 additions & 3 deletions lib/caesars.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c9c7397

Please sign in to comment.