Class: ActiveRecord::SerializeJSON
- Inherits:
-
Object
- Object
- ActiveRecord::SerializeJSON
- Defined in:
- lib/active_record/serialize_json.rb,
lib/active_record/serialize_json/version.rb
Constant Summary collapse
- VERSION =
ActiveRecord::SerializeJSON version
'0.1.4'
- VERSION_ARRAY =
:nodoc:
VERSION.split(/\./).map { |x| x.to_i }
- VERSION_MAJOR =
:nodoc:
VERSION_ARRAY[0]
- VERSION_MINOR =
:nodoc:
VERSION_ARRAY[1]
- VERSION_BUILD =
:nodoc:
VERSION_ARRAY[2]
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
Class Method Summary collapse
Instance Method Summary collapse
- #after_save(record) ⇒ Object
- #before_save(record) ⇒ Object
- #deserialize(record) ⇒ Object
-
#initialize(attribute, opts = {}) ⇒ SerializeJSON
constructor
A new instance of SerializeJSON.
- #serialize(record) ⇒ Object
Constructor Details
#initialize(attribute, opts = {}) ⇒ SerializeJSON
Returns a new instance of SerializeJSON.
8 9 10 11 12 |
# File 'lib/active_record/serialize_json.rb', line 8 def initialize(attribute, opts = {}) @attribute = attribute.to_s.to_sym @serialize = opts[:serialize] || {} @deserialize = opts[:deserialize] || {} end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
14 15 16 |
# File 'lib/active_record/serialize_json.rb', line 14 def attribute @attribute end |
Class Method Details
.deserialize(value, opts = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/active_record/serialize_json.rb', line 46 def self.deserialize(value, opts = {}) opts ||= { :max_nesting => false, :allow_nan => true, :quirks_mode => true, } if value.to_s.strip.empty? nil else JSON.parse(value, opts) end rescue => e if defined?(::Rails) ::Rails.logger.warn e else warn "#{e.class}: #{e}" end value end |
.serialize(value, opts = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/active_record/serialize_json.rb', line 36 def self.serialize(value, opts = {}) opts ||= { :max_nesting => false, :allow_nan => true, :quirks_mode => true, } result = JSON(value, opts) result =~ /\A\s*[{\[]/ and result end |
Instance Method Details
#after_save(record) ⇒ Object
22 23 24 25 26 |
# File 'lib/active_record/serialize_json.rb', line 22 def after_save(record) data = deserialize record a = @attribute record.instance_eval { write_attribute(a, data) } end |
#before_save(record) ⇒ Object
16 17 18 19 20 |
# File 'lib/active_record/serialize_json.rb', line 16 def before_save(record) json = serialize record a = @attribute record.instance_eval { write_attribute(a, json) } end |
#deserialize(record) ⇒ Object
32 33 34 |
# File 'lib/active_record/serialize_json.rb', line 32 def deserialize(record) self.class.deserialize(record.read_attribute(@attribute), @deserialize) end |
#serialize(record) ⇒ Object
28 29 30 |
# File 'lib/active_record/serialize_json.rb', line 28 def serialize(record) self.class.serialize(record.read_attribute(@attribute), @serialize) end |