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.



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

def contract
  @contract
end

Instance Method Details

#generate(output_dir = nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/peto/master.rb', line 25

def generate(output_dir=nil)
  parse.each do |name, content|
    filepath = File.join(output_dir||File::dirname(@filename), "#{name}.rb")
    write(filepath, content)
  end
end

#load(filename) ⇒ Object



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

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

#parseObject



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

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

#write(filepath, content) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/peto/master.rb', line 32

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
    print "create   ".green.bold
  end
  print " "

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

  puts filepath
end