Skip to content
This repository has been archived by the owner on Jun 19, 2019. It is now read-only.

Commit

Permalink
.add method refactored and some more specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger Campos committed Feb 18, 2011
1 parent cf5449a commit ae91184
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
11 changes: 3 additions & 8 deletions lib/lazyhash.rb
@@ -1,17 +1,12 @@
module LazyHash
class << self
def add(hash, key, value, pre = nil)
def add(hash, key, value)
skeys = key.split(".")
f = skeys.shift
if skeys.empty?
if pre.nil?
hash.send("[]=", f, value) unless hash.has_key?(f)
else
pre.send("[]=", f, value) unless pre.has_key?(f)
end
hash.send("[]=", f, value) unless !hash.is_a?(Hash) || (hash.has_key?(f) && hash[f].is_a?(Hash))
else
pre = pre.nil? ? hash.send("[]", f) : pre.send("[]", f)
add(hash, skeys.join("."), value, pre)
add(hash[f], skeys.join("."), value)
end
end

Expand Down
7 changes: 3 additions & 4 deletions spec/lazyhash_spec.rb
Expand Up @@ -28,11 +28,10 @@
end

it "should not overwrite a node previously defined as a 'value'" do
LazyHash.add(@hash, "es.projects", "title")
LazyHash.add(@hash, "es.projects.title", "Main title")
LazyHash.add(@hash, "es.projects", "Proyectos")
LazyHash.add(@hash, "es.projects.title", "try to convert a value into a folder with other values")

puts @hash.inspect
@hash.should == {"es" => {"projects" => "title"}}
@hash.should == {"es" => {"projects" => "Proyectos"}}
end
end

Expand Down

0 comments on commit ae91184

Please sign in to comment.