property_sets 0.5.8 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,6 +2,6 @@ source "http://rubygems.org"
2
2
 
3
3
  gem 'activerecord-jdbcmysql-adapter', '1.2.2', :platforms => :jruby
4
4
  gem 'mysql', :platforms => :ruby
5
-
5
+ gem 'ruby-debug'
6
6
  gemspec
7
7
 
@@ -4,6 +4,7 @@ source "http://rubygems.org"
4
4
 
5
5
  gem "activerecord-jdbcmysql-adapter", "1.2.2", :platforms=>:jruby
6
6
  gem "mysql", :platforms=>:ruby
7
+ gem "ruby-debug"
7
8
  gem "activerecord", "2.3.14"
8
9
  gem "activesupport", "2.3.14"
9
10
  gem "actionpack", "2.3.14"
@@ -1,10 +1,11 @@
1
1
  PATH
2
- remote: /Users/primdahl/Git/property_sets
2
+ remote: /Users/ben/src/property_sets
3
3
  specs:
4
4
  property_sets (0.5.6)
5
5
  actionpack (>= 2.3.14, < 3.3)
6
6
  activerecord (>= 2.3.14, < 3.3)
7
7
  activesupport (>= 2.3.14, < 3.3)
8
+ json
8
9
 
9
10
  GEM
10
11
  remote: http://rubygems.org/
@@ -22,13 +23,23 @@ GEM
22
23
  appraisal (0.4.0)
23
24
  bundler
24
25
  rake
26
+ columnize (0.3.6)
25
27
  jdbc-mysql (5.1.13)
28
+ json (1.6.1)
29
+ linecache (0.46)
30
+ rbx-require-relative (> 0.0.4)
26
31
  metaclass (0.0.1)
27
32
  mocha (0.10.3)
28
33
  metaclass (~> 0.0.1)
29
34
  mysql (2.8.1)
30
35
  rack (1.1.3)
31
36
  rake (0.9.2.2)
37
+ rbx-require-relative (0.0.5)
38
+ ruby-debug (0.10.4)
39
+ columnize (>= 0.1)
40
+ ruby-debug-base (~> 0.10.4.0)
41
+ ruby-debug-base (0.10.4)
42
+ linecache (>= 0.3)
32
43
  shoulda (2.11.3)
33
44
 
34
45
  PLATFORMS
@@ -46,4 +57,5 @@ DEPENDENCIES
46
57
  mysql
47
58
  property_sets!
48
59
  rake
60
+ ruby-debug
49
61
  shoulda
@@ -4,6 +4,7 @@ source "http://rubygems.org"
4
4
 
5
5
  gem "activerecord-jdbcmysql-adapter", "1.2.2", :platforms=>:jruby
6
6
  gem "mysql", :platforms=>:ruby
7
+ gem "ruby-debug"
7
8
  gem "activerecord", "3.2.1"
8
9
  gem "activesupport", "3.2.1"
9
10
  gem "actionpack", "3.2.1"
@@ -1,10 +1,11 @@
1
1
  PATH
2
- remote: /Users/primdahl/Git/property_sets
2
+ remote: /Users/ben/src/property_sets
3
3
  specs:
4
4
  property_sets (0.5.6)
5
5
  actionpack (>= 2.3.14, < 3.3)
6
6
  activerecord (>= 2.3.14, < 3.3)
7
7
  activesupport (>= 2.3.14, < 3.3)
8
+ json
8
9
 
9
10
  GEM
10
11
  remote: http://rubygems.org/
@@ -39,11 +40,15 @@ GEM
39
40
  rake
40
41
  arel (3.0.0)
41
42
  builder (3.0.0)
43
+ columnize (0.3.6)
42
44
  erubis (2.7.0)
43
45
  hike (1.2.1)
44
46
  i18n (0.6.0)
45
47
  jdbc-mysql (5.1.13)
46
48
  journey (1.0.1)
49
+ json (1.6.1)
50
+ linecache (0.46)
51
+ rbx-require-relative (> 0.0.4)
47
52
  metaclass (0.0.1)
