Class: Peto::Generator
- Inherits:
-
Object
- Object
- Peto::Generator
- Defined in:
- lib/peto/generator.rb
Instance Attribute Summary collapse
-
#contract ⇒ Object
readonly
Returns the value of attribute contract.
Instance Method Summary collapse
- #arg(name, type, options = {}) ⇒ Object
- #args(string_args) ⇒ Object
- #class_name ⇒ Object
- #each_procedures ⇒ Object
- #each_types ⇒ Object
- #generate_class(template_filename, type) ⇒ Object
- #generate_procedure(template_filename) ⇒ Object
-
#initialize(contract) ⇒ Generator
constructor
A new instance of Generator.
Constructor Details
#initialize(contract) ⇒ Generator
Returns a new instance of Generator.
23 24 25 |
# File 'lib/peto/generator.rb', line 23 def initialize(contract) @contract = contract end |
Instance Attribute Details
#contract ⇒ Object (readonly)
Returns the value of attribute contract.
26 27 28 |
# File 'lib/peto/generator.rb', line 26 def contract @contract end |
Instance Method Details
#arg(name, type, options = {}) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/peto/generator.rb', line 50 def arg(name, type, ={}) array_type = .delete(:array_type) { :name => name, :type => type.to_class_type, :array_type => array_type.nil? ? nil : array_type.to_class_type, } end |
#args(string_args) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/peto/generator.rb', line 43 def args(string_args) string_args.map do |str| splitted = str.split(":") arg(splitted.first, splitted.second, :array_type => splitted.third) end end |
#class_name ⇒ Object
39 40 41 |
# File 'lib/peto/generator.rb', line 39 def class_name @contract["name"].to_class_type end |
#each_procedures ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/peto/generator.rb', line 65 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_types ⇒ Object
59 60 61 62 63 |
# File 'lib/peto/generator.rb', line 59 def each_types @contract["types"].each do |name, args| yield name.to_class_type, args(args) end end |
#generate_class(template_filename, type) ⇒ Object
33 34 35 36 37 |
# File 'lib/peto/generator.rb', line 33 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
28 29 30 31 |
# File 'lib/peto/generator.rb', line 28 def generate_procedure(template_filename) erb = ERB.new(IO.read(template_filename), nil, "-") { "#{@contract["name"]}" => erb.result(binding) } end |