Module: Protobuf::ActiveRecord::Serialization::ClassMethods
- Defined in:
- lib/protobuf/active_record/serialization.rb
Instance Method Summary collapse
-
#_protobuf_convert_attributes_to_fields(key, value) ⇒ Object
:nodoc:.
- #_protobuf_field_options ⇒ Object
- #_protobuf_field_transformers ⇒ Object
-
#field_from_record(field, transformer = nil, &block) ⇒ Object
Define a field transformation from a record.
-
#protobuf_fields(*fields) ⇒ Object
Define the protobuf fields that will be automatically serialized (by default, all fields will be serialized).
-
#protobuf_message(message = nil, options = {}) ⇒ Object
Define the protobuf message class that should be used to serialize the object to protobuf.
Instance Method Details
#_protobuf_convert_attributes_to_fields(key, value) ⇒ Object
:nodoc:
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/protobuf/active_record/serialization.rb', line 22 def _protobuf_convert_attributes_to_fields(key, value) return value if value.nil? value = case when _protobuf_date_column?(key) then value.to_time.to_i when _protobuf_datetime_column?(key) then value.to_i when _protobuf_time_column?(key) then value.to_i when (key) then value.to_i else value end return value end |
#_protobuf_field_options ⇒ Object
41 42 43 |
# File 'lib/protobuf/active_record/serialization.rb', line 41 def @_protobuf_field_options ||= {} end |
#_protobuf_field_transformers ⇒ Object
45 46 47 |
# File 'lib/protobuf/active_record/serialization.rb', line 45 def _protobuf_field_transformers @_protobuf_field_transformers ||= {} end |
#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
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/protobuf/active_record/serialization.rb', line 66 def field_from_record(field, transformer = nil, &block) transformer ||= block if transformer.is_a?(Symbol) callable = lambda { |value| self.__send__(transformer, value) } else callable = transformer end 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
96 97 98 99 100 101 |
# File 'lib/protobuf/active_record/serialization.rb', line 96 def protobuf_fields(*fields) = fields. [:only] = fields if fields.present? self. = 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:
:user_message
"UserMessage"
"Namespaced::UserMessage"
:user_message, :only => [ :guid, :name ]
:user_message, :except => :email_domain
:user_message, :except => :email_domain, :deprecated => false
126 127 128 129 130 131 132 133 |
# File 'lib/protobuf/active_record/serialization.rb', line 126 def ( = nil, = {}) unless .nil? @protobuf_message = .to_s.classify.constantize self. = end @protobuf_message end |