In Files

Cerealize::ClassMethods

Public Instance Methods

cerealize(property, klass=nil, opt={}) click to toggle source
     # File lib/cerealize.rb, line 78
 78:     def cerealize property, klass=nil, opt={}
 79:       opt[:encoding] ||= :marshal
 80:       cerealize_option[property] =
 81:         opt.merge(:class => klass,
 82:                   :codec => Cerealize.codec_get(opt[:encoding]))
 83: 
 84:       field_orig  = "#{property}_orig"
 85:       field_cache = "#{property}_cache"
 86: 
 87:       attr_accessor field_orig
 88:       private field_orig, "#{field_orig}="
 89: 
 90:       module_eval         def #{field_cache}          @#{property}        end        def #{field_cache}=(new_value)          @#{property} = new_value        end
 91: 
 92:       # Invariants:
 93:       #   - instance_variable_defined?(field_cache)  IFF the READER or WRITER has been called
 94:       #   - instance_variable_defined?(field_pre)    IFF the READER was called BEFORE
 95:       #                                              any WRITER
 96: 
 97:       # READER method
 98:       #
 99:       module_eval         def #{property}          # See if no assignment yet          if !#{field_cache}            # Save property if not already saved            if !#{field_orig}              self.#{field_orig}= self[:#{property}]            end            # Set cached from pre            v = cerealize_decode(:#{property}, #{field_orig})            raise ActiveRecord::SerializationTypeMismatch, "expected #{klass}, got \#{v.class}" \\              if #{klass || 'nil'} && !v.nil? && !v.kind_of?(#{klass})            self.#{field_cache} = v          end          # Return cached          #{field_cache}        end, __FILE__, __LINE__ + 1
100: 
101:       # WRITER method
102:       #
103:       module_eval         def #{property}=(v)          #{property}_will_change! if #{field_cache} != v          self.#{field_cache} = v        end, __FILE__, __LINE__ + 1
104: 
105:       # Callback for before_save
106:       #
107:       module_eval         def #{property}_update_if_dirty          # See if we have a new cur value          if #{field_cache}            v = #{field_cache}            v_enc = cerealize_encode(:#{property}, v)            # See if no pre at all (i.e. it was written to before being read),            # or if different. When comparing, compare both marshalized string,            # and Object ==.            #            if !#{field_orig} ||              (v_enc != #{field_orig} &&                   v != cerealize_decode(:#{property}, #{field_orig}))              self[:#{property}] = v_enc            end          end          self.#{field_orig}  = nil          self.#{field_cache} = nil        end, __FILE__, __LINE__ + 1
108: 
109:       before_save("#{property}_update_if_dirty")
110:     end
cerealize_option() click to toggle source
    # File lib/cerealize.rb, line 74
74:     def cerealize_option
75:       @cerealize_option ||= {}
76:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.