active_record_serialize_json 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,5 @@
1
+ 2011-05-27 - 0.1.0
2
+ * Now Rails 2 and 3 compatible
1
3
  2011-03-21 - 0.0.2
2
4
  * Use ActiveRecord::Base API methods to get/set attributes.
3
5
  2010-08-19 - 0.0.1
data/Rakefile CHANGED
@@ -44,7 +44,6 @@ if defined? Gem
44
44
 
45
45
  s.require_path = 'lib'
46
46
 
47
- s.has_rdoc = true
48
47
  s.rdoc_options = PKG_RDOC_OPTIONS
49
48
  s.extra_rdoc_files << 'README'
50
49
  #s.test_files << 'test/serialize_json_test.rb'
@@ -53,8 +52,8 @@ if defined? Gem
53
52
  s.email = "flori@ping.de"
54
53
  s.homepage = "http://flori.github.com/#{PKG_NAME}"
55
54
  s.rubyforge_project = "#{PKG_NAME}"
56
- s.add_dependency 'json', '~>1.4'
57
- s.add_dependency 'activerecord', '~>2.3'
55
+ s.add_dependency 'json', '~>1.0'
56
+ s.add_dependency 'activerecord'
58
57
  end
59
58
 
60
59
  Rake::GemPackageTask.new(spec) do |pkg|
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.1.0
@@ -13,12 +13,14 @@ module ActiveRecord
13
13
 
14
14
  def before_save(record)
15
15
  json = serialize record
16
- record.write_attribute(@attribute, json)
16
+ a = @attribute
17
+ record.instance_eval { write_attribute(a, json) }
17
18
  end
18
19
 
19
20
  def after_save(record)
20
21
  data = deserialize record
21
- record.write_attribute(@attribute, data)
22
+ a = @attribute
23
+ record.instance_eval { write_attribute(a, data) }
22
24
  end
23
25
 
24
26
  def serialize(record)
@@ -31,14 +33,18 @@ module ActiveRecord
31
33
 
32
34
  def self.serialize(value, opts = {})
33
35
  opts ||= {}
34
- JSON.generate(value, opts)
36
+ value.to_json(opts)
35
37
  end
36
38
 
37
39
  def self.deserialize(value, opts = {})
38
40
  opts ||= {}
39
41
  JSON.parse(value, opts)
40
42
  rescue => e
41
- Rails.logger.warn e
43
+ if defined?(::Rails)
44
+ ::Rails.logger.warn e
45
+ else
46
+ warn "#{e.class}: #{e}"
47
+ end
42
48
  value
43
49
  end
44
50
  end
@@ -47,8 +53,8 @@ module ActiveRecord
47
53
  def self.serialize_json(attribute, opts = {})
48
54
  sj = SerializeJSON.new(attribute, opts)
49
55
 
50
- before_save sj
51
56
  after_save sj
57
+ before_save sj
52
58
 
53
59
  unless respond_to?(:serialize_json_attributes)
54
60
  cattr_accessor :serialize_json_attributes
@@ -56,11 +62,23 @@ module ActiveRecord
56
62
  end
57
63
  serialize_json_attributes[sj.attribute] = sj
58
64
 
59
- class_eval do
60
- define_method(:after_find) do
61
- super if defined? super
62
- self.class.serialize_json_attributes.each do |attribute, sj|
63
- write_attribute(attribute, sj.deserialize(self))
65
+ if ::ActiveRecord::VERSION::MAJOR >= 3
66
+ class_eval do
67
+ after_find do |record|
68
+ serialize_json_attributes.each do |attribute, sj|
69
+ record.instance_eval do
70
+ write_attribute(attribute, sj.deserialize(record))
71
+ end
72
+ end
73
+ end
74
+ end
75
+ else
76
+ class_eval do
77
+ define_method(:after_find) do
78
+ super if defined? super
79
+ self.class.serialize_json_attributes.each do |attribute, sj|
80
+ write_attribute(attribute, sj.deserialize(self))
81
+ end
64
82
  end
65
83
  end
66
84
  end
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  module SerializeJSON
3
3
  # ActiveRecord::SerializeJSON version
4
- VERSION = '0.0.2'
4
+ VERSION = '0.1.0'
5
5
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
6
6
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
7
7
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -6,7 +6,7 @@ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
6
6
  require 'active_record/serialize_json'
7
7
 
8
8
  ActiveRecord::Base.establish_connection(
9
- :adapter => "mysql",
9
+ :adapter => ActiveRecord::VERSION::MAJOR >= 3 ? 'mysql2' : 'mysql',
10
10
  :database => ENV['DATABASE'] || "test",
11
11
  :username => ENV['USER'],
12
12
  :password => ENV['PASSWORD']
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 2
10
- version: 0.0.2
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Florian Frank
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-21 00:00:00 +01:00
18
+ date: 2011-05-27 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,11 +26,11 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- hash: 7
29
+ hash: 15
30
30
  segments:
31
31
  - 1
32
- - 4
33
- version: "1.4"
32
+ - 0
33
+ version: "1.0"
34
34
  type: :runtime
35
35
  version_requirements: *id001
36
36
  - !ruby/object:Gem::Dependency
@@ -39,13 +39,12 @@ dependencies:
39
39
  requirement: &id002 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
- - - ~>
42
+ - - ">="
43
43
  - !ruby/object:Gem::Version
44
- hash: 5
44
+ hash: 3
45
45
  segments:
46
- - 2
47
- - 3
48
- version: "2.3"
46
+ - 0
47
+ version: "0"
49
48
  type: :runtime
50
49
  version_requirements: *id002
51
50
  description: Serialize an ActiveRecord::Base attribute via JSON in Ruby on Rails