Class: Peto::Generator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contract, language) ⇒ Generator

Returns a new instance of Generator.



57
58
59
60
# File 'lib/peto/generator.rb', line 57

def initialize(contract, language)
  @contract = contract
  @language = language
end

Instance Attribute Details

#contractObject (readonly)

Returns the value of attribute contract.



61
62
63
# File 'lib/peto/generator.rb', line 61

def contract
  @contract
end

Instance Method Details

#arg(name, type, options = {}) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/peto/generator.rb', line 85

def arg(name, type, options={})
  array_type = options.delete(:array_type)
  {
    :name => name,
    :type => type.to_class_type(@language),
    :array_type => array_type.nil? ? nil : array_type.to_class_type(@language),
  }
end

#args(string_args) ⇒ Object



78
79
80
81
82
83
# File 'lib/peto/generator.rb', line 78

def args(string_args)
  string_args.map do |str|
    splitted = str.split(":")
    arg(splitted.first, splitted.second, :array_type => splitted.third)
  end
end

#class_nameObject



74
75
76
# File 'lib/peto/generator.rb', line 74

def class_name
  @contract["name"].to_class_type(@language)
end

#each_proceduresObject



100
101
102
103
104
105
106
107
108
# File 'lib/peto/generator.rb', line 100

def each_procedures
  (@contract["procedures"]||[]).each do |name, procedure|
    yield name.to_method_name, args(procedure["args"])
    yield "#{name} response".to_method_name, args(procedure["returns"])
    (procedure["errors"]||[]).each do |error|
      yield "#{name} error #{error}".to_method_name, [arg("message", "string")]
    end
  end
end

#each_typesObject



94
95
96
97
98
# File 'lib/peto/generator.rb', line 94

def each_types
  @contract["types"].each do |name, args|
    yield name.to_class_type(@language), args(args)
  end
end

#generate_class(template_filename, type) ⇒ Object



68
69
70
71
72
# File 'lib/peto/generator.rb', line 68

def generate_class(template_filename, type)
  erb = ERB.new(IO.read(template_filename), nil, "-")
  @target = {:name => type.first, :args => args(type.second)}
  { "#{@target[:name]}" => erb.result(binding) }
end

#generate_procedure(template_filename) ⇒ Object



63
64
65
66
# File 'lib/peto/generator.rb', line 63

def generate_procedure(template_filename)
  erb = ERB.new(IO.read(template_filename), nil, "-")
  { "#{@contract["name"]}" => erb.result(binding) }
end