Class: Gapic::Presenters::FieldPresenter

Inherits:
Object
  • Object
show all
Includes:
Helpers::NamespaceHelper
Defined in:
lib/gapic/presenters/field_presenter.rb

Overview

A presenter for proto fields.

Instance Method Summary collapse

Methods included from Helpers::NamespaceHelper

#ensure_absolute_namespace, #fix_namespace, #ruby_namespace, #ruby_namespace_for_address

Constructor Details

#initialize(api, message, field) ⇒ FieldPresenter

Returns a new instance of FieldPresenter.



28
29
30
31
32
# File 'lib/gapic/presenters/field_presenter.rb', line 28

def initialize api, message, field
  @api = api
  @message = message
  @field = field
end

Instance Method Details

#as_kwarg(value: nil) ⇒ Object



61
62
63
# File 'lib/gapic/presenters/field_presenter.rb', line 61

def as_kwarg value: nil
  "#{name}: #{value || name}"
end

#camel_nameString

Name of this field, camel-cased

Returns:

  • (String)


131
132
133
# File 'lib/gapic/presenters/field_presenter.rb', line 131

def camel_name
  camel_name_for name
end

#default_valueObject



55
56
57
58
59
# File 'lib/gapic/presenters/field_presenter.rb', line 55

def default_value
  single = default_singular_value
  return "[#{single}]" if @field.repeated? && !@field.map?
  single
end

#default_value_for_typeString?

Returns a stringified default value for the protobuf types that are possible to fit into the query string parameter and nil for the other types (e.g. Messages)

Returns:

  • (String, nil)


114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gapic/presenters/field_presenter.rb', line 114

def default_value_for_type
  if @field.message?
    nil
  elsif @field.enum?
    ":#{@field.enum.values.first.name}"
  else
    case @field.type
    when 1, 2, 3, 4, 5, 6, 7, 13, 15, 16, 17, 18 then "0" # floating point or integer
    when 9, 12                                   then "\"\""
    when 8                                       then "false"
    end
  end
end

#doc_attribute_typeObject



42
43
44
45
# File 'lib/gapic/presenters/field_presenter.rb', line 42

def doc_attribute_type
  mode = @field.output_only? ? "r" : "rw"
  "@!attribute [#{mode}] #{@field.name}"
end

#doc_descriptionObject



51
52
53
# File 'lib/gapic/presenters/field_presenter.rb', line 51

def doc_description
  @field.docs_leading_comments
end

#doc_typesObject



38
39
40
# File 'lib/gapic/presenters/field_presenter.rb', line 38

def doc_types
  field_doc_types @field, false
end

#enum?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/gapic/presenters/field_presenter.rb', line 79

def enum?
  @field.enum?
end

#map?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/gapic/presenters/field_presenter.rb', line 95

def map?
  @field.map?
end

#message?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/gapic/presenters/field_presenter.rb', line 75

def message?
  @field.message?
end

#nameObject



34
35
36
# File 'lib/gapic/presenters/field_presenter.rb', line 34

def name
  @field.name
end

#oneof?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/gapic/presenters/field_presenter.rb', line 99

def oneof?
  @field.oneof?
end

#oneof_nameObject



103
104
105
# File 'lib/gapic/presenters/field_presenter.rb', line 103

def oneof_name
  @message.oneof_decl[@field.oneof_index].name
end

#output_doc_typesObject



47
48
49
# File 'lib/gapic/presenters/field_presenter.rb', line 47

def output_doc_types
  field_doc_types @field, true
end

#proto3_optional?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/gapic/presenters/field_presenter.rb', line 83

def proto3_optional?
  @field.proto3_optional?
end

#repeated?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/gapic/presenters/field_presenter.rb', line 87

def repeated?
  @field.repeated?
end

#required?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/gapic/presenters/field_presenter.rb', line 91

def required?
  @field.required?
end

#type_nameObject

TODO: remove, only used in tests



66
67
68
# File 'lib/gapic/presenters/field_presenter.rb', line 66

def type_name
  @field.type_name
end

#type_name_fullObject



70
71
72
73
# File 'lib/gapic/presenters/field_presenter.rb', line 70

def type_name_full
  return nil if type_name.blank?
  ruby_namespace @api, type_name
end