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 |
# 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 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 () ||= {} [:only] ||= [] if .fetch(:except, false) [:except] ||= [] if .fetch(:only, false) self.class..merge() 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_transformers ⇒ Object
:nodoc:
206 207 208 |
# File 'lib/protobuf/active_record/serialization.rb', line 206 def _protobuf_field_transformers self.class._protobuf_field_transformers end |
#_protobuf_message ⇒ Object
:nodoc:
211 212 213 |
# File 'lib/protobuf/active_record/serialization.rb', line 211 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)
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( = {}) 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:
216 217 218 219 220 221 |
# File 'lib/protobuf/active_record/serialization.rb', line 216 def to_proto( = {}) raise MessageNotDefined.new(self.class) if .nil? fields = self.fields_from_record() .new(fields) end |