Module: Cerealize::ClassMethods

Defined in:
lib/cerealize.rb,
lib/cerealize/attr_hash.rb

Instance Method Summary collapse

Instance Method Details

#attr_hash(property, attrs) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cerealize/attr_hash.rb', line 4

def attr_hash property, attrs
  ruby = attrs.inject([]){ |codes, attr|
    codes << <<-RUBY
      def #{attr}
        if #{property}
          #{property}[:#{attr}]
        else
          nil
        end
      end

      def #{attr}= value
        self.#{property} ||= {}

        # this line fixes the quirks in ActiveRecord 2.3.9 at
        # activerecord-2.3.9/lib/active_record/associations/association_collection.rb:L352-L363
        # when we're using associations and STI, thanks Jaime
        # TODO: test for this?
        #{property}_will_change! if
          respond_to?(:#{property}_will_change!) &&
          #{property}[:#{attr}] != value

        #{property}[:#{attr}] = value
      end
    RUBY
  }.join("\n")

  mod = if const_defined?(Cerealize::InternalName)
          const_get(Cerealize::InternalName)
        else
          const_set(Cerealize::InternalName, Module.new)
        end

  mod.module_eval(ruby, __FILE__, __LINE__)
  include mod unless self < mod
end

#cerealize(property, klass = nil, opt = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/cerealize.rb', line 88

def cerealize property, klass=nil, opt={}
  opt[:encoding] ||= :marshal
  cerealize_option[property] =
    opt.merge(:class => klass,
              :codec => Cerealize.codec_get(opt[:encoding]))

  field_orig  = "#{property}_orig"
  field_cache = "#{property}_cache"

  attr_accessor field_orig
  private field_orig, "#{field_orig}="

  mod = if const_defined?(Cerealize::InternalName)
          const_get(Cerealize::InternalName)
        else
          const_set(Cerealize::InternalName, Module.new)
        end

  mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{field_cache}
      if defined?(@#{property})
        @#{property}
      else
        # define @#{property} to avoid mutual recursion
        @#{property} = nil
        @#{property} = #{property}
      end
    end

    def #{field_cache}=(new_value)
      @#{property} = new_value
    end
  RUBY

  # Invariants:
  #   - instance_variable_defined?(field_cache)  IFF the READER or WRITER has been called
  #   - instance_variable_defined?(field_pre)    IFF the READER was called BEFORE
  #                                              any WRITER

  # READER method
  #
  mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{property}
      # Return cached
      return #{field_cache} if defined?(@#{property}) && #{field_cache}

      # No assignment yet, save property if not already saved
      self.#{field_orig}= self[:#{property}] if !#{field_orig}

      # Set cached from pre
      value = cerealize_decode(:#{property}, #{field_orig})

      raise ActiveRecord::SerializationTypeMismatch, "expected #{klass}, got \#{value.class}" \\
        if #{klass.inspect} && !value.nil? && !value.kind_of?(#{klass})

      self.#{field_cache} = value
    end
  RUBY

  # WRITER method
  #
  mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{property}=(value)
      #{property}_will_change! if #{field_cache} != value
      self.#{field_cache} = value
    end
  RUBY

  # Callback for before_save
  #
  mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{property}_update_if_dirty
      # See if we have a new cur value
      if instance_variable_defined?('@#{property}')
        value     = #{field_cache}
        value_enc = cerealize_encode(:#{property}, value)

        # See if no orig 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} ||
          (value_enc != #{field_orig} &&
           value     != cerealize_decode(:#{property}, #{field_orig}))
          self[:#{property}] = value_enc
        end

        remove_instance_variable('@#{property}')
      end

      self.#{field_orig} = nil
    end
  RUBY

  include mod unless self < mod
  before_save("#{property}_update_if_dirty")
end

#cerealize_update_codec_cacheObject



82
83
84
85
86
# File 'lib/cerealize.rb', line 82

def cerealize_update_codec_cache
  cerealize_option.each{ |(property, opt)|
    opt.merge!(:codec => Cerealize.codec_get(opt[:encoding]))
  }
end