Module: Magicprotorb::Naming

Defined in:
lib/magicprotorb/naming.rb

Overview

Translates proto identifiers into Ruby constant names exactly the way protoc’s Ruby generator does, so the constants magicprotorb synthesizes are identical to those a checked-in ‘*_pb.rb` would define.

package my_co.sub_pkg.v1  ->  MyCo::SubPkg::V1
message OuterMsg          ->  OuterMsg            (nested under its parent)

Each dot-separated package segment is split on “_” and each part is capitalized (my_co -> MyCo, v1 -> V1); message/enum names keep their own casing with only the first letter forced upper.

Class Method Summary collapse

Class Method Details

.camelize(segment) ⇒ Object



24
25
26
# File 'lib/magicprotorb/naming.rb', line 24

def camelize(segment)
  segment.split("_").map { |part| part.empty? ? "" : part[0].upcase + part[1..] }.join
end

.constant_name(simple_name) ⇒ Object

Constant name for a message/enum simple name (first letter upper).



29
30
31
# File 'lib/magicprotorb/naming.rb', line 29

def constant_name(simple_name)
  simple_name[0].upcase + simple_name[1..]
end

.package_modules(package) ⇒ Object

“MyCo”, “SubPkg”, “V1”

for “my_co.sub_pkg.v1”; [] for an empty package.



18
19
20
21
22
# File 'lib/magicprotorb/naming.rb', line 18

def package_modules(package)
  return [] if package.nil? || package.empty?

  package.split(".").map { |segment| camelize(segment) }
end