Module: Protobuf::Generate::Language::Helpers

Defined in:
lib/protobuf/generate/language.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



7
8
9
# File 'lib/protobuf/generate/language.rb', line 7

def filename
  @filename
end

#templateObject

Returns the value of attribute template.



7
8
9
# File 'lib/protobuf/generate/language.rb', line 7

def template
  @template
end

Instance Method Details

#type_count(*types) ⇒ Object



21
22
23
24
25
# File 'lib/protobuf/generate/language.rb', line 21

def type_count *types
  select{|e| e.kind_of?(Protobuf::Generate::Ast::Message)}.inject(0) do |count, message|
    count += message.fields.count{|f| types.map(&:to_s).include?(f.type)}
  end
end

#type_enum?(type) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/protobuf/generate/language.rb', line 13

def type_enum? type
  !!find{|e| e.kind_of?(Protobuf::Generate::Ast::Enum) && e.name == type }
end

#type_enum_default(type, default) ⇒ Object



27
28
29
30
# File 'lib/protobuf/generate/language.rb', line 27

def type_enum_default type, default
  enum = find{|e| e.kind_of?(Protobuf::Generate::Ast::Enum) && e.name == type} # TODO: Or raise unknown type.
  (enum.fields.find{|f| f.name == default.to_s} || enum.fields.first).name
end

#type_message?(type) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/protobuf/generate/language.rb', line 9

def type_message? type
  !!find{|e| e.kind_of?(Protobuf::Generate::Ast::Message) && e.name == type }
end

#type_wire(type) ⇒ Object



17
18
19
# File 'lib/protobuf/generate/language.rb', line 17

def type_wire type
  Hash[*%w{int32 0 int64 0 sint32 0 sint64 0 uint32 0 uint64 0 string 2 bool 0 float 5 double 1 fixed32 5 fixed64 1 sfixed32 5 sfixed64 1 bytes 2}][type]
end