Class: Peto::Master

Inherits:
Object
  • Object
show all
Defined in:
lib/peto/master.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contractObject (readonly)

Returns the value of attribute contract.



18
19
20
# File 'lib/peto/master.rb', line 18

def contract
  @contract
end

Instance Method Details

#class_filename(name, language) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/peto/master.rb', line 26

def class_filename(name, language)
  case language
  when "rb"
    "#{name}.#{language}"
  when "as"
    "peto/#{name.capitalize}.#{language}"
  else
    raise "invalid language #{language.inspect}"
  end
end

#generate(language, output_dir = nil) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/peto/master.rb', line 37

def generate(language, output_dir=nil)
  raise "language is nil" if language.nil?
  parse(language).each do |name, content|
    filepath = File.join(output_dir||File::dirname(@filename), language, class_filename(name, language))
    write(filepath, content)
  end
end

#load(filename) ⇒ Object



14
15
16
17
# File 'lib/peto/master.rb', line 14

def load(filename)
  @filename = filename
  @contract = YAML.load(IO.read(Pathname(filename)))
end

#parse(language) ⇒ Object



20
21
22
23
24
# File 'lib/peto/master.rb', line 20

def parse(language)
  (@contract["types"]||{}).inject({}) {|result, type|
    result.merge!(Generator.new(@contract, language).generate_class(TEMPLATE_DIR + "/#{language}_classes.erb", type))
  }.merge!(Generator.new(@contract, language).generate_procedure(TEMPLATE_DIR + "/#{language}_procedures.erb"))
end

#write(filepath, content) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/peto/master.rb', line 45

def write(filepath, content)
  print "  "
  if File.exist?(filepath)
    if File.read(filepath) == content
      print "identical".blue.bold
    else
      print "   update".white.bold
    end
  else
    FileUtils.mkdir_p(File.dirname(filepath))
    print "   create".green.bold
  end
  print " "

  open(filepath, "w") do |file|
    file.write(content)
  end

  puts filepath
end