remodel-h-r19 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/LICENSE +20 -0
- data/README.md +96 -0
- data/Rakefile +27 -0
- data/VERSION +1 -0
- data/bin/redis-monitor.rb +25 -0
- data/docs/docco.css +185 -0
- data/docs/remodel.html +269 -0
- data/example/book.rb +13 -0
- data/lib/remodel-h.rb +69 -0
- data/lib/remodel/entity.rb +202 -0
- data/lib/remodel/has_many.rb +71 -0
- data/lib/remodel/mapper.rb +29 -0
- data/remodel-h.gemspec +73 -0
- data/test/helper.rb +13 -0
- data/test/test_entity.rb +297 -0
- data/test/test_entity_delete.rb +45 -0
- data/test/test_many_to_many.rb +77 -0
- data/test/test_many_to_one.rb +107 -0
- data/test/test_mappers.rb +67 -0
- data/test/test_monkeypatches.rb +30 -0
- data/test/test_one_to_many.rb +96 -0
- data/test/test_one_to_one.rb +57 -0
- metadata +98 -0
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class Item < Remodel::Entity
|
4
|
+
property :boolean, :class => Boolean
|
5
|
+
property :string, :class => String
|
6
|
+
property :integer, :class => Integer
|
7
|
+
property :float, :class => Float
|
8
|
+
property :array, :class => Array
|
9
|
+
property :hash, :class => Hash
|
10
|
+
property :time, :class => Time
|
11
|
+
property :date, :class => Date
|
12
|
+
end
|
13
|
+
|
14
|
+
class TestMappers < Test::Unit::TestCase
|
15
|
+
|
16
|
+
context "create" do
|
17
|
+
setup do
|
18
|
+
@item = Item.create('cx', :time => Time.at(1234567890), :date => Date.parse("1972-06-16"))
|
19
|
+
end
|
20
|
+
|
21
|
+
should "store unmapped values" do
|
22
|
+
assert_equal Time, @item.instance_eval { @attributes[:time].class }
|
23
|
+
assert_equal Date, @item.instance_eval { @attributes[:date].class }
|
24
|
+
end
|
25
|
+
|
26
|
+
should "not change mapped values" do
|
27
|
+
assert_equal Time.at(1234567890), @item.time
|
28
|
+
assert_equal Date.parse("1972-06-16"), @item.date
|
29
|
+
end
|
30
|
+
|
31
|
+
should "not change mapped values after reload" do
|
32
|
+
@item.reload
|
33
|
+
assert_equal Time.at(1234567890), @item.time
|
34
|
+
assert_equal Date.parse("1972-06-16"), @item.date
|
35
|
+
end
|
36
|
+
|
37
|
+
should "serialize mapped values correctly" do
|
38
|
+
json = redis.hget(@item.context, @item.key)
|
39
|
+
assert_match /1234567890/, json
|
40
|
+
assert_match /"1972-06-16"/, json
|
41
|
+
end
|
42
|
+
|
43
|
+
should "handle nil values" do
|
44
|
+
item = Item.create('cx')
|
45
|
+
assert_nil item.boolean
|
46
|
+
assert_nil item.string
|
47
|
+
assert_nil item.integer
|
48
|
+
assert_nil item.float
|
49
|
+
assert_nil item.array
|
50
|
+
assert_nil item.hash
|
51
|
+
assert_nil item.time
|
52
|
+
assert_nil item.date
|
53
|
+
end
|
54
|
+
|
55
|
+
should "reject invalid types" do
|
56
|
+
assert_raise(Remodel::InvalidType) { Item.create('cx', :boolean => 'hello') }
|
57
|
+
assert_raise(Remodel::InvalidType) { Item.create('cx', :string => true) }
|
58
|
+
assert_raise(Remodel::InvalidType) { Item.create('cx', :integer => 33.5) }
|
59
|
+
assert_raise(Remodel::InvalidType) { Item.create('cx', :float => 5) }
|
60
|
+
assert_raise(Remodel::InvalidType) { Item.create('cx', :array => {}) }
|
61
|
+
assert_raise(Remodel::InvalidType) { Item.create('cx', :hash => []) }
|
62
|
+
assert_raise(Remodel::InvalidType) { Item.create('cx', :time => Date.new) }
|
63
|
+
assert_raise(Remodel::InvalidType) { Item.create('cx', :date => Time.now) }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestMonkeypatches < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "Boolean" do
|
6
|
+
should "be the superclass of both true and false" do
|
7
|
+
assert true.is_a?(Boolean)
|
8
|
+
assert false.is_a?(Boolean)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context "Class[]" do
|
13
|
+
should "return given Class objects" do
|
14
|
+
assert_equal String, Class[String]
|
15
|
+
end
|
16
|
+
|
17
|
+
should "return the Class object for a given String" do
|
18
|
+
assert_equal String, Class['String']
|
19
|
+
end
|
20
|
+
|
21
|
+
should "return the Class object for a given Symbol" do
|
22
|
+
assert_equal String, Class[:String]
|
23
|
+
end
|
24
|
+
|
25
|
+
should "work for nested classes" do
|
26
|
+
assert_equal Remodel::Entity, Class['Remodel::Entity']
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
|
4
|
+
class TestOneToMany < Test::Unit::TestCase
|
5
|
+
|
6
|
+
class Piece < Remodel::Entity
|
7
|
+
has_one :puzzle, :class => 'TestOneToMany::Puzzle', :reverse => 'pieces'
|
8
|
+
property :color
|
9
|
+
end
|
10
|
+
|
11
|
+
class Puzzle < Remodel::Entity
|
12
|
+
has_many :pieces, :class => 'TestOneToMany::Piece', :reverse => 'puzzle'
|
13
|
+
property :topic
|
14
|
+
end
|
15
|
+
|
16
|
+
context "has_one" do
|
17
|
+
context "association getter" do
|
18
|
+
should "exist" do
|
19
|
+
assert Piece.create('cx').respond_to?(:puzzle)
|
20
|
+
end
|
21
|
+
|
22
|
+
should "return nil by default" do
|
23
|
+
assert_nil Piece.create('cx').puzzle
|
24
|
+
end
|
25
|
+
|
26
|
+
should "return the associated entity" do
|
27
|
+
puzzle = Puzzle.create('cx', :topic => 'animals')
|
28
|
+
piece = Piece.create('cx')
|
29
|
+
redis.hset('cx', "#{piece.key}_puzzle", puzzle.key)
|
30
|
+
assert_equal 'animals', piece.puzzle.topic
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "association setter" do
|
35
|
+
should "exist" do
|
36
|
+
assert Piece.create('cx').respond_to?(:'puzzle=')
|
37
|
+
end
|
38
|
+
|
39
|
+
should "store the key of the associated entity" do
|
40
|
+
puzzle = Puzzle.create('cx')
|
41
|
+
piece = Piece.create('cx')
|
42
|
+
piece.puzzle = puzzle
|
43
|
+
assert_equal puzzle.key, redis.hget(puzzle.context, "#{piece.key}_puzzle")
|
44
|
+
end
|
45
|
+
|
46
|
+
should "add the entity to the reverse association" do
|
47
|
+
puzzle = Puzzle.create('cx')
|
48
|
+
piece = Piece.create('cx')
|
49
|
+
piece.puzzle = puzzle
|
50
|
+
assert_equal 1, puzzle.pieces.size
|
51
|
+
assert_equal piece.id, puzzle.pieces.first.id
|
52
|
+
end
|
53
|
+
|
54
|
+
should "remove the entity from the old reverse association" do
|
55
|
+
puzzle = Puzzle.create('cx')
|
56
|
+
piece = puzzle.pieces.create
|
57
|
+
new_puzzle = Puzzle.create('cx')
|
58
|
+
piece.puzzle = new_puzzle
|
59
|
+
assert_equal [], puzzle.reload.pieces
|
60
|
+
end
|
61
|
+
|
62
|
+
should "be settable to nil" do
|
63
|
+
piece = Piece.create('cx')
|
64
|
+
piece.puzzle = nil
|
65
|
+
assert_nil piece.puzzle
|
66
|
+
end
|
67
|
+
|
68
|
+
should "remove the key if set to nil" do
|
69
|
+
piece = Piece.create('cx')
|
70
|
+
piece.puzzle = Puzzle.create('cx')
|
71
|
+
piece.puzzle = nil
|
72
|
+
assert_nil redis.hget(piece.context, "#{piece.key}_puzzle")
|
73
|
+
end
|
74
|
+
|
75
|
+
should "remove the entity from the reverse association if set to nil" do
|
76
|
+
puzzle = Puzzle.create('cx')
|
77
|
+
piece = Piece.create('cx')
|
78
|
+
piece.puzzle = puzzle
|
79
|
+
piece.puzzle = nil
|
80
|
+
puzzle.reload
|
81
|
+
assert_equal 0, puzzle.pieces.size
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context "reload" do
|
87
|
+
should "reset has_one associations" do
|
88
|
+
piece = Piece.create('cx', :color => 'black')
|
89
|
+
piece.puzzle = Puzzle.create('cx')
|
90
|
+
redis.hdel 'cx', "#{piece.key}_puzzle"
|
91
|
+
piece.reload
|
92
|
+
assert_nil piece.puzzle
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestOneToOne < Test::Unit::TestCase
|
4
|
+
|
5
|
+
class Man < Remodel::Entity
|
6
|
+
has_one :wife, :class => 'TestOneToOne::Woman', :reverse => 'husband'
|
7
|
+
property :name
|
8
|
+
end
|
9
|
+
|
10
|
+
class Woman < Remodel::Entity
|
11
|
+
has_one :husband, :class => 'TestOneToOne::Man', :reverse => 'wife'
|
12
|
+
property :name
|
13
|
+
end
|
14
|
+
|
15
|
+
context "both associations" do
|
16
|
+
should "be nil by default" do
|
17
|
+
assert_equal nil, Man.new('cx').wife
|
18
|
+
assert_equal nil, Woman.new('cx').husband
|
19
|
+
end
|
20
|
+
|
21
|
+
context "setter" do
|
22
|
+
setup do
|
23
|
+
@bill = Man.create('cx', :name => 'Bill')
|
24
|
+
@mary = Woman.create('cx', :name => 'Mary')
|
25
|
+
end
|
26
|
+
|
27
|
+
context "non-nil value" do
|
28
|
+
should "also set husband" do
|
29
|
+
@bill.wife = @mary
|
30
|
+
assert_equal @bill, @mary.husband
|
31
|
+
end
|
32
|
+
|
33
|
+
should "also set wife" do
|
34
|
+
@mary.husband = @bill
|
35
|
+
assert_equal @mary, @bill.wife
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "nil value" do
|
40
|
+
setup do
|
41
|
+
@bill.wife = @mary
|
42
|
+
end
|
43
|
+
|
44
|
+
should "also clear husband" do
|
45
|
+
@bill.wife = nil
|
46
|
+
assert_equal nil, @mary.husband
|
47
|
+
end
|
48
|
+
|
49
|
+
should "also clear wife" do
|
50
|
+
@mary.husband = nil
|
51
|
+
assert_equal nil, @bill.wife
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: remodel-h-r19
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 4
|
10
|
+
version: 0.2.4
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Tim Lossen
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-10-15 00:00:00 +02:00
|
19
|
+
default_executable: redis-monitor.rb
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: persist your objects to redis hashes.
|
23
|
+
email: tim@lossen.de
|
24
|
+
executables:
|
25
|
+
- redis-monitor.rb
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- LICENSE
|
30
|
+
- README.md
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- LICENSE
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- bin/redis-monitor.rb
|
38
|
+
- docs/docco.css
|
39
|
+
- docs/remodel.html
|
40
|
+
- example/book.rb
|
41
|
+
- lib/remodel-h.rb
|
42
|
+
- lib/remodel/entity.rb
|
43
|
+
- lib/remodel/has_many.rb
|
44
|
+
- lib/remodel/mapper.rb
|
45
|
+
- remodel-h.gemspec
|
46
|
+
- test/helper.rb
|
47
|
+
- test/test_entity.rb
|
48
|
+
- test/test_entity_delete.rb
|
49
|
+
- test/test_many_to_many.rb
|
50
|
+
- test/test_many_to_one.rb
|
51
|
+
- test/test_mappers.rb
|
52
|
+
- test/test_monkeypatches.rb
|
53
|
+
- test/test_one_to_many.rb
|
54
|
+
- test/test_one_to_one.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/tlossen/remodel
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --charset=UTF-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.3.7
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: ruby 1.9.2 remodel variant which uses hashes
|
89
|
+
test_files:
|
90
|
+
- test/helper.rb
|
91
|
+
- test/test_entity.rb
|
92
|
+
- test/test_entity_delete.rb
|
93
|
+
- test/test_many_to_many.rb
|
94
|
+
- test/test_many_to_one.rb
|
95
|
+
- test/test_mappers.rb
|
96
|
+
- test/test_monkeypatches.rb
|
97
|
+
- test/test_one_to_many.rb
|
98
|
+
- test/test_one_to_one.rb
|