remodel-h 0.2.8 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.8
1
+ 0.2.9
@@ -86,7 +86,7 @@ module Remodel
86
86
  name = name.to_sym
87
87
  mapper[name] = Remodel.mapper_for(options[:class])
88
88
  default_value = options[:default]
89
- define_method(name) { @attributes[name].nil? ? default_value : @attributes[name] }
89
+ define_method(name) { @attributes[name].nil? ? self.class.copy_of(default_value) : @attributes[name] }
90
90
  define_method("#{name}=") { |value| @attributes[name] = value }
91
91
  end
92
92
 
@@ -211,6 +211,10 @@ module Remodel
211
211
  @associations ||= []
212
212
  end
213
213
 
214
+ def self.copy_of(value)
215
+ value.is_a?(Array) || value.is_a?(Hash) ? value.dup : value
216
+ end
217
+
214
218
  end
215
219
 
216
220
  end
data/remodel-h.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{remodel-h}
8
- s.version = "0.2.8"
8
+ s.version = "0.2.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tim Lossen"]
12
- s.date = %q{2011-03-29}
12
+ s.date = %q{2011-04-18}
13
13
  s.default_executable = %q{redis-monitor.rb}
14
14
  s.description = %q{persist your objects to redis hashes.}
15
15
  s.email = %q{tim@lossen.de}
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
34
34
  "remodel-h.gemspec",
35
35
  "test/helper.rb",
36
36
  "test/test_entity.rb",
37
+ "test/test_entity_defaults.rb",
37
38
  "test/test_entity_delete.rb",
38
39
  "test/test_many_to_many.rb",
39
40
  "test/test_many_to_one.rb",
@@ -49,6 +50,7 @@ Gem::Specification.new do |s|
49
50
  s.test_files = [
50
51
  "test/helper.rb",
51
52
  "test/test_entity.rb",
53
+ "test/test_entity_defaults.rb",
52
54
  "test/test_entity_delete.rb",
53
55
  "test/test_many_to_many.rb",
54
56
  "test/test_many_to_one.rb",
data/test/test_entity.rb CHANGED
@@ -11,34 +11,6 @@ end
11
11
 
12
12
  class TestEntity < Test::Unit::TestCase
13
13
 
14
- context "[default values]" do
15
- should "be returned for missing properties" do
16
- bar = Bar.new('cx')
17
- assert_equal 123, bar.d
18
- end
19
-
20
- should "be returned for properties that are nil" do
21
- bar = Bar.new('cx', :d => 'cool')
22
- bar.d = nil
23
- assert_equal 123, bar.d
24
- end
25
-
26
- should "not be returned for given properties" do
27
- bar = Bar.new('cx', :d => 'cool')
28
- assert_equal 'cool', bar.d
29
- end
30
-
31
- should "not be stored" do
32
- bar = Bar.create('cx')
33
- assert !(/123/ =~ redis.hget('cx', bar.key))
34
- end
35
-
36
- should "be returned by as_json" do
37
- bar = Bar.new('cx')
38
- assert_equal 123, bar.as_json[:d]
39
- end
40
- end
41
-
42
14
  context "new" do
43
15
  should "set properties" do
44
16
  foo = Foo.new('cx', :x => 1, :y => 2)
@@ -0,0 +1,55 @@
1
+ require 'helper'
2
+
3
+ class TestEntityDefaults < Test::Unit::TestCase
4
+
5
+ class Bar < Remodel::Entity
6
+ property :simple, :default => 123
7
+ property :array, :default => [1]
8
+ property :hash, :default => { :foo => 1 }
9
+ end
10
+
11
+ context "[default values]" do
12
+ should "be returned for missing properties" do
13
+ bar = Bar.new('cx')
14
+ assert_equal 123, bar.simple
15
+ end
16
+
17
+ should "be returned for properties that are nil" do
18
+ bar = Bar.new('cx', :simple => 'cool')
19
+ bar.simple = nil
20
+ assert_equal 123, bar.simple
21
+ end
22
+
23
+ should "not be returned for given properties" do
24
+ bar = Bar.new('cx', :simple => 'cool')
25
+ assert_equal 'cool', bar.simple
26
+ end
27
+
28
+ should "not be stored" do
29
+ bar = Bar.create('cx')
30
+ assert !(/123/ =~ redis.hget('cx', bar.key))
31
+ end
32
+
33
+ should "be returned by as_json" do
34
+ bar = Bar.new('cx')
35
+ assert_equal 123, bar.as_json[:simple]
36
+ end
37
+ end
38
+
39
+ context "[collections]" do
40
+ setup do
41
+ @bar, @baz = Bar.new('cx'), Bar.new('cx')
42
+ end
43
+
44
+ should "not share arrays" do
45
+ @bar.array[0] += 1
46
+ assert_equal [1], @baz.array
47
+ end
48
+
49
+ should "not share hashes" do
50
+ @bar.hash[:foo] = 42
51
+ assert_equal 1, @baz.hash[:foo]
52
+ end
53
+ end
54
+
55
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 8
9
- version: 0.2.8
8
+ - 9
9
+ version: 0.2.9
10
10
  platform: ruby
11
11
  authors:
12
12
  - Tim Lossen
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-03-29 00:00:00 +02:00
17
+ date: 2011-04-18 00:00:00 +02:00
18
18
  default_executable: redis-monitor.rb
19
19
  dependencies: []
20
20
 
@@ -43,6 +43,7 @@ files:
43
43
  - remodel-h.gemspec
44
44
  - test/helper.rb
45
45
  - test/test_entity.rb
46
+ - test/test_entity_defaults.rb
46
47
  - test/test_entity_delete.rb
47
48
  - test/test_many_to_many.rb
48
49
  - test/test_many_to_one.rb
@@ -85,6 +86,7 @@ summary: remodel variant which uses hashes
85
86
  test_files:
86
87
  - test/helper.rb
87
88
  - test/test_entity.rb
89
+ - test/test_entity_defaults.rb
88
90
  - test/test_entity_delete.rb
89
91
  - test/test_many_to_many.rb
90
92
  - test/test_many_to_one.rb