serializable_attributes 1.0.0 → 1.1.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/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  # SerializedAttributes
2
2
 
3
- SerializedAttributes allows you to add an encoded hash to an ActiveRecord model.
4
- This is similar to the built-in ActiveRecord serialization, except that the field
3
+ SerializedAttributes allows you to add an encoded hash to an ActiveRecord model.
4
+ This is similar to the built-in ActiveRecord serialization, except that the field
5
5
  is converted to JSON, gzipped, and stored in a BLOB field. This uses the json
6
6
  gem which is much faster than YAML serialization. However, JSON is not nearly as
7
7
  flexible, so you're stuck with strings/integers/dates/etc.
8
8
 
9
- Where possible, ActiveRecord compatible methods are generated so that a migration
9
+ Where possible, ActiveRecord compatible methods are generated so that a migration
10
10
  should be pretty simple. See unit tests for examples.
11
11
 
12
12
  Some of the code and most of the ideas are taken from [Heresy][Heresy], a ruby
@@ -57,4 +57,3 @@ class Profile < ActiveRecord::Base
57
57
  end
58
58
  end
59
59
  ```
60
-
@@ -31,7 +31,7 @@
31
31
  # end
32
32
  #
33
33
  module SerializableAttributes
34
- VERSION = "1.0.0"
34
+ VERSION = "1.1.0"
35
35
 
36
36
  require File.expand_path('../serializable_attributes/types', __FILE__)
37
37
  require File.expand_path('../serializable_attributes/schema', __FILE__)
@@ -93,6 +93,23 @@ module SerializableAttributes
93
93
  map! { |s| s.to_s }.sort!
94
94
  end
95
95
 
96
+ @model.send(:define_method, :read_attribute) do |attribute_name|
97
+ schema = self.class.send("#{data_field}_schema")
98
+ if schema.include?(attribute_name)
99
+ data[attribute_name.to_s]
100
+ else
101
+ super(attribute_name)
102
+ end
103
+ end
104
+
105
+ if defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::STRING >= '3.1'
106
+ @model.send(:define_method, :attributes) do
107
+ attributes = super().merge(send(data_field))
108
+ attributes.delete blob_field
109
+ attributes
110
+ end
111
+ end
112
+
96
113
  @model.send(:define_method, data_field) do
97
114
  instance_variable_get("@#{data_field}") || begin
98
115
  instance_variable_get("@#{changed_ivar}").clear if send("#{changed_ivar}?")
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
12
12
  ## If your rubyforge_project name is different, then edit it and comment out
13
13
  ## the sub! line in the Rakefile
14
14
  s.name = 'serializable_attributes'
15
- s.version = '1.0.0'
16
- s.date = '2012-05-14'
15
+ s.version = '1.1.0'
16
+ s.date = '2012-06-06'
17
17
  s.rubyforge_project = 'serializable_attributes'
18
18
 
19
19
  ## Make sure your summary is short. The description may be as long
@@ -91,6 +91,16 @@ formatters.each do |fmt|
91
91
  assert_equal %w(active age average birthday body extras lottery_picks names title), @record.attribute_names
92
92
  end
93
93
 
94
+ test "#read_attribute reads serialized fields" do
95
+ @record.body = 'a'
96
+ assert_equal 'a', @record.read_attribute(:body)
97
+ end
98
+
99
+ test "#attributes contains serialized fields" do
100
+ @record.body = 'a'
101
+ assert_equal 'a', @record.attributes['body']
102
+ end
103
+
94
104
  test "initialization does not call writers" do
95
105
  def @record.title=(v)
96
106
  raise ArgumentError
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serializable_attributes
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Rick Olson
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2012-05-14 00:00:00 Z
19
+ date: 2012-06-06 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: activerecord