Class: Protobuf::Visitor::CreateMessageVisitor

Inherits:
Base
  • Object
show all
Defined in:
lib/protobuf/compiler/visitors.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#silent

Instance Method Summary collapse

Methods inherited from Base

#create_file_with_backup, #log_writing

Constructor Details

#initialize(proto_file = nil, proto_dir = '.', out_dir = '.') ⇒ CreateMessageVisitor

Returns a new instance of CreateMessageVisitor.



38
39
40
41
42
43
44
# File 'lib/protobuf/compiler/visitors.rb', line 38

def initialize(proto_file=nil, proto_dir='.', out_dir='.')
  @proto_dir, @out_dir = proto_dir, out_dir
  @indent = 0
  @context = []
  @attach_proto = false
  @proto_file = proto_file
end

Instance Attribute Details

#attach_protoObject

Returns the value of attribute attach_proto.



36
37
38
# File 'lib/protobuf/compiler/visitors.rb', line 36

def attach_proto
  @attach_proto
end

#contextObject

Returns the value of attribute context.



36
37
38
# File 'lib/protobuf/compiler/visitors.rb', line 36

def context
  @context
end

#indentObject

Returns the value of attribute indent.



36
37
38
# File 'lib/protobuf/compiler/visitors.rb', line 36

def indent
  @indent
end

#packageObject

Returns the value of attribute package.



36
37
38
# File 'lib/protobuf/compiler/visitors.rb', line 36

def package
  @package
end

#proto_fileObject

Returns the value of attribute proto_file.



36
37
38
# File 'lib/protobuf/compiler/visitors.rb', line 36

def proto_file
  @proto_file
end

Instance Method Details

#attach_proto?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/protobuf/compiler/visitors.rb', line 46

def attach_proto?
  @attach_proto
end

#close_rubyObject



72
73
74
75
76
77
# File 'lib/protobuf/compiler/visitors.rb', line 72

def close_ruby
  while 0 < indent
    decrement
    write('end')
  end
end

#commented_proto_contentsObject



50
51
52
53
54
55
56
57
58
# File 'lib/protobuf/compiler/visitors.rb', line 50

def commented_proto_contents
  if proto_file
    proto_filepath = if File.exist?(proto_file)
                     then proto_file
                     else "#{@proto_dir}/#{proto_file}"
                     end
    File.read(proto_filepath).gsub(/^/, '# ')
  end
end

#create_files(filename, out_dir, file_create) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/protobuf/compiler/visitors.rb', line 112

def create_files(filename, out_dir, file_create)
  $: << File.expand_path(out_dir)
  Class.new.class_eval(to_s) # check the message
  $:.delete File.expand_path(out_dir)
rescue LoadError
  puts "Error creating file #{filename}"
  puts $!.message
  exit 1
else 
  
  file = File.basename(filename)
  message_module = Util.module_to_path(package.map{|p| p.to_s.capitalize}.join('::'))
  filename = "#{out_dir}/#{message_module}/#{file}"
  
  if file_create
    log_writing(filename)
    FileUtils.mkpath(File.dirname(filename))
    File.open(filename, 'w') {|file| file.write(to_s) }
  else
    to_s
  end
end

#decrementObject



68
69
70
# File 'lib/protobuf/compiler/visitors.rb', line 68

def decrement
  @indent -= 1
end

#in_context(klass, &block) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/protobuf/compiler/visitors.rb', line 87

def in_context(klass, &block)
  increment
  context.push klass
  block.call
  context.pop
  decrement
end

#incrementObject



64
65
66
# File 'lib/protobuf/compiler/visitors.rb', line 64

def increment
  @indent += 1
end

#required_message_from_proto(proto_file) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/protobuf/compiler/visitors.rb', line 100

def required_message_from_proto(proto_file)
  rb_path = [
    proto_file.sub(/\.proto\z/, '.pb.rb')
  ].join('/').gsub(/\/{2,}/, '/')
  
  unless File.exist?(rb_path)
    Compiler.compile(proto_file, @proto_dir, @out_dir)
  end
  
  rb_path.sub(/\.rb$/, '')
end

#rubyObject



79
80
81
# File 'lib/protobuf/compiler/visitors.rb', line 79

def ruby
  @ruby ||= []
end

#to_sObject



83
84
85
# File 'lib/protobuf/compiler/visitors.rb', line 83

def to_s
  @ruby.join("\n")
end

#visit(node) ⇒ Object



95
96
97
98
# File 'lib/protobuf/compiler/visitors.rb', line 95

def visit(node)
  node.accept_message_visitor(self)
  self
end

#write(str) ⇒ Object



60
61
62
# File 'lib/protobuf/compiler/visitors.rb', line 60

def write(str)
  ruby << "#{'  ' * @indent}#{str}"
end