48
53
  mocha (0.10.3)
49
54
  metaclass (~> 0.0.1)
@@ -55,6 +60,12 @@ GEM
55
60
  rack-test (0.6.1)
56
61
  rack (>= 1.0)
57
62
  rake (0.9.2.2)
63
+ rbx-require-relative (0.0.5)
64
+ ruby-debug (0.10.4)
65
+ columnize (>= 0.1)
66
+ ruby-debug-base (~> 0.10.4.0)
67
+ ruby-debug-base (0.10.4)
68
+ linecache (>= 0.3)
58
69
  shoulda (2.11.3)
59
70
  sprockets (2.1.2)
60
71
  hike (~> 1.2)
@@ -78,4 +89,5 @@ DEPENDENCIES
78
89
  mysql
79
90
  property_sets!
80
91
  rake
92
+ ruby-debug
81
93
  shoulda
@@ -3,7 +3,7 @@ require 'property_sets/active_record_extension'
3
3
  require 'property_sets/action_view_extension'
4
4
 
5
5
  module PropertySets
6
- VERSION = "0.5.8"
6
+ VERSION = "0.6.0"
7
7
 
8
8
  def self.ensure_property_set_class(association, owner_class)
9
9
  const_name = "#{owner_class.name}#{association.to_s.singularize.capitalize}".to_sym
@@ -4,7 +4,7 @@ module PropertySets
4
4
  module ActiveRecordExtension
5
5
  module ClassMethods
6
6
 
7
- def property_set(association, &block)
7
+ def property_set(association, options = {}, &block)
8
8
  unless include?(PropertySets::ActiveRecordExtension::InstanceMethods)
9
9
  self.send(:include, PropertySets::ActiveRecordExtension::InstanceMethods)
10
10
  cattr_accessor :property_set_index
@@ -17,7 +17,8 @@ module PropertySets
17
17
  property_class = PropertySets.ensure_property_set_class(association, self)
18
18
  property_class.instance_eval(&block)
19
19
 
20
- has_many association, :class_name => property_class.name, :autosave => true, :dependent => :destroy do
20
+ hash_opts = {:class_name => property_class.name, :autosave => true, :dependent => :destroy}.merge(options)
21
+ has_many association, hash_opts do
21
22
 
22
23
  # Accepts a name value pair hash { :name => 'value', :pairs => true } and builds a property for each key
23
24
  def set(property_pairs, with_protection = false)
@@ -48,6 +49,7 @@ module PropertySets
48
49
  define_method "#{key}=" do |value|
49
50
  instance = lookup(key)
50
51
  instance.value = PropertySets::Casting.write(property_class.type(key), value)
52
+ instance.value
51
53
  end
52
54
 
53
55
  define_method "#{key}_record" do
@@ -55,6 +57,10 @@ module PropertySets
55
57
  end
56
58
  end
57
59
 
60
+ define_method :property_serialized? do |key|
61
+ property_class.type(key) == :serialized
62
+ end
63
+
58
64
  def save(*args)
59
65
  each { |p| p.save(*args) }
60
66
  end
@@ -87,6 +93,7 @@ module PropertySets
87
93
  def lookup(arg)
88
94
  instance = lookup_without_default(arg)
89
95
  instance ||= build_default(arg)
96
+ instance.validate_serialization = property_serialized?(arg)
90
97
 
91
98
  if ActiveRecord::VERSION::MAJOR == 3
92
99
  owner = proxy_association.owner
@@ -110,6 +117,8 @@ module PropertySets
110
117
  end
111
118
  association_class.new(:value => default(arg))
112
119
  end
120
+ instance.validate_serialization = property_serialized?(arg)
121
+ instance
113
122
  end
114
123
  end
115
124
  end
@@ -1,3 +1,5 @@
1
+ require 'json'
2
+
1
3
  module PropertySets
2
4
  module Casting
3
5
 
@@ -15,6 +17,8 @@ module PropertySets
15
17
  value.to_i
16
18
  when :boolean
17
19
  ![ "false", "0", "", "off", "n" ].member?(value.to_s.downcase)
