Class: Protod::Proto::Package
- Includes:
- FieldCollectable, Findable, InterpreterBindable
- Defined in:
- lib/protod/proto/package.rb
Class Method Summary collapse
Instance Method Summary collapse
- #all_packages ⇒ Object
- #empty? ⇒ Boolean
- #external? ⇒ Boolean
- #freeze ⇒ Object
- #full_ident ⇒ Object
- #pb_const ⇒ Object
- #proto_path ⇒ Object
- #to_proto ⇒ Object
Methods included from InterpreterBindable
Methods included from FieldCollectable
Methods inherited from Part
#ancestor_as, #has?, #ident=, #push, #root
Class Method Details
.clear! ⇒ Object
32 33 34 |
# File 'lib/protod/proto/package.rb', line 32 def clear! @packages = nil end |
.find_or_register_package(full_ident, **attributes) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/protod/proto/package.rb', line 40 def find_or_register_package(full_ident, **attributes) full_ident.split('.').inject(nil) do |parent, ident| current_packages = parent ? parent.packages : roots current_packages.find { _1.ident == ident } || new.tap do _1.assign_attributes( parent: parent, ident: ident, for_ruby: parent&.for_ruby && "#{parent.for_ruby}::#{ident.camelize}", for_java: parent&.for_java && "#{parent.for_java}.#{ident}" ) current_packages.push(_1) end end.tap do _1.assign_attributes(**attributes.compact) if attributes.compact.present? end end |
.roots ⇒ Object
36 37 38 |
# File 'lib/protod/proto/package.rb', line 36 def roots @packages ||= [] end |
Instance Method Details
#all_packages ⇒ Object
72 73 74 |
# File 'lib/protod/proto/package.rb', line 72 def all_packages packages.flat_map(&:all_packages).tap { _1.unshift(self) } end |
#empty? ⇒ Boolean
76 77 78 |
# File 'lib/protod/proto/package.rb', line 76 def empty? services.empty? && .empty? end |
#external? ⇒ Boolean
80 81 82 |
# File 'lib/protod/proto/package.rb', line 80 def external? url.present? end |
#freeze ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/protod/proto/package.rb', line 84 def freeze services.each.with_index(1) { |s, i| s.index = i } .each.with_index(1) { |m, i| m.index = i } @package_map = self.class.findable_keys_for(:package).index_with { |k| all_packages.index_by(&k.to_sym) } @service_map = self.class.findable_keys_for(:service).index_with { |k| services.index_by(&k.to_sym) } @message_map = self.class.findable_keys_for(:message).index_with { |k| .index_by(&k.to_sym) } super end |
#full_ident ⇒ Object
64 65 66 |
# File 'lib/protod/proto/package.rb', line 64 def full_ident [parent&.full_ident, ident].compact.join('.').presence if ident end |
#pb_const ⇒ Object
68 69 70 |
# File 'lib/protod/proto/package.rb', line 68 def pb_const for_ruby&.constantize || full_ident.split('.').map(&:camelize).join('::').constantize end |
#proto_path ⇒ Object
60 61 62 |
# File 'lib/protod/proto/package.rb', line 60 def proto_path full_ident.gsub('.', '/').then { "#{_1}.proto" } end |
#to_proto ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/protod/proto/package.rb', line 95 def to_proto syntax_part = format_proto("syntax = \"proto3\";") package_part = format_proto("package %s;", full_ident) option_part = [ for_ruby ? format_proto("option ruby_package = \"%s\";", for_ruby) : nil, for_java ? format_proto("option java_package = \"%s\";", for_java) : nil, ].compact.join("\n").presence import_part = [ *collect_fields.filter_map(&:interpreter).uniq.filter_map(&:proto_path).uniq.reject { _1 == proto_path }, *imports ].uniq.sort.map { format_proto('import "%s";', _1) }.join("\n").presence = .map { _1.to_proto }.join("\n\n").presence service_part = services.map { _1.to_proto }.join("\n\n").presence [ [syntax_part, package_part, option_part, import_part, , service_part].compact.join("\n\n"), "\n" ].join end |