lazyhash 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/lazyhash.rb CHANGED
@@ -1,17 +1,12 @@
1
1
  module LazyHash
2
2
  class << self
3
- def add(hash, key, value, pre = nil)
3
+ def add(hash, key, value)
4
4
  skeys = key.split(".")
5
5
  f = skeys.shift
6
6
  if skeys.empty?
7
- if pre.nil?
8
- hash.send("[]=", f, value) unless hash.has_key?(f)
9
- else
10
- pre.send("[]=", f, value) unless pre.has_key?(f)
11
- end
7
+ hash.send("[]=", f, value) unless !hash.is_a?(Hash) || (hash.has_key?(f) && hash[f].is_a?(Hash))
12
8
  else
13
- pre = pre.nil? ? hash.send("[]", f) : pre.send("[]", f)
14
- add(hash, skeys.join("."), value, pre)
9
+ add(hash[f], skeys.join("."), value)
15
10
  end
16
11
  end
17
12
 
@@ -19,5 +14,9 @@ module LazyHash
19
14
  lazy = lambda { |h,k| h[k] = Hash.new(&lazy) }
20
15
  Hash.new(&lazy)
21
16
  end
17
+
18
+ def no_lazy(hash)
19
+ eval(hash.inspect)
20
+ end
22
21
  end
23
22
  end
@@ -1,3 +1,3 @@
1
1
  module Lazyhash
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -5,17 +5,55 @@ describe LazyHash do
5
5
  @hash = LazyHash.build_hash
6
6
  end
7
7
 
8
- it "should assign consecutive values with different deep's" do
9
- LazyHash.add(@hash, "es.projects.title", "Main title")
10
- LazyHash.add(@hash, "es.ph1", "one paragraph")
8
+ it "should not be marshable" do
9
+ lambda { Marshal.dump(@hash) }.should raise_error
10
+ end
11
+
12
+ describe ".add" do
13
+ it "should assign consecutive values with different deep's" do
14
+ LazyHash.add(@hash, "es.projects.title", "Main title")
15
+ LazyHash.add(@hash, "es.ph1", "one paragraph")
16
+
17
+ @hash.should == {"es" => {"projects" => {"title" => "Main title"}, "ph1" => "one paragraph"}}
18
+ end
19
+
20
+ it "should overwrite values for the same key" do
21
+ LazyHash.add(@hash, "es.ph1", "First value")
22
+ LazyHash.add(@hash, "es.ph1", "Second value")
23
+
24
+ @hash.should == {"es" => {"ph1" => "Second value"}}
25
+ end
11
26
 
12
- @hash.should == {"es" => {"projects" => {"title" => "Main title"}, "ph1" => "one paragraph"}}
27
+ it "should not overwrite a node previously defined as a 'folder'" do
28
+ LazyHash.add(@hash, "es.projects.title", "Main title")
29
+ LazyHash.add(@hash, "es.projects", "try to convert the projects folder into a value with this string")
30
+
31
+ @hash.should == {"es" => {"projects" => {"title" => "Main title"}}}
32
+ end
33
+
34
+ it "should not overwrite a node previously defined as a 'value'" do
35
+ LazyHash.add(@hash, "es.projects", "Proyectos")
36
+ LazyHash.add(@hash, "es.projects.title", "try to convert a value into a folder with other values")
37
+
38
+ @hash.should == {"es" => {"projects" => "Proyectos"}}
39
+ end
13
40
  end
14
41
 
15
- it "should not overwrite existing values with keys" do
16
- LazyHash.add(@hash, "es.projects.title", "Main title")
17
- LazyHash.add(@hash, "es.projects", "title")
42
+ describe ".no_lazy" do
43
+ it "should return a copy of the given hash" do
44
+ LazyHash.add(@hash, "es.projects", "Proyectos")
45
+ unlazy = LazyHash.no_lazy(@hash)
46
+ unlazy.should == {"es" => {"projects" => "Proyectos"}}
47
+ end
48
+
49
+ it "should return a copy of the given hash without the proc constructor" do
50
+ unlazy = LazyHash.no_lazy(@hash)
51
+ unlazy.default_proc.should be_nil
52
+ end
18
53
 
19
- @hash.should == {"es" => {"projects" => {"title" => "Main title"}}}
54
+ it "should return a marshable hash" do
55
+ unlazy = LazyHash.no_lazy(@hash)
56
+ lambda { Marshal.dump(unlazy) }.should_not raise_error
57
+ end
20
58
  end
21
59
  end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazyhash
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 0
10
- version: 0.1.0
4
+ prerelease:
5
+ version: 0.1.1
11
6
  platform: ruby
12
7
  authors:
13
8
  - Roger Campos
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-02-17 00:00:00 +01:00
13
+ date: 2011-02-18 00:00:00 +01:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -26,10 +21,6 @@ dependencies:
26
21
  requirements:
27
22
  - - ~>
28
23
  - !ruby/object:Gem::Version
29
- hash: 9
30
- segments:
31
- - 2
32
- - 5
33
24
  version: "2.5"
34
25
  type: :development
35
26
  version_requirements: *id001
@@ -65,23 +56,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
56
  requirements:
66
57
  - - ">="
67
58
  - !ruby/object:Gem::Version
68
- hash: 3
69
- segments:
70
- - 0
71
59
  version: "0"
72
60
  required_rubygems_version: !ruby/object:Gem::Requirement
73
61
  none: false
74
62
  requirements:
75
63
  - - ">="
76
64
  - !ruby/object:Gem::Version
77
- hash: 3
78
- segments:
79
- - 0
80
65
  version: "0"
81
66
  requirements: []
82
67
 
83
68
  rubyforge_project: lazyhash
84
- rubygems_version: 1.3.7
69
+ rubygems_version: 1.5.2
85
70
  signing_key:
86
71
  specification_version: 3
87
72
  summary: add values to a hash with an arbitrary deep of keys