objectified_sessions 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 3ddbe03428f2939faa0bae986029767c4bcfa112
4
- data.tar.gz: 8f562df0cc6716bcf4262d413a54375e1eca5810
5
2
  SHA512:
6
- metadata.gz: f10073ecd7bf153818d79d20d8470641e1f84c9493b8e2ec67f7e61fe1a38ec16ebeee57416b2abe39e172e08cc398a341a0e332b6d328d65940b0db69c83157
7
- data.tar.gz: 39f589ecdb7e1422a4651843974c4c35bf10d720a9f3353eee551578b9d06fe12485a37e4f108410b3308526be2ded60c14b562671c73e7148314d2a0b7e7ed2
3
+ data.tar.gz: e57eb2d7c2aaebc88310d412bafa67bd20f99268153bac567234776f77169a027e495e5a9010fe1b86d8d5bbbef3f7b8c74c2cc3b4209435ecafdf1a9bb4245e
4
+ metadata.gz: d896af16837d0fbfdd9d3db4f4c2d66223fbbb60d975be3101355f5338907840c573d41b51389c0430f5ff46238e4f2425c912c4e1dbd4e0115f31f4e14c483f
5
+ SHA1:
6
+ data.tar.gz: 3aceba4aa45c04acb79fecfac7f63220bf55411f
7
+ metadata.gz: 63dcb0a0ba471ba0e15b3dd1f7ab4c06abc77505
@@ -186,13 +186,13 @@ module ObjectifiedSessions
186
186
  new_field = ObjectifiedSessions::FieldDefinition.new(self, name, options)
187
187
 
188
188
  # Check for a conflict with the field name.
189
- if @fields[new_field.name]
189
+ if (other_field = @fields[new_field.name]) && (other_field != new_field)
190
190
  raise ObjectifiedSessions::Errors::DuplicateFieldNameError.new(self, new_field.name)
191
191
  end
192
192
 
193
193
  # Check for a conflict with the storage name.
194
- if @fields_by_storage_name[new_field.storage_name]
195
- raise ObjectifiedSessions::Errors::DuplicateFieldStorageNameError.new(self, @fields_by_storage_name[new_field.storage_name].name, new_field.name, new_field.storage_name)
194
+ if (other_field = @fields_by_storage_name[new_field.storage_name]) && (other_field != new_field)
195
+ raise ObjectifiedSessions::Errors::DuplicateFieldStorageNameError.new(self, other_field.name, new_field.name, new_field.storage_name)
196
196
  end
197
197
 
198
198
  @fields[new_field.name] = new_field
@@ -37,6 +37,19 @@ module ObjectifiedSessions
37
37
  create_methods!
38
38
  end
39
39
 
40
+ # Allow field comparison. We do this when you define a field that has the exact same name or storage name as another
41
+ # field -- we allow it if (and only if) they are identical in every other way.
42
+ def ==(other)
43
+ return false unless other.kind_of?(ObjectifiedSessions::FieldDefinition)
44
+ session_class == other.send(:session_class) && name == other.name && storage_name == other.storage_name &&
45
+ type == other.send(:type) && visibility == other.send(:visibility)
46
+ end
47
+
48
+ # Make sure eql? works the same as ==.
49
+ def eql?(other)
50
+ self == other
51
+ end
52
+
40
53
  # Returns the key under which this field should read and write its data. This will be its name, unless a
41
54
  # +:storage+ option was passed to the constructor, in which case it will be that value, instead.
42
55
  def storage_name
@@ -56,7 +69,7 @@ module ObjectifiedSessions
56
69
  end
57
70
 
58
71
  private
59
- attr_reader :type, :visibility
72
+ attr_reader :type, :visibility, :session_class
60
73
 
61
74
  # Process the options passed in; this validates them, and sets +@type+, +@visibility+, and +@storage_name+
62
75
  # appropriately.
@@ -86,7 +99,7 @@ module ObjectifiedSessions
86
99
  return unless type == :normal
87
100
 
88
101
  fn = name
89
- dmm = @session_class._dynamic_methods_module
102
+ dmm = session_class._dynamic_methods_module
90
103
  mn = name.to_s.downcase
91
104
 
92
105
  dmm.define_method(mn) do
@@ -1,3 +1,3 @@
1
1
  module ObjectifiedSessions
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -35,6 +35,18 @@ describe "ObjectifiedSessions basic operations" do
35
35
  @controller_instance.objsession.foo.should == 234
36
36
  end
37
37
 
38
+ it "should let you redefine a field if it's exactly identical" do
39
+ define_objsession_class { field :foo, :visibility => :private, :storage => :baz }
40
+
41
+ @objsession_class.class_eval { field :foo, :visibility => :private, :storage => :baz }
42
+
43
+ lambda { @objsession_class.class_eval { field :foo, :visibility => :public, :storage => :baz } }.should raise_error(ObjectifiedSessions::Errors::DuplicateFieldNameError)
44
+ lambda { @objsession_class.class_eval { field :foo, :visibility => :private, :storage => :aaa } }.should raise_error(ObjectifiedSessions::Errors::DuplicateFieldNameError)
45
+ lambda { @objsession_class.class_eval { field :quux, :visibility => :public, :storage => :baz } }.should raise_error(ObjectifiedSessions::Errors::DuplicateFieldStorageNameError)
46
+ lambda { @objsession_class.class_eval { inactive :foo, :visibility => :private, :storage => :baz } }.should raise_error(ObjectifiedSessions::Errors::DuplicateFieldNameError)
47
+ lambda { @objsession_class.class_eval { retired :foo, :visibility => :private, :storage => :baz } }.should raise_error(ObjectifiedSessions::Errors::DuplicateFieldNameError)
48
+ end
49
+
38
50
  it "should allow hash access to the underlying session" do
39
51
  define_objsession_class do
40
52
  field :foo, :visibility => :private
@@ -171,11 +171,11 @@ describe "ObjectifiedSessions error handling" do
171
171
  e.message.should match(/baz/i)
172
172
  end
173
173
 
174
- it "should not let you define more than one field with the same name" do
174
+ it "should not let you define more than one field with the same name, but that differ" do
175
175
  e = capture_exception(ObjectifiedSessions::Errors::DuplicateFieldNameError) do
176
176
  define_objsession_class do
177
177
  field :foo
178
- field 'foo'
178
+ field 'foo', :visibility => :private
179
179
  end
180
180
  end
181
181
 
@@ -5,16 +5,22 @@ describe ObjectifiedSessions::FieldDefinition do
5
5
  ObjectifiedSessions::FieldDefinition
6
6
  end
7
7
 
8
- before :each do
9
- @session_class = double("session_class")
10
- allow(@session_class).to receive(:kind_of?).with(Class).and_return(true)
8
+ def new_session_class
9
+ out = double("session_class")
10
+ allow(out).to receive(:kind_of?).with(Class).and_return(true)
11
11
 
12
- @dmm = Module.new do
12
+ dmm = Module.new do
13
13
  class << self
14
14
  public :define_method, :private
15
15
  end
16
16
  end
17
- allow(@session_class).to receive(:_dynamic_methods_module).with().and_return(@dmm)
17
+ allow(out).to receive(:_dynamic_methods_module).with().and_return(dmm)
18
+ out
19
+ end
20
+
21
+ before :each do
22
+ @session_class = new_session_class
23
+ @dmm = @session_class._dynamic_methods_module
18
24
  end
19
25
 
20
26
  it "should normalize field names properly" do
@@ -58,6 +64,42 @@ describe ObjectifiedSessions::FieldDefinition do
58
64
  klass.new(@session_class, :foo, { :type => :retired, :visibility => :public }).allow_access_to_data?.should_not be
59
65
  end
60
66
 
67
+ [ "==", "eql?" ].each do |method_name|
68
+ describe method_name do
69
+ before :each do
70
+ @mn = method_name
71
+ @basis = klass.new(@session_class, :foo, { :type => :normal, :visibility => :public })
72
+ end
73
+
74
+ it "should match for fields that are identical" do
75
+ expect(@basis.send(@mn, klass.new(@session_class, :foo, { :type => :normal, :visibility => :public }))).to be_true
76
+ expect(@basis.send(@mn, klass.new(@session_class, :foo, { :type => :normal, :visibility => :public, :storage => :foo }))).to be_true
77
+ end
78
+
79
+ it "should not match if the session class is different" do
80
+ session_class_2 = new_session_class
81
+ expect(@basis.send(@mn, klass.new(session_class_2, :foo, { :type => :normal, :visibility => :public, :storage => :foo }))).not_to be_true
82
+ end
83
+
84
+ it "should not match if the name is different" do
85
+ expect(@basis.send(@mn, klass.new(@session_class, :bar, { :type => :normal, :visibility => :public }))).not_to be_true
86
+ end
87
+
88
+ it "should not match if the type is different" do
89
+ expect(@basis.send(@mn, klass.new(@session_class, :foo, { :type => :inactive, :visibility => :public }))).not_to be_true
90
+ expect(@basis.send(@mn, klass.new(@session_class, :foo, { :type => :retired, :visibility => :public }))).not_to be_true
91
+ end
92
+
93
+ it "should not match if the visibility is different" do
94
+ expect(@basis.send(@mn, klass.new(@session_class, :bar, { :type => :normal, :visibility => :private }))).not_to be_true
95
+ end
96
+
97
+ it "should not match if the storage is different" do
98
+ expect(@basis.send(@mn, klass.new(@session_class, :bar, { :type => :normal, :visibility => :public, :storage => :baz }))).not_to be_true
99
+ end
100
+ end
101
+ end
102
+
61
103
  context "with dynamic-methods module testing" do
62
104
  before :each do
63
105
  dmm = @dmm
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: objectified_sessions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Geweke