Sha256: 420daadea05cc150d5b1edfa4909f8fa7e352fbd87cb0ab60e2a9e38c33651d9

Contents?: true

Size: 1.22 KB

Versions: 15

Compression:

Stored size: 1.22 KB

Contents

#!/usr/bin/env ruby

require 'addressbook.pb'

def prompt_for_address(person)
  print 'Enter person ID number: '
  person.id = STDIN.gets.strip.to_i
  print 'Enter name: '
  person.name = STDIN.gets.strip
  print 'Enter email address (blank for none): '
  email = STDIN.gets.strip
  person.email = email unless email.empty?

  loop do
    print 'Enter a phone number (or leave blank to finish): '
    break if (number = STDIN.gets.strip).empty?

    person.phone << Tutorial::Person::PhoneNumber.new
    person.phone.last.number = number

    print 'Is this a mobile, home, or work phone? '
    person.phone.last.type = 
      case type = STDIN.gets.strip
      when 'mobile'
        Tutorial::Person::PhoneType::MOBILE
      when 'home'
        Tutorial::Person::PhoneType::HOME
      when 'work'
        Tutorial::Person::PhoneType::WORK
      else
        puts 'Unknown phone type; leaving as default value.'
        nil
      end
  end
end

unless ARGV.size == 1
  puts "Usage: #{$0} ADDRESS_BOOK_FILE" 
  exit
end

address_book = Tutorial::AddressBook.new
address_book.parse_from_file ARGV[0] if File.exist? ARGV[0]
address_book.person << Tutorial::Person.new
prompt_for_address address_book.person.last
address_book.serialize_to_file ARGV[0]

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
macks-ruby_protobuf-0.3.2.1 examples/writing_a_message.rb
macks-ruby_protobuf-0.3.2.2 examples/writing_a_message.rb
macks-ruby_protobuf-0.3.2.3 examples/writing_a_message.rb
macks-ruby_protobuf-0.3.3 examples/writing_a_message.rb
protobuf-1.0.0 examples/writing_a_message.rb
ruby_protobuf-0.4.11 examples/writing_a_message.rb
ruby_protobuf-0.4.10 examples/writing_a_message.rb
ruby_protobuf-0.4.9 examples/writing_a_message.rb
ruby_protobuf-0.4.8 examples/writing_a_message.rb
ruby_protobuf-0.4.7 examples/writing_a_message.rb
ruby_protobuf-0.4.6 examples/writing_a_message.rb
ruby_protobuf-0.4.5 examples/writing_a_message.rb
ruby_protobuf-0.4.4 examples/writing_a_message.rb
ruby_protobuf-0.4.1 examples/writing_a_message.rb
ruby_protobuf-0.3.3 examples/writing_a_message.rb