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
-
#_filter_field_attributes(options = {}) ⇒ Object
:nodoc:.
-
#_filtered_fields(options = {}) ⇒ Object
:nodoc:.
-
#_normalize_options(options) ⇒ Object
:nodoc:.
-
#_protobuf_convert_attributes_to_fields(field, value) ⇒ Object
:nodoc:.
-
#_protobuf_field_transformers ⇒ Object
:nodoc:.
-
#_protobuf_message ⇒ Object
:nodoc:.
-
#fields_from_record(options = {}) ⇒ Object
Extracts attributes that correspond to fields on the specified protobuf message, performing any necessary column conversions on them.
-
#to_proto(options = {}) ⇒ Object
:nodoc:.
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( = {}) = () fields = _filtered_fields() fields &= [ [:only] ].flatten if [:only].present? fields -= [ [:except] ].flatten if [: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( = {}) exclude_deprecated = ! .fetch(:deprecated, true) fields = self.class..all_fields.map do |field| next if field.nil? next if field.deprecated? && exclude_deprecated field.name.to_sym end fields += [ .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 () ||= {} [:only] ||= [] if .fetch(:except, false) [:except] ||= [] if .fetch(:only, false) self.class..merge() 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_transformers ⇒ Object
:nodoc:
210 211 212 |
# File 'lib/protobuf/active_record/serialization.rb', line 210 def _protobuf_field_transformers self.class._protobuf_field_transformers end |
#_protobuf_message ⇒ Object
:nodoc:
215 216 217 |
# File 'lib/protobuf/active_record/serialization.rb', line 215 def self.class. 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( = {}) field_attributes = _filter_field_attributes() field_attributes += [ .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:
220 221 222 223 224 225 |
# File 'lib/protobuf/active_record/serialization.rb', line 220 def to_proto( = {}) raise MessageNotDefined.new(self.class) if .nil? fields = self.fields_from_record() .new(fields) end |