Module: Protobug::Compiler::Descriptor

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptorObject (readonly)

Returns the value of attribute descriptor.



62
63
64
# File 'lib/protobug/compiler.rb', line 62

def descriptor
  @descriptor
end

#fileObject (readonly)

Returns the value of attribute file.



62
63
64
# File 'lib/protobug/compiler.rb', line 62

def file
  @file
end

#parentObject (readonly)

Returns the value of attribute parent.



62
63
64
# File 'lib/protobug/compiler.rb', line 62

def parent
  @parent
end

#source_locObject (readonly)

Returns the value of attribute source_loc.



62
63
64
# File 'lib/protobug/compiler.rb', line 62

def source_loc
  @source_loc
end

Class Method Details

.included(base) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/protobug/compiler.rb', line 110

def self.included(base)
  base.extend(ClassMethods)
  base.class_eval do
    methods = []

    fields_by_name.each do |name, field|
      unless field.is_a?(Field::MessageField) &&
             /\Agoogle\.protobuf\.([^.]*Descriptor[^.]*)\z/ =~ field.message_type
        next
      end
      raise "expected #{self}.#{name} to be repeated" unless field.repeated?

      message_name = Regexp.last_match(1)

      methods << name

      define_method(name) do
        field_type = Compiler.const_get(message_name)
        super().map.with_index { |d, idx| field_type.new(d, self, path: @path + [field.number, idx]) }
      end
    end

    define_method(:each_declaration) do |&blk|
      return enum_for(__method__) unless blk # rubocop:disable Lint/ToEnumArguments

      decls = methods.flat_map { send(_1) }
      decls.sort_by! { |d| d.source_loc&.span || [] }
      decls.each(&blk)
      nil
    end
  end
end

Instance Method Details

#full_nameObject



88
89
90
91
92
93
94
# File 'lib/protobug/compiler.rb', line 88

def full_name
  if parent
    "#{parent.full_name}.#{name}"
  else
    descriptor.package
  end
end

#initialize(descriptor, parent, path:) ⇒ Object

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/protobug/compiler.rb', line 64

def initialize(descriptor, parent, path:)
  unless descriptor.is_a?(self.class.descriptor_class)
    raise ArgumentError,
          "#{descriptor.class} (#{descriptor}) is not #{self.class.descriptor_class}"
  end

  raise ArgumentError, "parent must be a Descriptor" if parent && !parent.is_a?(Descriptor)

  @descriptor = descriptor
  @parent = parent
  @file = parent&.file || self
  @path = path

  super(descriptor)

  @source_loc = if parent
                  file.source_code_info.location.find do |loc|
                    loc.path == path
                  end
                else
                  source_code_info.location[1]
                end
end

#to_constantObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/protobug/compiler.rb', line 96

def to_constant
  if parent
    "#{parent.to_constant}::#{name}"
  else
    return file.options.ruby_package if file.options&.ruby_package?

    parts = descriptor.package.split(".")
    parts.map! do |part|
      part.split("_").map(&:capitalize).join
    end
    parts.join("::")
  end
end