property 2.3.3 → 2.3.4

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.
@@ -1,4 +1,10 @@
1
- == 2.3.3 2012-08-16
1
+ == 2.3.4 2013-10-17
2
+
3
+ * Minor enhancements
4
+ * Raising 'Property::DecodingError' in case of decoding failure.
5
+ * Added support for 'invalid_property_failover' to use as failover in case of decoding failure.
6
+
7
+ == 2.3.3 2013-08-16
2
8
 
3
9
  * Minor enhancements
4
10
  * Specifying rails dependency to avoid ruby version clash.
@@ -5,7 +5,7 @@ module Property
5
5
  #
6
6
  # The properties are encoded et decoded with a serialization tool than you can change by including
7
7
  # a Serialization module that should implement 'encode_properties' and 'decode_properties'.
8
- # The default is to use Marshal through Property::Serialization::Marshal.
8
+ # The default is to use JSON through Property::Serialization::JSON.
9
9
  #
10
10
  # The attributes= method filters native attributes and properties in order to store
11
11
  # them apart.
@@ -42,6 +42,8 @@ module Property
42
42
  end
43
43
  load_and_dump_methods =<<-EOF
44
44
  private
45
+ @@invalid_property_failover = nil
46
+
45
47
  def load_properties
46
48
  raw_data = #{accessor}read_attribute('properties')
47
49
  prop = raw_data ? decode_properties(raw_data) : Properties.new
@@ -49,6 +51,12 @@ module Property
49
51
  # type casting on write.
50
52
  prop.owner = self
51
53
  prop
54
+ rescue => err
55
+ if @@invalid_property_failover
56
+ Properties[@@invalid_property_failover]
57
+ else
58
+ raise Property::DecodingError.new err.message
59
+ end
52
60
  end
53
61
 
54
62
  def dump_properties
@@ -62,6 +70,11 @@ module Property
62
70
  end
63
71
  true
64
72
  end
73
+
74
+ def self.invalid_property_failover(failover)
75
+ raise "Invalid failover property value type: should be a Hash." unless failover.kind_of?(Hash) || failover == nil
76
+ @@invalid_property_failover = failover
77
+ end
65
78
  EOF
66
79
  class_eval(load_and_dump_methods, __FILE__, __LINE__)
67
80
  end
@@ -1,4 +1,8 @@
1
1
  module Property
2
2
  class Error < Exception
3
3
  end
4
+
5
+ # Raised in case property decoding fails and there is no invalid property default.
6
+ class DecodingError < ::Property::Error
7
+ end
4
8
  end
@@ -42,7 +42,7 @@ module Property
42
42
  properties.to_json
43
43
  end
44
44
 
45
- # Decode Marshal encoded properties
45
+ # Decode JSON encoded properties
46
46
  def decode_properties(string)
47
47
  ::JSON.parse(string)
48
48
  end
@@ -1,3 +1,3 @@
1
1
  module Property
2
- VERSION = '2.3.3'
2
+ VERSION = '2.3.4'
3
3
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{property}
8
- s.version = "2.3.3"
8
+ s.version = "2.3.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Renaud Kern", "Gaspard Bucher"]
12
- s.date = %q{2013-08-16}
12
+ s.date = %q{2013-10-17}
13
13
  s.description = %q{Wrap model properties into a single database column and declare properties from within the model.}
14
14
  s.email = %q{gaspard@teti.ch}
15
15
  s.extra_rdoc_files = [
@@ -57,6 +57,13 @@ class Test::Unit::TestCase
57
57
  assert_equal Property::Properties, properties.class
58
58
  assert_equal subject['string'], properties['string']
59
59
  end
60
+
61
+ should 'encode and decode with any chars' do
62
+ string = @obj.encode_properties(subject)
63
+ properties = @obj.decode_properties(string)
64
+ assert_equal Property::Properties, properties.class
65
+ assert_equal subject['string'], properties['string']
66
+ end
60
67
  end # with ascii 18
61
68
 
62
69
 
@@ -122,7 +122,6 @@ class AttributeTest < Test::Unit::TestCase
122
122
  assert_equal 'bar', subject.properties.delete('foo')
123
123
  assert_nil subject.properties['foo']
124
124
  end
125
-
126
125
  end
127
126
 
128
127
  context 'Retrieving' do
@@ -239,6 +238,65 @@ class AttributeTest < Test::Unit::TestCase
239
238
  assert_equal @cat, subject.prop['myserialized']
240
239
  end
241
240
  end
241
+
242
+ context 'invalid encoded property' do
243
+ subject do
244
+ klass = Class.new(ActiveRecord::Base) do
245
+ include Property
246
+ invalid_property_failover 'error' => 'Property invalid!', 'myfloat' => 0.0
247
+ set_table_name :dummies
248
+ property.float 'myfloat'
249
+ end
250
+
251
+ obj = klass.create('myfloat' => 78.9)
252
+ obj = klass.find(obj)
253
+ obj.instance_eval do
254
+ @attributes['properties'] = '{aaa:'
255
+ end
256
+ obj
257
+ end
258
+
259
+ should 'find invalid default' do
260
+ assert_equal({'error' => 'Property invalid!', 'myfloat' => 0.0}, subject.prop)
261
+ assert_equal Property::Properties, subject.prop.class
262
+ end
263
+
264
+ context 'without a default' do
265
+ subject do
266
+ klass = Class.new(ActiveRecord::Base) do
267
+ include Property
268
+ set_table_name :dummies
269
+ property.float 'myfloat'
270
+ end
271
+
272
+ obj = klass.create('myfloat' => 78.9)
273
+ obj = klass.find(obj)
274
+ obj.instance_eval do
275
+ @attributes['properties'] = '{aaa:'
276
+ end
277
+ obj
278
+ end
279
+
280
+ should 'raise an error' do
281
+ assert_raise(Property::DecodingError) do
282
+ subject.prop
283
+ end
284
+ end
285
+ end
286
+ end
287
+
288
+ context 'setting invalid failover' do
289
+
290
+ should 'not allow non-hash values' do
291
+ klass = Class.new(ActiveRecord::Base) do
292
+ include Property
293
+ invalid_property_failover 'error' => 'Property invalid!', 'myfloat' => 0.0
294
+ set_table_name :dummies
295
+ property.float 'myfloat'
296
+ end
297
+
298
+ end
299
+ end
242
300
  end
243
301
 
244
302
  context 'Setting attributes' do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: property
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
8
  - 3
9
- - 3
10
- version: 2.3.3
9
+ - 4
10
+ version: 2.3.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Renaud Kern
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2013-08-16 00:00:00 +02:00
19
+ date: 2013-10-17 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency