Module: Pb::Serializable::Dsl

Included in:
Pb::Serializable
Defined in:
lib/pb/serializable/dsl.rb,
lib/pb/serializable/dsl/oneof.rb,
lib/pb/serializable/dsl/attribute.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, opts = {}) ⇒ void

This method returns an undefined value.

Parameters:

  • name (Symbol)

    An attribute name

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :allow_nil (Boolean)

    Set true if this attribute allow to be nil

  • :serializer (Class)

    A serializer class for this attribute

  • :if (String, Symbol, Proc)

    A method, proc or string to call to determine to serialize this field

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pb/serializable/dsl.rb', line 22

def attribute(name, opts = {})
  raise ::Pb::Serializer::MissingMessageTypeError, "message specificaiton is missed" unless __pb_serializer_message_class

  fd = __pb_serializer_message_class.descriptor.find { |fd| fd.name.to_sym == name }

  raise ::Pb::Serializer::UnknownFieldError, "#{name} is not defined in #{ __pb_serializer_message_class.name}" unless fd

  attr = Attribute.new(
    name: name,
    options: opts,
    field_descriptor: fd,
    oneof: @current_oneof&.name,
  )

  __pb_serializer_attr_by_name[name] = attr

  unless method_defined?(attr.name)
    define_method attr.name do
      primary_object.public_send(attr.name)
    end
  end
end

#ignore(*names) ⇒ void

This method returns an undefined value.

Examples:

Ignore attributes

ignore :deprecated_field, :not_implemented_field

Parameters:

  • names (Array<Symbol>)

    Attribute names to be ignored



49
50
51
52
53
# File 'lib/pb/serializable/dsl.rb', line 49

def ignore(*names)
  names.each do |name|
    attribute name, ignore: true
  end
end

#message(klass) ⇒ void

This method returns an undefined value.

Parameters:

  • klass (Class)

    Protobuf message class



9
10
11
# File 'lib/pb/serializable/dsl.rb', line 9

def message(klass)
  self.__pb_serializer_message_class = klass
end

#oneof(name, allow_nil: false) ⇒ void

This method returns an undefined value.

Examples:

Define oneof attributes

oneof :test_oneof do
  attribute :name
  attribute :sub_message
end

Parameters:

  • name (Symbol)

    An oneof attribute name

  • allow_nil (Boolean) (defaults to: false)

    Set true if this oneof attribute allow to be nil



63
64
65
66
67
68
69
70
71
72
# File 'lib/pb/serializable/dsl.rb', line 63

def oneof(name, allow_nil: false)
  @current_oneof = Oneof.new(
    name: name,
    allow_nil: allow_nil,
    attributes: [],
  )
  yield
  __pb_serializer_oneof_by_name[name] = @current_oneof
  @current_oneof = nil
end