20
+ when :serialized
21
+ JSON.parse(value)
18
22
  end
19
23
  end
20
24
 
@@ -28,6 +32,8 @@ module PropertySets
28
32
  else
29
33
  value.in_time_zone("UTC").to_s
30
34
  end
35
+ when :serialized
36
+ value.to_json
31
37
  else
32
38
  value.to_s
33
39
  end
@@ -28,6 +28,8 @@ module PropertySets
28
28
  value.to_s
29
29
  end
30
30
 
31
+ attr_accessor :validate_serialization
32
+
31
33
  private
32
34
 
33
35
  def validate_format_of_name
@@ -38,6 +40,12 @@ module PropertySets
38
40
  end
39
41
  end
40
42
 
43
+ def validate_length_of_serialized_data
44
+ if validate_serialization && self.class.columns_hash["value"].limit < self.value.size
45
+ errors.add(:value, :invalid)
46
+ end
47
+ end
48
+
41
49
  def coerce_value
42
50
  self.value = value.to_s unless value.nil?
43
51
  end
@@ -51,6 +59,7 @@ module PropertySets
51
59
  module ClassMethods
52
60
  def self.extended(base)
53
61
  base.validate :validate_format_of_name
62
+ base.validate :validate_length_of_serialized_data
54
63
  base.before_create :coerce_value
55
64
  end
56
65
 
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'property_sets'
16
- s.version = '0.5.8'
17
- s.date = '2012-02-08'
16
+ s.version = '0.6.0'
17
+ s.date = '2012-03-22'
18
18
  s.rubyforge_project = 'property_sets'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
@@ -50,6 +50,7 @@ Gem::Specification.new do |s|
50
50
  s.add_runtime_dependency("activesupport", ">= 2.3.14", "< 3.3")
51
51
  s.add_runtime_dependency("activerecord", ">= 2.3.14", "< 3.3")
52
52
  s.add_runtime_dependency("actionpack", ">= 2.3.14", "< 3.3")
53
+ s.add_runtime_dependency("json")
53
54
 
54
55
  ## List your development dependencies here. Development dependencies are
55
56
  ## those that are only needed during development
@@ -93,4 +94,4 @@ Gem::Specification.new do |s|
93
94
  ## Test files will be grabbed from the file list. Make sure the path glob
94
95
  ## matches what you actually use.
95
96
  s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
96
- end
97
+ end
@@ -69,5 +69,6 @@ class Account < ActiveRecord::Base
69
69
  property :datetime_prop, :type => :datetime
70
70
  property :float_prop, :type => :float
71
71
  property :int_prop, :type => :integer
72
+ property :serialized_prop, :type => :serialized
72
73
  end
73
74
  end
@@ -8,6 +8,10 @@ class TestCasting < ActiveSupport::TestCase
8
8
  assert_equal nil, PropertySets::Casting.read(:string, nil)
9
9
  assert_equal nil, PropertySets::Casting.read(:hello, nil)
10
10
  end
11
+
12
+ should "deserialize properly" do
13
+ assert_equal [1,2,3], PropertySets::Casting.read(:serialized, "[1, 2, 3]")
14
+ end
11
15
  end
12
16
 
13
17
  context "Casting#write" do
@@ -24,6 +28,10 @@ class TestCasting < ActiveSupport::TestCase
24
28
  should "convert integers to strings" do
25
29
  assert_equal "123", PropertySets::Casting.write(:integer, 123)
26
30
  end
31
+
32
+ should "serialize data marked as :serialize to json" do
33
+ assert_equal "[123]", PropertySets::Casting.write(:serialized, [123])
34
+ end
27
35
  end
28
36
 
29
37
  end
@@ -15,7 +15,16 @@ class TestPropertySets < ActiveSupport::TestCase
15
15
  end
16
16
 
17
17
  should "register the property sets used on a class" do
