Sha256: f5a60f987695ae471e6d19ad7f8ffe7ddc0535729a550ad68f84db27df8fa195

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 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, self) : 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

1 entries across 1 versions & 1 rubygems

Version Path
json_record-1.0.4 lib/json_record/json_field.rb