Module: Protobuf::ActiveRecord::Serialization

Extended by:
ActiveSupport::Concern
Defined in:
lib/protobuf/active_record/serialization.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_filter_field_attributes(options = {}) ⇒ Object

:nodoc:



137
138
139
140
141
142
143
144
145
# File 'lib/protobuf/active_record/serialization.rb', line 137

def _filter_field_attributes(options = {})
  options = _normalize_options(options)

  fields = _filtered_fields(options)
  fields &= [ options[:only] ].flatten if options[:only].present?
  fields -= [ options[:except] ].flatten if options[:except].present?

  fields
end

#_filtered_fields(options = {}) ⇒ Object

:nodoc:



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/protobuf/active_record/serialization.rb', line 148

def _filtered_fields(options = {})
  exclude_deprecated = ! options.fetch(:deprecated, true)

  fields = self.class.protobuf_message.all_fields.map do |field|
    next if field.nil?
    next if exclude_deprecated && field.deprecated?
    field.name.to_sym
  end
  fields.compact!

  fields
end

#_normalize_options(options) ⇒ Object

:nodoc:



162
163
164
165
166
167
168
# File 'lib/protobuf/active_record/serialization.rb', line 162

def _normalize_options(options)
  options ||= {}
  options[:only] ||= [] if options.fetch(:except, false)
  options[:except] ||= [] if options.fetch(:only, false)

  self.class._protobuf_field_options.merge(options)
end

#_protobuf_convert_attributes_to_fields(field, value) ⇒ Object

:nodoc:



201
202
203
# File 'lib/protobuf/active_record/serialization.rb', line 201

def _protobuf_convert_attributes_to_fields(field, value)
  self.class._protobuf_convert_attributes_to_fields(field, value)
end

#_protobuf_field_transformersObject

:nodoc:



206
207
208
# File 'lib/protobuf/active_record/serialization.rb', line 206

def _protobuf_field_transformers
  self.class._protobuf_field_transformers
end

#_protobuf_messageObject

:nodoc:



211
212
213
# File 'lib/protobuf/active_record/serialization.rb', line 211

def _protobuf_message
  self.class.protobuf_message
end

#fields_from_record(options = {}) ⇒ Object

Extracts attributes that correspond to fields on the specified protobuf message, performing any necessary column conversions on them. Accepts a hash of options for specifying which fields should be serialized.

Examples:

fields_from_record(:only => [ :guid, :name ])
fields_from_record(:except => :email_domain)
fields_from_record(:include => :email_domain)
fields_from_record(:except => :email_domain, :deprecated => false)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/protobuf/active_record/serialization.rb', line 180

def fields_from_record(options = {})
  field_attributes = _filter_field_attributes(options)
  field_attributes += [ options.fetch(:include, []) ]
  field_attributes.flatten!
  field_attributes.compact!
  field_attributes.uniq!

  field_attributes = field_attributes.inject({}) do |hash, field|
    if _protobuf_field_transformers.has_key?(field)
      hash[field] = _protobuf_field_transformers[field].call(self)
    else
      value = respond_to?(field) ? __send__(field) : nil
      hash[field] = _protobuf_convert_attributes_to_fields(field, value)
    end
    hash
  end

  field_attributes
end

#to_proto(options = {}) ⇒ Object

:nodoc:

Raises:



216
217
218
219
220
221
# File 'lib/protobuf/active_record/serialization.rb', line 216

def to_proto(options = {})
  raise MessageNotDefined.new(self.class) if _protobuf_message.nil?

  fields = self.fields_from_record(options)
  _protobuf_message.new(fields)
end