18
- assert_equal [ :settings, :texts, :validations, :typed_data ], Account.property_set_index
18
+ [ :settings, :texts, :validations, :typed_data ].each do |name|
19
+ assert Account.property_set_index.include?(name)
20
+ end
21
+ end
22
+
23
+ should "pass-through any options from the second parameter" do
24
+ Account.expects(:has_many).with { |association, h|
25
+ association == :foo && h[:conditions] == "bar"
26
+ }
27
+ Account.property_set(:foo, :conditions => "bar") {}
19
28
  end
20
29
 
21
30
  should "support protecting attributes" do
@@ -293,6 +302,22 @@ class TestPropertySets < ActiveSupport::TestCase
293
302
  assert_equal string_rep, @account.typed_data.lookup("datetime_prop").value
294
303
  end
295
304
  end
305
+
306
+ context "serialized data" do
307
+ should "store data in json" do
308
+ value = {:a => 1, :b => 2}
309
+ @account.typed_data.serialized_prop = value
310
+ @account.save!
311
+ @account.reload
312
+ assert_equal({'a' => 1, 'b' => 2}, @account.typed_data.serialized_prop)
313
+ end
314
+
315
+ should "not overflow the column" do
316
+ @account.typed_data.serialized_prop = (1..100_000).to_a
317
+ assert !@account.typed_data.lookup(:serialized_prop).valid?
318
+ assert !@account.save
319
+ end
320
+ end
296
321
  end
297
322
  end
298
323
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: property_sets
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 8
10
- version: 0.5.8
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Morten Primdahl
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-08 00:00:00 Z
18
+ date: 2012-03-22 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activesupport
@@ -87,7 +87,7 @@ dependencies:
87
87
  type: :runtime
88
88
  version_requirements: *id003
89
89
  - !ruby/object:Gem::Dependency
90
- name: rake
90
+ name: json
91
91
  prerelease: false
92
92
  requirement: &id004 !ruby/object:Gem::Requirement
93
93
  none: false
@@ -98,10 +98,10 @@ dependencies:
98
98
  segments:
99
99
  - 0
100
100
  version: "0"
101
- type: :development
101
+ type: :runtime
102
102
  version_requirements: *id004
103
103
  - !ruby/object:Gem::Dependency
104
- name: bundler
104
+ name: rake
105
105
  prerelease: false
106
106
  requirement: &id005 !ruby/object:Gem::Requirement
107
107
  none: false
@@ -115,7 +115,7 @@ dependencies:
115
115
  type: :development
116
116
  version_requirements: *id005
117
117
  - !ruby/object:Gem::Dependency
118
- name: shoulda
118
+ name: bundler
119
119
  prerelease: false
120
120
  requirement: &id006 !ruby/object:Gem::Requirement
121
121
  none: false
@@ -129,7 +129,7 @@ dependencies:
129
129
  type: :development
130
130
  version_requirements: *id006
131
131
  - !ruby/object:Gem::Dependency
132
- name: mocha
132
+ name: shoulda
133
133
  prerelease: false
134
134
  requirement: &id007 !ruby/object:Gem::Requirement
135
135
  none: false
@@ -143,7 +143,7 @@ dependencies:
143
143
  type: :development
144
144
  version_requirements: *id007
145
145
  - !ruby/object:Gem::Dependency
146
- name: appraisal
146
+ name: mocha
147
147
  prerelease: false
148
148
  requirement: &id008 !ruby/object:Gem::Requirement
149
149
  none: false
@@ -156,6 +156,20 @@ dependencies:
156
156
  version: "0"
157
157
  type: :development
158
158
  version_requirements: *id008
159
+ - !ruby/object:Gem::Dependency
160
+ name: appraisal
161
+ prerelease: false
162
+ requirement: &id009 !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ hash: 3
168
+ segments:
169
+ - 0
170
+ version: "0"
171
+ type: :development
172
+ version_requirements: *id009
159
173
  description: This gem is an ActiveRecord extension which provides a convenient interface for managing per row properties.
160
174
  email: primdahl@me.com
161
175
  executables: []
@@ -226,3 +240,4 @@ test_files:
226
240
  - test/test_casting.rb
227
241
  - test/test_property_sets.rb
228
242
  - test/test_view_extensions.rb
243
+ has_rdoc: