Sha256: c9e45d6051be3c8ee29d26d7343b40060cbef202ba99b825fdbea91bfe469f84
Contents?: true
Size: 1.84 KB
Versions: 2
Compression:
Stored size: 1.84 KB
Contents
require 'zlib' module JsonRecord class JsonField include AttributeMethods def initialize (record, name, schemas) @record = record @name = name @schemas = schemas @attributes = nil @compressed = record.class.columns_hash[name].type == :binary end def serialize if @attributes stripped_attributes = {} @attributes.each_pair{|k, v| stripped_attributes[k] = v unless v.blank?} json = stripped_attributes.to_json json = Zlib::Deflate.deflate(json) if json and @compressed @record[@name] = json end end def deserialize @attributes = {} @schemas.each do |schema| schema.fields.values.each do |field| @attributes[field.name] = field.multivalued? ? EmbeddedDocumentArray.new(field.type, @record) : field.default end end unless @record[@name].blank? json = @record[@name] json = Zlib::Inflate.inflate(json) if @compressed do_not_track_changes = Thread.current[:do_not_track_json_field_changes] Thread.current[:do_not_track_json_field_changes] = true begin ActiveSupport::JSON.decode(json).each_pair do |attr_name, attr_value| field = nil @schemas.each{|schema| field = schema.fields[attr_name]; break if field} field = FieldDefinition.new(attr_name, :type => attr_value.class) unless field write_attribute(field, attr_value, @record) end ensure Thread.current[:do_not_track_json_field_changes] = do_not_track_changes end end end def json_attributes deserialize unless @attributes @attributes end def changes @record.changes end def changed_attributes @record.send(:changed_attributes) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
json_record-1.0.6 | lib/json_record/json_field.rb |
json_record-1.0.5 | lib/json_record/json_field.rb |