property 1.2.0 → 2.0.0
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/History.txt +14 -0
- data/README.rdoc +6 -2
- data/lib/property.rb +2 -1
- data/lib/property/attribute.rb +3 -2
- data/lib/property/declaration.rb +65 -17
- data/lib/property/index.rb +21 -1
- data/lib/property/properties.rb +1 -1
- data/lib/property/role.rb +16 -10
- data/lib/property/role_module.rb +83 -196
- data/lib/property/schema.rb +68 -117
- data/lib/property/stored_role.rb +13 -6
- data/lib/property/version.rb +1 -1
- data/property.gemspec +6 -4
- data/test/database.rb +1 -0
- data/test/fixtures.rb +1 -0
- data/test/shoulda_macros/index.rb +6 -3
- data/test/shoulda_macros/role.rb +26 -50
- data/test/unit/property/declaration_test.rb +34 -40
- data/test/unit/property/index_field_test.rb +56 -0
- data/test/unit/property/role_test.rb +6 -16
- data/test/unit/property/stored_role_test.rb +86 -1
- metadata +15 -4
@@ -42,8 +42,10 @@ class DeclarationTest < Test::Unit::TestCase
|
|
42
42
|
assert_equal %w{age first_name name}, subject.schema.column_names.sort
|
43
43
|
end
|
44
44
|
|
45
|
-
|
46
|
-
|
45
|
+
# This is allowed: it's the user's responsability to make sure such a thing does not happen
|
46
|
+
# or cause problems.
|
47
|
+
should 'be allowed to overwrite a property from the parent class' do
|
48
|
+
assert_nothing_raised do
|
47
49
|
subject.class_eval do
|
48
50
|
property.string 'name'
|
49
51
|
end
|
@@ -58,9 +60,10 @@ class DeclarationTest < Test::Unit::TestCase
|
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
61
|
-
# This is
|
62
|
-
|
63
|
-
|
63
|
+
# This is allowed, it's the user's responsability to make sure such a thing does not cause
|
64
|
+
# problems.
|
65
|
+
should 'be allowed to define a property with the name of a method in the parent class' do
|
66
|
+
assert_nothing_raised do
|
64
67
|
subject.class_eval do
|
65
68
|
property.string 'method_in_parent'
|
66
69
|
end
|
@@ -77,7 +80,7 @@ class DeclarationTest < Test::Unit::TestCase
|
|
77
80
|
end
|
78
81
|
end
|
79
82
|
end
|
80
|
-
|
83
|
+
|
81
84
|
context 'An instance' do
|
82
85
|
subject do
|
83
86
|
Class.new(ActiveRecord::Base) do
|
@@ -85,15 +88,15 @@ class DeclarationTest < Test::Unit::TestCase
|
|
85
88
|
include Property
|
86
89
|
end.new
|
87
90
|
end
|
88
|
-
|
91
|
+
|
89
92
|
should 'be able to include a role with _name_ property' do
|
90
93
|
role_with_name = Property::Role.new('foo')
|
91
94
|
role_with_name.property do |p|
|
92
95
|
p.string :name
|
93
96
|
end
|
94
|
-
|
97
|
+
|
95
98
|
assert_nothing_raised do
|
96
|
-
subject.
|
99
|
+
subject.include_role role_with_name
|
97
100
|
end
|
98
101
|
end
|
99
102
|
end # An instance
|
@@ -175,12 +178,12 @@ class DeclarationTest < Test::Unit::TestCase
|
|
175
178
|
assert_equal Time, column.klass
|
176
179
|
assert_equal :datetime, column.type
|
177
180
|
end
|
178
|
-
|
181
|
+
|
179
182
|
should 'allow multiple declarations in one go' do
|
180
183
|
subject.property.string 'foo', 'bar', 'baz'
|
181
184
|
assert_equal %w{bar baz foo}, subject.schema.column_names.sort
|
182
185
|
end
|
183
|
-
|
186
|
+
|
184
187
|
should 'allow multiple declarations in an Array' do
|
185
188
|
subject.property.string ['foo', 'bar', 'baz']
|
186
189
|
assert_equal %w{bar baz foo}, subject.schema.column_names.sort
|
@@ -225,7 +228,7 @@ class DeclarationTest < Test::Unit::TestCase
|
|
225
228
|
p.string 'poem'
|
226
229
|
end
|
227
230
|
|
228
|
-
@instance.
|
231
|
+
@instance.include_role @poet
|
229
232
|
end
|
230
233
|
|
231
234
|
should 'behave like any other property column' do
|
@@ -236,10 +239,19 @@ class DeclarationTest < Test::Unit::TestCase
|
|
236
239
|
assert_equal Hash['poem' => 'shazam'], @instance.prop
|
237
240
|
end
|
238
241
|
|
242
|
+
should 'use method_missing for property methods' do
|
243
|
+
assert !@instance.respond_to?(:poem=)
|
244
|
+
assert_nothing_raised do
|
245
|
+
@instance.poem = 'shazam'
|
246
|
+
assert_equal 'shazam', @instance.poem
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
239
250
|
should 'not affect instance class' do
|
240
251
|
assert !subject.schema.column_names.include?('poem')
|
241
252
|
assert_raise(NoMethodError) do
|
242
|
-
subject.new
|
253
|
+
instance = subject.new
|
254
|
+
instance.poem = 'not a poet'
|
243
255
|
end
|
244
256
|
end
|
245
257
|
end
|
@@ -265,29 +277,13 @@ class DeclarationTest < Test::Unit::TestCase
|
|
265
277
|
subject { Class.new(Developer) }
|
266
278
|
|
267
279
|
should 'raise an exception if we ask to behave like a class without schema' do
|
268
|
-
assert_raise(TypeError) { subject.
|
280
|
+
assert_raise(TypeError) { subject.include_role String }
|
269
281
|
end
|
270
282
|
|
271
283
|
should 'raise an exception if we ask to behave like an object' do
|
272
|
-
assert_raise(TypeError) { subject.
|
273
|
-
end
|
274
|
-
|
275
|
-
should 'raise an exception if the role redefines properties' do
|
276
|
-
@emp = Property::Role.new('empi')
|
277
|
-
@emp.property.string 'first_name'
|
278
|
-
assert_raise(Property::RedefinedPropertyError) do
|
279
|
-
subject.has_role @emp
|
280
|
-
end
|
281
|
-
end
|
282
|
-
|
283
|
-
should 'raise an exception if the role contains superclass methods' do
|
284
|
-
@emp = Property::Role.new('empi')
|
285
|
-
@emp.property.string 'method_in_parent'
|
286
|
-
assert_raise(Property::RedefinedMethodError) do
|
287
|
-
subject.has_role @emp
|
288
|
-
end
|
284
|
+
assert_raise(TypeError) { subject.include_role 'me' }
|
289
285
|
end
|
290
|
-
|
286
|
+
|
291
287
|
should 'inherit properties when asking to behave like a class' do
|
292
288
|
@class = Class.new(ActiveRecord::Base) do
|
293
289
|
include Property
|
@@ -295,10 +291,10 @@ class DeclarationTest < Test::Unit::TestCase
|
|
295
291
|
p.string 'hop'
|
296
292
|
end
|
297
293
|
end
|
298
|
-
|
299
|
-
subject.
|
294
|
+
|
295
|
+
subject.include_role @class
|
300
296
|
assert_equal %w{language last_name hop age first_name}, subject.schema.column_names
|
301
|
-
assert subject.has_role?(@class)
|
297
|
+
assert subject.has_role?(@class.schema)
|
302
298
|
end
|
303
299
|
end
|
304
300
|
|
@@ -321,12 +317,10 @@ class DeclarationTest < Test::Unit::TestCase
|
|
321
317
|
p.string 'first_name'
|
322
318
|
p.string 'famous', :default => :get_is_famous
|
323
319
|
p.integer 'age'
|
320
|
+
end
|
324
321
|
|
325
|
-
|
326
|
-
|
327
|
-
'no'
|
328
|
-
end
|
329
|
-
end
|
322
|
+
def get_is_famous
|
323
|
+
'no'
|
330
324
|
end
|
331
325
|
|
332
326
|
def version
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'fixtures'
|
3
|
+
|
4
|
+
class IndexFieldTest < ActiveSupport::TestCase
|
5
|
+
|
6
|
+
class Person < ActiveRecord::Base
|
7
|
+
include Property
|
8
|
+
set_table_name :employees
|
9
|
+
property.string 'name'
|
10
|
+
property.float 'age', :index => '.idx_float1'
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'A class with a field index' do
|
14
|
+
subject do
|
15
|
+
Person
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'on record creation' do
|
19
|
+
should 'succeed' do
|
20
|
+
assert_difference('Person.count', 1) do
|
21
|
+
Person.create('name' => 'Jake Sully', 'age' => 30)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
should 'save index in the model' do
|
26
|
+
person = Person.create('name' => 'Jake Sully', 'age' => 30)
|
27
|
+
assert_equal 30, Person.find(person.id).idx_float1
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'on record update' do
|
32
|
+
subject do
|
33
|
+
Person.create('name' => 'Jake Sully', 'age' => 30)
|
34
|
+
end
|
35
|
+
|
36
|
+
should 'update index entries' do
|
37
|
+
subject.update_attributes('age' => 15)
|
38
|
+
assert_equal 15, Person.find(subject).idx_float1
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'on index rebuild' do
|
43
|
+
subject do
|
44
|
+
p = Person.create('name' => 'Jake Sully', 'age' => 30)
|
45
|
+
Person.connection.execute "UPDATE #{Person.table_name} SET idx_float1 = NULL"
|
46
|
+
Person.find(p)
|
47
|
+
end
|
48
|
+
|
49
|
+
should 'rebuild field index' do
|
50
|
+
subject.rebuild_index!
|
51
|
+
assert_equal 30, Person.find(subject.id).idx_float1
|
52
|
+
end
|
53
|
+
end # on index rebuild
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
@@ -10,17 +10,10 @@ class RoleTest < ActiveSupport::TestCase
|
|
10
10
|
@poet = Property::Role.new('Poet') do |p|
|
11
11
|
p.string 'poem', :default => :muse
|
12
12
|
p.integer 'year'
|
13
|
-
|
14
|
-
p.actions do
|
15
|
-
def muse
|
16
|
-
'I am your muse'
|
17
|
-
end
|
18
|
-
end
|
19
13
|
end
|
20
14
|
end
|
21
15
|
|
22
|
-
|
23
|
-
should_add_role_methods
|
16
|
+
should_insert_properties_on_include_role_poet
|
24
17
|
should_take_part_in_used_list
|
25
18
|
should_not_maintain_indices # no indexed column defined
|
26
19
|
|
@@ -48,12 +41,10 @@ class RoleTest < ActiveSupport::TestCase
|
|
48
41
|
property do |p|
|
49
42
|
p.string 'poem', :default => :muse
|
50
43
|
p.integer 'year'
|
44
|
+
end
|
51
45
|
|
52
|
-
|
53
|
-
|
54
|
-
'I am your muse'
|
55
|
-
end
|
56
|
-
end
|
46
|
+
def muse
|
47
|
+
'I am your muse'
|
57
48
|
end
|
58
49
|
end
|
59
50
|
|
@@ -61,8 +52,7 @@ class RoleTest < ActiveSupport::TestCase
|
|
61
52
|
@poet = Foo
|
62
53
|
end
|
63
54
|
|
64
|
-
|
65
|
-
should_add_role_methods
|
55
|
+
should_insert_properties_on_include_role_poet
|
66
56
|
should_take_part_in_used_list
|
67
57
|
|
68
58
|
context 'set on a sub-class instance' do
|
@@ -71,7 +61,7 @@ class RoleTest < ActiveSupport::TestCase
|
|
71
61
|
end
|
72
62
|
|
73
63
|
should 'not raise an exception' do
|
74
|
-
assert_nothing_raised { subject.
|
64
|
+
assert_nothing_raised { subject.include_role Developer }
|
75
65
|
end
|
76
66
|
end # set on a sub-class instance
|
77
67
|
end # A class used as role
|
@@ -24,7 +24,92 @@ class StoredRoleTest < ActiveSupport::TestCase
|
|
24
24
|
@poet = Role.find(role.id)
|
25
25
|
end
|
26
26
|
|
27
|
-
|
27
|
+
|
28
|
+
# ========================== should_insert_properties_on_include_role_poet
|
29
|
+
|
30
|
+
context 'added' do
|
31
|
+
|
32
|
+
context 'to a parent class' do
|
33
|
+
setup do
|
34
|
+
@parent = Class.new(ActiveRecord::Base) do
|
35
|
+
set_table_name :dummies
|
36
|
+
include Property
|
37
|
+
property.string 'name'
|
38
|
+
|
39
|
+
def muse
|
40
|
+
'I am your muse'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
@klass = Class.new(@parent)
|
45
|
+
end
|
46
|
+
|
47
|
+
should 'propagate definitions to child' do
|
48
|
+
@parent.include_role @poet
|
49
|
+
assert_equal %w{name poem year}, @klass.schema.column_names.sort
|
50
|
+
end
|
51
|
+
|
52
|
+
should 'return true on has_role?' do
|
53
|
+
@parent.include_role @poet
|
54
|
+
assert @klass.has_role?(@poet)
|
55
|
+
end
|
56
|
+
|
57
|
+
should 'not raise an exception on double inclusion' do
|
58
|
+
@parent.include_role @poet
|
59
|
+
assert_nothing_raised { @parent.include_role @poet }
|
60
|
+
end
|
61
|
+
|
62
|
+
should 'add accessor methods to child' do
|
63
|
+
subject = @klass.new
|
64
|
+
assert_raises(NoMethodError) { subject.poem = 'Poe'}
|
65
|
+
@parent.include_role @poet
|
66
|
+
|
67
|
+
assert_nothing_raised { subject.poem = 'Poe'}
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'to a class' do
|
72
|
+
setup do
|
73
|
+
@klass = Class.new(ActiveRecord::Base) do
|
74
|
+
set_table_name :dummies
|
75
|
+
include Property
|
76
|
+
property.string 'name'
|
77
|
+
|
78
|
+
def muse
|
79
|
+
'I am your muse'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
should 'insert definitions' do
|
85
|
+
@klass.include_role @poet
|
86
|
+
assert_equal %w{name poem year}, @klass.schema.column_names.sort
|
87
|
+
end
|
88
|
+
|
89
|
+
should 'return true on class has_role?' do
|
90
|
+
@klass.include_role @poet
|
91
|
+
assert @klass.has_role?(@poet)
|
92
|
+
end
|
93
|
+
|
94
|
+
should 'return role from column' do
|
95
|
+
@klass.include_role @poet
|
96
|
+
assert_equal (@poet.kind_of?(Class) ? @poet.schema : @poet), @klass.schema.columns['poem'].role
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'to an instance' do
|
101
|
+
subject { Developer.new }
|
102
|
+
|
103
|
+
setup do
|
104
|
+
subject.include_role @poet
|
105
|
+
end
|
106
|
+
|
107
|
+
should 'merge property definitions' do
|
108
|
+
assert_equal %w{age first_name language last_name poem year}, subject.schema.column_names.sort
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
# ==========================
|
28
113
|
should_take_part_in_used_list(false)
|
29
114
|
should_not_maintain_indices # no indexed column defined
|
30
115
|
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: property
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
|
-
- 1
|
7
7
|
- 2
|
8
8
|
- 0
|
9
|
-
|
9
|
+
- 0
|
10
|
+
version: 2.0.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Renaud Kern
|
@@ -15,16 +16,18 @@ autorequire:
|
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2010-
|
19
|
+
date: 2010-11-10 00:00:00 +01:00
|
19
20
|
default_executable:
|
20
21
|
dependencies:
|
21
22
|
- !ruby/object:Gem::Dependency
|
22
23
|
name: shoulda
|
23
24
|
prerelease: false
|
24
25
|
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
25
27
|
requirements:
|
26
28
|
- - ">="
|
27
29
|
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
28
31
|
segments:
|
29
32
|
- 0
|
30
33
|
version: "0"
|
@@ -34,9 +37,11 @@ dependencies:
|
|
34
37
|
name: activerecord
|
35
38
|
prerelease: false
|
36
39
|
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
37
41
|
requirements:
|
38
42
|
- - ">="
|
39
43
|
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
40
45
|
segments:
|
41
46
|
- 0
|
42
47
|
version: "0"
|
@@ -93,6 +98,7 @@ files:
|
|
93
98
|
- test/unit/property/dirty_test.rb
|
94
99
|
- test/unit/property/index_complex_test.rb
|
95
100
|
- test/unit/property/index_custom_test.rb
|
101
|
+
- test/unit/property/index_field_test.rb
|
96
102
|
- test/unit/property/index_foreign_test.rb
|
97
103
|
- test/unit/property/index_simple_test.rb
|
98
104
|
- test/unit/property/role_test.rb
|
@@ -111,23 +117,27 @@ rdoc_options:
|
|
111
117
|
require_paths:
|
112
118
|
- lib
|
113
119
|
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
114
121
|
requirements:
|
115
122
|
- - ">="
|
116
123
|
- !ruby/object:Gem::Version
|
124
|
+
hash: 3
|
117
125
|
segments:
|
118
126
|
- 0
|
119
127
|
version: "0"
|
120
128
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
121
130
|
requirements:
|
122
131
|
- - ">="
|
123
132
|
- !ruby/object:Gem::Version
|
133
|
+
hash: 3
|
124
134
|
segments:
|
125
135
|
- 0
|
126
136
|
version: "0"
|
127
137
|
requirements: []
|
128
138
|
|
129
139
|
rubyforge_project: property
|
130
|
-
rubygems_version: 1.3.
|
140
|
+
rubygems_version: 1.3.7
|
131
141
|
signing_key:
|
132
142
|
specification_version: 3
|
133
143
|
summary: model properties wrap into a single database column
|
@@ -145,6 +155,7 @@ test_files:
|
|
145
155
|
- test/unit/property/dirty_test.rb
|
146
156
|
- test/unit/property/index_complex_test.rb
|
147
157
|
- test/unit/property/index_custom_test.rb
|
158
|
+
- test/unit/property/index_field_test.rb
|
148
159
|
- test/unit/property/index_foreign_test.rb
|
149
160
|
- test/unit/property/index_simple_test.rb
|
150
161
|
- test/unit/property/role_test.rb
|