simple_eav 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/simple_eav.rb +17 -2
- data/lib/version.rb +1 -1
- data/spec/db/schema.rb +5 -0
- data/spec/lib/simple_eav_spec.rb +73 -30
- data/spec/spec_helper.rb +1 -0
- data/spec/support/child.rb +3 -0
- data/spec/support/person.rb +3 -1
- metadata +7 -3
data/lib/simple_eav.rb
CHANGED
@@ -16,10 +16,24 @@ module SimpleEav
|
|
16
16
|
self.class.simple_eav_column
|
17
17
|
end
|
18
18
|
|
19
|
+
private
|
20
|
+
def associations_of_class
|
21
|
+
self.class.reflect_on_all_associations.map{|assoc| assoc.name.to_sym }
|
22
|
+
end
|
23
|
+
|
19
24
|
def actual_columns_of_table
|
20
25
|
self.class.columns.map{|col| col.name.to_sym }
|
21
26
|
end
|
22
27
|
|
28
|
+
public
|
29
|
+
def reserved_attributes
|
30
|
+
associations_of_class + actual_columns_of_table
|
31
|
+
end
|
32
|
+
|
33
|
+
def reserved_attribute?(attribute)
|
34
|
+
reserved_attributes.include?(attribute)
|
35
|
+
end
|
36
|
+
|
23
37
|
def simple_eav_attributes
|
24
38
|
_attributes = self.send(simple_eav_column.to_sym)
|
25
39
|
_attributes.is_a?(Hash) ? _attributes : {}
|
@@ -35,7 +49,7 @@ module SimpleEav
|
|
35
49
|
# - remove undefined columns to prevent UnknownAttribute::Error from being thrown
|
36
50
|
simple_eav_attrs = read_attribute(simple_eav_column.to_sym) || {}
|
37
51
|
_attributes.each do |column,value|
|
38
|
-
next if
|
52
|
+
next if reserved_attribute?(column.to_sym)
|
39
53
|
simple_eav_attrs[column] = value
|
40
54
|
_attributes.delete(column)
|
41
55
|
end
|
@@ -44,7 +58,8 @@ module SimpleEav
|
|
44
58
|
end
|
45
59
|
|
46
60
|
def respond_to?(method, include_private=false)
|
47
|
-
|
61
|
+
return true if super(method, include_private)
|
62
|
+
simple_eav_attributes.has_key?(method)
|
48
63
|
end
|
49
64
|
|
50
65
|
def method_missing(method, *args, &block)
|
data/lib/version.rb
CHANGED
data/spec/db/schema.rb
CHANGED
data/spec/lib/simple_eav_spec.rb
CHANGED
@@ -12,10 +12,61 @@ describe SimpleEav do
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
+
describe "Reserved attributes" do
|
16
|
+
before(:each) do
|
17
|
+
@child = Child.create
|
18
|
+
@person = Person.create(:child=>@child)
|
19
|
+
end
|
20
|
+
it "knows the associations of the object" do
|
21
|
+
@person.send(:associations_of_class).should include(:child)
|
22
|
+
end
|
23
|
+
it "knows the reserved attributes" do
|
24
|
+
@person.should_receive(:associations_of_class).and_return([:child])
|
25
|
+
@person.should_receive(:actual_columns_of_table).and_return([:name])
|
26
|
+
@person.reserved_attributes.should eql([:child, :name])
|
27
|
+
end
|
28
|
+
it "does not accept reserved attributes for eav" do
|
29
|
+
@person.should_receive(:reserved_attributes).and_return([:name, :number])
|
30
|
+
@person.reserved_attribute?(:name).should be_true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
15
34
|
describe "Expected ActiveRecord behavior" do
|
16
|
-
|
17
|
-
|
18
|
-
|
35
|
+
describe "common" do
|
36
|
+
it "handles an empty string of attributes" do
|
37
|
+
person = Person.create(:simple_attributes=>'')
|
38
|
+
person.should_not be_new_record
|
39
|
+
end
|
40
|
+
it "sets all of the attributes" do
|
41
|
+
person = Person.create!({
|
42
|
+
:age=>99,
|
43
|
+
:simple_attributes=>{
|
44
|
+
:name=>'John'
|
45
|
+
}
|
46
|
+
})
|
47
|
+
person.age.should eql(99)
|
48
|
+
person.name.should eql('John')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
describe "associations" do
|
52
|
+
before(:each) do
|
53
|
+
@child = Child.create
|
54
|
+
@person = Person.create(:child=>@child)
|
55
|
+
end
|
56
|
+
it "assigns the has_one" do
|
57
|
+
@person.simple_eav_attributes.should_not have_key(:child)
|
58
|
+
@person.child.should eql(@child)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
describe "serialization" do
|
62
|
+
it "serializes and deserializes the simple_eav attributes" do
|
63
|
+
person = Person.new({:simple_attributes=>{
|
64
|
+
:name=>'John'
|
65
|
+
}})
|
66
|
+
person.save!
|
67
|
+
person.reload
|
68
|
+
person.name.should eql('John')
|
69
|
+
end
|
19
70
|
end
|
20
71
|
describe "given a hash of undefined attributes" do
|
21
72
|
it "creates a person with a name" do
|
@@ -52,24 +103,6 @@ describe SimpleEav do
|
|
52
103
|
person.simple_eav_attributes.should have_key :new_age
|
53
104
|
end
|
54
105
|
end
|
55
|
-
it "sets all of the attributes" do
|
56
|
-
person = Person.create!({
|
57
|
-
:age=>99,
|
58
|
-
:simple_attributes=>{
|
59
|
-
:name=>'John'
|
60
|
-
}
|
61
|
-
})
|
62
|
-
person.age.should eql(99)
|
63
|
-
person.name.should eql('John')
|
64
|
-
end
|
65
|
-
it "serializes and deserializes the simple_eav attributes" do
|
66
|
-
person = Person.new({:simple_attributes=>{
|
67
|
-
:name=>'John'
|
68
|
-
}})
|
69
|
-
person.save!
|
70
|
-
person.reload
|
71
|
-
person.name.should eql('John')
|
72
|
-
end
|
73
106
|
end
|
74
107
|
|
75
108
|
describe "#method_missing" do
|
@@ -79,6 +112,25 @@ describe SimpleEav do
|
|
79
112
|
end
|
80
113
|
end
|
81
114
|
|
115
|
+
describe "#respond_to" do
|
116
|
+
before(:each) do
|
117
|
+
@person = Person.new
|
118
|
+
end
|
119
|
+
|
120
|
+
it "the defined attribute `age` is responded to" do
|
121
|
+
@person.respond_to?(:age).should be_true
|
122
|
+
end
|
123
|
+
|
124
|
+
it "the custome attribute `name` is responded to" do
|
125
|
+
@person.name = 'John'
|
126
|
+
@person.respond_to?(:name).should be_true
|
127
|
+
end
|
128
|
+
|
129
|
+
it "someone else's name is not responded to" do
|
130
|
+
@person.respond_to?(:someone_elses_name).should be_false
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
82
134
|
describe "A missing attribute" do
|
83
135
|
before( :each ) do
|
84
136
|
@person = Person.new
|
@@ -122,15 +174,6 @@ describe SimpleEav do
|
|
122
174
|
@person.name = 'Joe'
|
123
175
|
@person.name.should eql('Joe')
|
124
176
|
end
|
125
|
-
|
126
|
-
it "who responds to his name" do
|
127
|
-
@person.name = 'John'
|
128
|
-
@person.respond_to?(:name).should be_true
|
129
|
-
end
|
130
|
-
|
131
|
-
it "who doesn't respond to someone else's name" do
|
132
|
-
@person.respond_to?(:someone_elses_name).should be_false
|
133
|
-
end
|
134
177
|
end
|
135
178
|
end
|
136
179
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/support/person.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: simple_eav
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.4.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Tim Linquist
|
@@ -10,7 +10,8 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-08-03 00:00:00 -07:00
|
14
|
+
default_executable:
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|
16
17
|
name: activerecord
|
@@ -78,7 +79,9 @@ files:
|
|
78
79
|
- spec/db/schema.rb
|
79
80
|
- spec/lib/simple_eav_spec.rb
|
80
81
|
- spec/spec_helper.rb
|
82
|
+
- spec/support/child.rb
|
81
83
|
- spec/support/person.rb
|
84
|
+
has_rdoc: true
|
82
85
|
homepage: http://github.com/timo3377/simple_eav
|
83
86
|
licenses: []
|
84
87
|
|
@@ -102,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
105
|
requirements: []
|
103
106
|
|
104
107
|
rubyforge_project:
|
105
|
-
rubygems_version: 1.
|
108
|
+
rubygems_version: 1.5.0
|
106
109
|
signing_key:
|
107
110
|
specification_version: 3
|
108
111
|
summary: A simple alternative to acts_as_eav_model.
|
@@ -110,4 +113,5 @@ test_files:
|
|
110
113
|
- spec/db/schema.rb
|
111
114
|
- spec/lib/simple_eav_spec.rb
|
112
115
|
- spec/spec_helper.rb
|
116
|
+
- spec/support/child.rb
|
113
117
|
- spec/support/person.rb
|