Module: Protobuf::ActiveRecord::Serialization::ClassMethods

Defined in:
lib/protobuf/active_record/serialization.rb

Instance Method Summary collapse

Instance Method Details

#_protobuf_field_optionsObject



23
24
25
# File 'lib/protobuf/active_record/serialization.rb', line 23

def _protobuf_field_options
  @_protobuf_field_options ||= {}
end

#_protobuf_field_symbol_transformersObject



27
28
29
# File 'lib/protobuf/active_record/serialization.rb', line 27

def _protobuf_field_symbol_transformers
  @_protobuf_field_symbol_transformers ||= {}
end

#_protobuf_field_transformersObject



31
32
33
# File 'lib/protobuf/active_record/serialization.rb', line 31

def _protobuf_field_transformers
  @_protobuf_field_transformers ||= {}
end

#_protobuf_message_deprecated_fieldsObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/protobuf/active_record/serialization.rb', line 35

def _protobuf_message_deprecated_fields
  @_protobuf_message_deprecated_fields ||= begin
    self.protobuf_message.all_fields.map do |field|
      next if field.nil?
      next unless field.deprecated?

      field.name.to_sym
    end
  end
end

#_protobuf_message_non_deprecated_fieldsObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/protobuf/active_record/serialization.rb', line 46

def _protobuf_message_non_deprecated_fields
  @_protobuf_message_non_deprecated_fields ||= begin
    self.protobuf_message.all_fields.map do |field|
      next if field.nil?
      next if field.deprecated?

      field.name.to_sym
    end
  end
end

#_protobuf_write_collection_assocation_method(field) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/protobuf/active_record/serialization.rb', line 142

def _protobuf_write_collection_assocation_method(field)
  self.class_eval "    def _protobuf_active_record_serialize_\#{field}\n      value = \#{field}.to_a # Terminate the association\n    rescue NameError # Has happened when field is not on model or ignored from db\n      return nil\n    end\n  RUBY\nend\n", __FILE__, __LINE__ + 1

#_protobuf_write_convert_to_fields_method(field) ⇒ Object



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
185
186
187
188
189
# File 'lib/protobuf/active_record/serialization.rb', line 152

def _protobuf_write_convert_to_fields_method(field)
  is_datetime_time_or_timestamp_column = _protobuf_date_datetime_time_or_timestamp_column?(field)
  is_date_column = _protobuf_date_column?(field)

  if is_datetime_time_or_timestamp_column
    if is_date_column
      self.class_eval "        def _protobuf_active_record_serialize_\#{field}\n          value = \#{field}\n          return nil if value.nil?\n\n          value.to_time(:utc).to_i\n        rescue NameError # Has happened when field is not on model or ignored from db\n          return nil\n        end\n      RUBY\n    else\n      self.class_eval <<-RUBY, __FILE__, __LINE__ + 1\n        def _protobuf_active_record_serialize_\#{field}\n          value = \#{field}\n          return nil if value.nil?\n\n          value.to_i\n        rescue NameError # Has happened when field is not on model or ignored from db\n          return nil\n        end\n      RUBY\n    end\n  else\n    self.class_eval <<-RUBY, __FILE__, __LINE__ + 1\n      def _protobuf_active_record_serialize_\#{field}\n        \#{field}\n      rescue NameError # Has happened when field is not on model or ignored from db\n        return nil\n      end\n    RUBY\n  end\nend\n", __FILE__, __LINE__ + 1

#_protobuf_write_field_transformer_method(field) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/protobuf/active_record/serialization.rb', line 191

def _protobuf_write_field_transformer_method(field)
  self.class_eval "    def _protobuf_active_record_serialize_\#{field}\n      self.class._protobuf_field_transformers[:\#{field}].call(self)\n    end\n  RUBY\nend\n", __FILE__, __LINE__ + 1

#_protobuf_write_nil_method(field) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/protobuf/active_record/serialization.rb', line 199

