module TwirpRails::ActiveRecordExtension

Public Instance Methods

to_twirp(*args) click to toggle source
# File lib/twirp_rails/active_record_extension.rb, line 19
def to_twirp(*args)
  all.map { |entity| entity.to_twirp(*args) }
end
twirp_message(message_class) click to toggle source

Using to set twirp class used by to_twirp method @example twirp_message TwirpModel

# File lib/twirp_rails/active_record_extension.rb, line 9
def twirp_message(message_class)
  @twirp_message = message_class
end
twirp_message_class() click to toggle source
# File lib/twirp_rails/active_record_extension.rb, line 13
def twirp_message_class
  @twirp_message = @twirp_message.constantize if @twirp_message.is_a?(String)

  @twirp_message
end

Private Instance Methods

to_twirp_as_class(klass) click to toggle source
# File lib/twirp_rails/active_record_extension.rb, line 42
def to_twirp_as_class(klass)
  unless klass.respond_to?(:descriptor)
    raise "Class #{klass} must be a protobuf message class"
  end

  # TODO performance optimization needed
  to_twirp_as_fields(klass.descriptor.map &:name)
end
to_twirp_as_fields(fields) click to toggle source
# File lib/twirp_rails/active_record_extension.rb, line 51
def to_twirp_as_fields(fields)
  fields.each_with_object({}) do |field, h|
    h[field] = attributes.fetch(field) { public_send(field) }
  end
end