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
160
161
162
163
# 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 field.deprecated? && exclude_deprecated

    field.name.to_sym
  end
  fields += [ options.fetch(:include, nil) ]
  fields.flatten!
  fields.compact!
  fields.uniq!

  fields
end

#_normalize_options(options) ⇒ Object

:nodoc:



166
167
168
169
170
171
172
# File 'lib/protobuf/active_record/serialization.rb', line 166

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:



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

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

#_protobuf_field_transformersObject

:nodoc:



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

def _protobuf_field_transformers
  self.class._protobuf_field_transformers
end

#_protobuf_messageObject

:nodoc:



215
216
217
# File 'lib/protobuf/active_record/serialization.rb', line 215

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)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/protobuf/active_record/serialization.rb', line 184

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:



220
221
222
223
224
225
# File 'lib/protobuf/active_record/serialization.rb', line 220

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

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