def _protobuf_write_nil_method(field)
  self.class_eval "    def _protobuf_active_record_serialize_\#{field}\n      nil\n    end\n  RUBY\nend\n", __FILE__, __LINE__ + 1

#_protobuf_write_symbol_transformer_method(field) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/protobuf/active_record/serialization.rb', line 207

def _protobuf_write_symbol_transformer_method(field)
  transformer_method = _protobuf_field_symbol_transformers[field]

  if self.respond_to?(transformer_method, true)
    if self.respond_to?(transformer_method, false)
      self.class_eval "        def _protobuf_active_record_serialize_\#{field}\n          self.class.\#{transformer_method}(self)\n        end\n      RUBY\n    else\n      self.class_eval <<-RUBY, __FILE__, __LINE__ + 1\n        def _protobuf_active_record_serialize_\#{field}\n          self.class.__send__(:\#{transformer_method}, self)\n        end\n      RUBY\n    end\n  else\n    self.class_eval <<-RUBY, __FILE__, __LINE__ + 1\n      def _protobuf_active_record_serialize_\#{field}\n        \#{transformer_method}\n      end\n    RUBY\n  end\nend\n", __FILE__, __LINE__ + 1

#field_from_record(field, transformer = nil, &block) ⇒ Object

Define a field transformation from a record. Accepts a Symbol, callable, or block that is called with the record being serialized.

When given a callable or block, it is directly used to convert the field.

When a symbol is given, it extracts the method with the same name.

The callable or method must accept a single parameter, which is the proto message.

Examples:

field_from_record :public_key, :convert_public_key_to_proto
field_from_record :status, lambda { |record| # Do some stuff... }
field_from_record :status do |record|
  # Do some blocky stuff...
end


74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/protobuf/active_record/serialization.rb', line 74

def field_from_record(field, transformer = nil, &block)
  if transformer.is_a?(Symbol)
    _protobuf_field_symbol_transformers[field] = transformer
    return
  end

  transformer ||= block
  callable = transformer
  unless callable.respond_to?(:call)
    raise FieldTransformerError
  end

  _protobuf_field_transformers[field.to_sym] = callable
end

#protobuf_fields(*fields) ⇒ Object

Define the protobuf fields that will be automatically serialized (by default, all fields will be serialized). Accepts any number of field names and is equivalent to passing the :only option to protobuf_message.

If :except is specified, all fields except the specified fields will be serialized.

By default, deprecated fields will be serialized. To exclude deprecated fields, pass :deprecated => false in the options hash.

Examples:

protobuf_fields :guid, :name
protobuf_fields :except => :email_domain
protobuf_fields :except => :email_domain, :deprecated => false


103
104
105
106
107
108
# File 'lib/protobuf/active_record/serialization.rb', line 103

def protobuf_fields(*fields)
  options = fields.extract_options!
  options[:only] = fields if fields.present?

  self._protobuf_field_options = options
end

#protobuf_message(message = nil, options = {}) ⇒ Object

Define the protobuf message class that should be used to serialize the object to protobuf. Accepts a string or symbol and an options hash.

When protobuf_message is declared, Protoable automatically extracts the fields from the message and automatically adds a to_proto method that serializes the object to protobuf.

The fields that will be automatically serialized can be configured by passing :only or :except in the options hash. If :only is specified, only the specified fields will be serialized. If :except is specified, all field except the specified fields will be serialized.

By default, deprecated fields will be serialized. To exclude deprecated fields, pass :deprecated => false in the options hash.

Examples:

protobuf_message :user_message
protobuf_message "UserMessage"
protobuf_message "Namespaced::UserMessage"
protobuf_message :user_message, :only => [ :guid, :name ]
protobuf_message :user_message, :except => :email_domain
protobuf_message :user_message, :except => :email_domain, :deprecated => false


133
134
135
136
137
138
139
140
# File 'lib/protobuf/active_record/serialization.rb', line 133

def protobuf_message(message = nil, options = {})
  unless message.nil?
    @protobuf_message = message.to_s.classify.constantize
    self._protobuf_field_options = options
  end

  @protobuf_message
end