peto 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,5 +1,29 @@
1
1
  = peto
2
2
 
3
+ peto is a RPC code generator.
4
+ Defining types and procedures on RPC from contract files.
5
+ Contract file is simple YAML format.
6
+
7
+ name: foo
8
+ types:
9
+ human: [name:string, age:integer]
10
+ procedures:
11
+ find_human:
12
+ args: [id:integer]
13
+ returns: [found:human]
14
+ errors: [human not found, invalid id]
15
+
16
+ peto generates some codes.
17
+ * human.rb : human's structure class
18
+ * foo.rb : procedure class. this has find_human, find_human_response, find_human_error_human_not_found, find_human_error_invalid_id
19
+
20
+ Use Foo class methods to create hash and send it as JSON string for RPC.
21
+
22
+
23
+ peto has a Rails helper module.
24
+ Look examples/rails_app and use it, if you want to create Rails RPC server.
25
+
26
+
3
27
  == Usage
4
28
 
5
29
  installing:
@@ -31,13 +55,7 @@ use foo.rb:
31
55
  user = Peto::User.new(:name => "alice", :age => 23, :animals=>[cat, dog])
32
56
 
33
57
  # generating procedure hash
34
- p Peto::Foo.set_user(user) # => { :procedure=>"set_user",
35
- :args=>{ :user=>{ :name=>"alice",
36
- :age=>23,
37
- :animals=>[
38
- Peto::Animal({:name=>"cat"}),
39
- Peto::Animal({:name=>"dog"})
40
- ]}}}
58
+ Peto::Foo.set_user(user)
41
59
 
42
60
  == in Rails
43
61
 
@@ -96,7 +114,7 @@ run server
96
114
 
97
115
  % rails server
98
116
 
99
- post next json to http://server/foo/
117
+ post next JSON to http://server/foo/
100
118
  {
101
119
  "procedure" : "set_user",
102
120
  "args" : {
@@ -106,6 +124,9 @@ post next json to http://server/foo/
106
124
  }
107
125
  }
108
126
 
127
+ this JSON string is created by
128
+ ActiveSupport::JSON.encode(Peto::Foo.set_user(user))
129
+
109
130
 
110
131
  == Note on Patches/Pull Requests
111
132
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.2.6
data/bin/peto CHANGED
@@ -12,10 +12,11 @@ OptionParser.new do |opt|
12
12
  opt.on("-o output directory") {|value| options[:output] = value }
13
13
  opt.parse!(ARGV)
14
14
  end
15
- input = ARGV.pop
15
+ input = ARGV.shift
16
+ language = ARGV.shift
16
17
 
17
18
  if input.nil?
18
- puts "usage:\n % peto contract.yml"
19
+ puts "usage:\n % peto contract.yml rb"
19
20
  exit
20
21
  end
21
22
 
@@ -23,5 +24,5 @@ options[:output] = File.dirname(input) if options[:output].nil?
23
24
 
24
25
  peto = Peto::Master.new
25
26
  peto.load(input)
26
- peto.generate(options[:output])
27
+ peto.generate(language, options[:output])
27
28
 
@@ -17,7 +17,6 @@ module RailsApp
17
17
 
18
18
  # generated files by peto
19
19
  config.autoload_paths += %W(#{config.root}/contracts/generated)
20
- config.autoload_paths += %W(#{config.root}/../../lib)
21
20
 
22
21
  # Only load the plugins named here, in the order given (default is alphabetical).
23
22
  # :all can be used as a placeholder for all plugins not explicitly named.
@@ -3,7 +3,3 @@ require File.expand_path('../application', __FILE__)
3
3
 
4
4
  # Initialize the rails application
5
5
  RailsApp::Application.initialize!
6
-
7
- #ActionController::Base.param_parsers[Mime::Type::lookup("application/json")] = Proc.new {
8
- # ActiveSupport::JSON.decode(data)
9
- #}
@@ -0,0 +1,39 @@
1
+ require "peto/mixin/peto_class"
2
+
3
+ module Peto
4
+ class Animal
5
+ include PetoClass
6
+
7
+ def self.create(hash_args)
8
+ instance = new
9
+ instance.name = hash_args["name"]
10
+ return instance
11
+ end
12
+
13
+ def initialize(args={})
14
+ @name = nil
15
+
16
+ set_args(args)
17
+ raise_errors unless valid?
18
+ end
19
+
20
+ attr_reader :name
21
+
22
+ def name=(value)
23
+ @name = value
24
+ raise_errors unless valid?
25
+ end
26
+
27
+ def members
28
+ [:name]
29
+ end
30
+
31
+ def types
32
+ {:name => String}
33
+ end
34
+
35
+ def arrays
36
+ {}
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,93 @@
1
+ require "peto/mixin/peto_errorable"
2
+
3
+
4
+ module Peto
5
+ class Foo
6
+ extend PetoErrorable
7
+
8
+ # set_user methods
9
+
10
+ def self.set_user(user)
11
+ set_user_valid?(user)
12
+ return {
13
+ :procedure => "set_user",
14
+ :args => {
15
+ :user => hashize(user),
16
+ }
17
+ }
18
+ end
19
+
20
+ def self.set_user_valid?(user)
21
+ invalid_type("user", User, user) unless user.class == User
22
+ raise_errors unless errors.empty?
23
+ end
24
+
25
+ def self.set_user_hash_to_args(hash_args)
26
+ args = []
27
+ args.push(User.create(hash_args["user"]))
28
+ return args
29
+ end
30
+
31
+
32
+ # set_user_response methods
33
+
34
+ def self.set_user_response(a,b)
35
+ set_user_response_valid?(a,b)
36
+ return {
37
+ :procedure => "set_user_response",
38
+ :args => {
39
+ :a => hashize(a),
40
+ :b => hashize(b),
41
+ }
42
+ }
43
+ end
44
+
45
+ def self.set_user_response_valid?(a,b)
46
+ invalid_type("a", Fixnum, a) unless a.class == Fixnum
47
+ invalid_type("b", String, b) unless b.class == String
48
+ raise_errors unless errors.empty?
49
+ end
50
+
51
+ def self.set_user_response_hash_to_args(hash_args)
52
+ args = []
53
+ args.push(Fixnum.create(hash_args["a"]))
54
+ args.push(String.create(hash_args["b"]))
55
+ return args
56
+ end
57
+
58
+
59
+ # set_user_error_invalid_user methods
60
+
61
+ def self.set_user_error_invalid_user(message)
62
+ set_user_error_invalid_user_valid?(message)
63
+ return {
64
+ :procedure => "set_user_error_invalid_user",
65
+ :args => {
66
+ :message => hashize(message),
67
+ }
68
+ }
69
+ end
70
+
71
+ def self.set_user_error_invalid_user_valid?(message)
72
+ invalid_type("message", String, message) unless message.class == String
73
+ raise_errors unless errors.empty?
74
+ end
75
+
76
+ def self.set_user_error_invalid_user_hash_to_args(hash_args)
77
+ args = []
78
+ args.push(String.create(hash_args["message"]))
79
+ return args
80
+ end
81
+
82
+
83
+
84
+ def self.hashize(var)
85
+ return var if primitive_type?(var.class)
86
+ var.to_hash
87
+ end
88
+
89
+ def self.primitive_type?(type)
90
+ [Fixnum,String].include?(type)
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,53 @@
1
+ require "peto/mixin/peto_class"
2
+
3
+ module Peto
4
+ class User
5
+ include PetoClass
6
+
7
+ def self.create(hash_args)
8
+ instance = new
9
+ instance.name = hash_args["name"]
10
+ instance.age = hash_args["age"]
11
+ instance.animals = hash_args["animals"].map {|arg| Animal.create(arg) }
12
+ return instance
13
+ end
14
+
15
+ def initialize(args={})
16
+ @name = nil
17
+ @age = nil
18
+ @animals = [] # for Animal
19
+
20
+ set_args(args)
21
+ raise_errors unless valid?
22
+ end
23
+
24
+ attr_reader :name
25
+ attr_reader :age
26
+ attr_reader :animals
27
+
28
+ def name=(value)
29
+ @name = value
30
+ raise_errors unless valid?
31
+ end
32
+ def age=(value)
33
+ @age = value
34
+ raise_errors unless valid?
35
+ end
36
+ def animals=(value)
37
+ @animals = value
38
+ raise_errors unless valid?
39
+ end
40
+
41
+ def members
42
+ [:name,:age,:animals]
43
+ end
44
+
45
+ def types
46
+ {:name => String,:age => Fixnum,:animals => Array}
47
+ end
48
+
49
+ def arrays
50
+ {:animals => Animal}
51
+ end
52
+ end
53
+ end
@@ -18,9 +18,12 @@ class String
18
18
  end
19
19
  end
20
20
 
21
- def atomic_types
21
+ def rb_primitive_types
22
22
  [Fixnum, String]
23
23
  end
24
+ def as_primitive_types
25
+ ["int", "String", "Number", "uint", "Boolean"]
26
+ end
24
27
 
25
28
  module Peto
26
29
  class Generator
data/lib/peto/master.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  require "pathname"
2
2
  require "yaml"
3
- require "peto/generator"
3
+ require "fileutils"
4
4
  require "term/ansicolor"
5
+ require "peto/generator"
5
6
 
6
7
  class String
7
8
  include Term::ANSIColor
@@ -16,15 +17,16 @@ module Peto
16
17
  end
17
18
  attr_reader :contract
18
19
 
19
- def parse
20
+ def parse(language)
20
21
  (@contract["types"]||{}).inject({}) {|result, type|
21
- result.merge!(Generator.new(@contract).generate_class(TEMPLATE_DIR + "/rb_classes.erb", type))
22
- }.merge!(Generator.new(@contract).generate_procedure(TEMPLATE_DIR + "/rb_procedures.erb"))
22
+ result.merge!(Generator.new(@contract).generate_class(TEMPLATE_DIR + "/#{language}_classes.erb", type))
23
+ }.merge!(Generator.new(@contract).generate_procedure(TEMPLATE_DIR + "/#{language}_procedures.erb"))
23
24
  end
24
25
 
25
- def generate(output_dir=nil)
26
- parse.each do |name, content|
27
- filepath = File.join(output_dir||File::dirname(@filename), "#{name}.rb")
26
+ def generate(language, output_dir=nil)
27
+ raise "language is nil" if language.nil?
28
+ parse(language).each do |name, content|
29
+ filepath = File.join(output_dir||File::dirname(@filename), language, "#{name}.#{language}")
28
30
  write(filepath, content)
29
31
  end
30
32
  end
@@ -38,6 +40,7 @@ module Peto
38
40
  print " update".white.bold
39
41
  end
40
42
  else
43
+ FileUtils.mkdir_p(File.dirname(filepath))
41
44
  print " create".green.bold
42
45
  end
43
46
  print " "
@@ -6,6 +6,8 @@ module Peto
6
6
  class RakeTask < ::Rake::TaskLib
7
7
  attr_accessor :contracts
8
8
  attr_accessor :output_dir
9
+ attr_accessor :languages
10
+
9
11
  attr_accessor :name
10
12
  attr_accessor :fail_on_error
11
13
  attr_accessor :failure_message
@@ -13,6 +15,8 @@ module Peto
13
15
  def initialize(*args)
14
16
  @contracts ||= []
15
17
  @output_dir ||= "./"
18
+ @languages ||= []
19
+
16
20
  @name ||= :peto
17
21
  @fail_on_error ||= true
18
22
 
@@ -26,15 +30,17 @@ module Peto
26
30
  puts "No contracts"
27
31
  else
28
32
  begin
29
- self.contracts = [contracts] if contracts.class == String
30
- contracts.each do |contract|
31
- peto = Peto::Master.new
32
- peto.load(contract)
33
- peto.generate(output_dir)
33
+ languages.each do |language|
34
+ self.contracts = [contracts] if contracts.class == String
35
+ contracts.each do |contract|
36
+ peto = Peto::Master.new
37
+ peto.load(contract)
38
+ peto.generate(language, output_dir)
39
+ end
34
40
  end
35
- rescue
36
- puts failure_message if failure_message
37
- raise "peto failed" if fail_on_error
41
+ #rescue
42
+ # puts failure_message if failure_message
43
+ # raise "peto failed" if fail_on_error
38
44
  end
39
45
  end
40
46
  end
@@ -0,0 +1,60 @@
1
+ <%- name = @target[:name].camelize -%>
2
+ <%- args = @target[:args] -%>
3
+ import peto.*;
4
+
5
+ package peto
6
+ {
7
+ class <%= name %>
8
+ {
9
+ public static function create(hash_args):<%=name%>
10
+ {
11
+ var instance:<%= name %> = new <%= name %>;
12
+ <%- args.each do |arg| -%>
13
+ <%- if rb_primitive_types.include?(arg[:type].constantize) -%>
14
+ instance.<%=arg[:name]%> = hash_args.<%= arg[:name]%>;
15
+ <%- elsif arg[:array_type] -%>
16
+ for each ( var arg:Object in hash_args.<%= arg[:name]%>)
17
+ instance.<%=arg[:name]%>.push(<%=arg[:array_type]%>.create(arg));
18
+ <%- else -%>
19
+ instance.<%=arg[:name]%> = <%=arg[:type]%>.create(hash_args.<%= arg[:name]%>);
20
+ <%- end -%>
21
+ <%- end -%>
22
+ return instance;
23
+ }
24
+
25
+ public function <%=name%>(<%= args.map{|arg| "#{arg[:name]}:#{arg[:type]}"}.join(",") %>)
26
+ {
27
+ <%- args.each do |arg| -%>
28
+ this.<%= arg[:name] %> = <%= arg[:name] %>;
29
+ <%- end -%>
30
+ }
31
+
32
+ <%- args.each do |arg| -%>
33
+ private var <%= arg[:name] %>:<%= arg[:type] %>;
34
+ public function get <%= arg[:name] %>():<%= arg[:type] %>
35
+ {
36
+ return <%= arg[:name] %>;
37
+ }
38
+ public function set <%= arg[:name] %>(value:<%= arg[:type] %>)
39
+ {
40
+ <%= arg[:name] %> = value;
41
+ }
42
+ <%- end -%>
43
+
44
+ public function toHash():Object
45
+ {
46
+ var hash:Object = new Object;
47
+ <%- args.each do |arg| -%>
48
+ <%- if rb_primitive_types.include?(arg[:type].constantize) -%>
49
+ hash_args["<%= arg[:name]%>"] = <%=arg[:name]%>;
50
+ <%- elsif arg[:array_type] -%>
51
+ for each ( var arg:<%= arg[:array_type]%> in <%= arg[:name]%>)
52
+ hash_args["<%= arg[:name]%>"] = <%=arg[:name]%>.toHash():
53
+ <%- else -%>
54
+ hash_args["<%= arg[:name]%>"] = <%=arg[:name]%>.toHash():
55
+ <%- end -%>
56
+ <%- end -%>
57
+ return hash;
58
+ }
59
+ }
60
+ }
@@ -0,0 +1,55 @@
1
+ <%-
2
+ def comma_args(args)
3
+ args.map{|arg| arg[:name]}.join(",")
4
+ end
5
+ def comma_args_with_type(args)
6
+ args.map{|arg| "#{arg[:name]}:#{arg[:type]}"}.join(",")
7
+ end
8
+ -%>
9
+
10
+ import peto.*;
11
+
12
+ package peto
13
+ {
14
+ class <%= class_name %>
15
+ {
16
+ <%- each_procedures do |name, args| -%>
17
+ // <%=name%> methods
18
+
19
+ public static function <%= name %>(<%= comma_args_with_type(args) %>):Object
20
+ {
21
+ return {
22
+ procedure : "<%=name%>",
23
+ args : {
24
+ <%- args.each do |arg| -%>
25
+ <%=arg[:name]%> : hashize(<%=arg[:name]%>),
26
+ <%- end -%>
27
+ }
28
+ }
29
+ }
30
+
31
+ public static function <%= name %>_hash_to_args(hash_args:Object):Array
32
+ {
33
+ var args:Array = new Array();
34
+ <%- args.each do |arg| -%>
35
+ args.push(<%=arg[:type]%>.create(hash_args.<%=arg[:name]%>));
36
+ <%- end -%>
37
+ return args;
38
+ }
39
+
40
+ <%- end -%>
41
+
42
+ public static function hashize(var:Object):Object
43
+ {
44
+ if ( isPrimitiveType(typeof(var)) )
45
+ return var;
46
+ else
47
+ return var.toHash();
48
+ }
49
+
50
+ public static function isPrimitiveType(type):Boolean
51
+ {
52
+ [<%=as_primitive_types.join(",")%>].indexOf?(type) != -1;
53
+ }
54
+ }
55
+ }
@@ -9,7 +9,7 @@ module Peto
9
9
  def self.create(hash_args)
10
10
  instance = new
11
11
  <%- args.each do |arg| -%>
12
- <%- if atomic_types.include?(arg[:type].constantize) -%>
12
+ <%- if rb_primitive_types.include?(arg[:type].constantize) -%>
13
13
  instance.<%=arg[:name]%> = hash_args["<%= arg[:name]%>"]
14
14
  <%- elsif arg[:array_type] -%>
15
15
  instance.<%=arg[:name]%> = hash_args["<%= arg[:name]%>"].map {|arg| <%=arg[:array_type]%>.create(arg) }
@@ -44,12 +44,12 @@ module Peto
44
44
  <%- end -%>
45
45
 
46
46
  def self.hashize(var)
47
- return var if atomic_type?(var.class)
47
+ return var if primitive_type?(var.class)
48
48
  var.to_hash
49
49
  end
50
50
 
51
- def self.atomic_type?(type)
52
- [<%=atomic_types.join(",")%>].include?(type)
51
+ def self.primitive_type?(type)
52
+ [<%=rb_primitive_types.join(",")%>].include?(type)
53
53
  end
54
54
  end
55
55
  end
data/test/test_peto.rb CHANGED
@@ -25,8 +25,8 @@ class TestPeto < Test::Unit::TestCase
25
25
  @peto.load("test/contracts/generating.yml")
26
26
  end
27
27
  should "returns string by loaded contract" do
28
- assert_equal Hash, @peto.parse.class
29
- @peto.parse.each do |name, content|
28
+ assert_equal Hash, @peto.parse(:rb).class
29
+ @peto.parse(:rb).each do |name, content|
30
30
  assert_equal String, name.class
31
31
  assert_equal String, content.class
32
32
  end
@@ -38,7 +38,7 @@ class TestPeto < Test::Unit::TestCase
38
38
  setup do
39
39
  @peto = Peto::Master.new
40
40
  @peto.load("test/contracts/generating.yml")
41
- @generated = @peto.parse
41
+ @generated = @peto.parse(:rb)
42
42
  end
43
43
  should "be readable as ruby" do
44
44
  @generated.each do |filepath, content|
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 5
9
- version: 0.2.5
8
+ - 6
9
+ version: 0.2.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Toshiyuki Hirooka
@@ -81,6 +81,8 @@ files:
81
81
  - lib/peto/rails/rails_controller_helper.rb
82
82
  - lib/peto/rails/rails_controller_test_helper.rb
83
83
  - lib/peto/rake_task.rb
84
+ - lib/templates/as_classes.erb
85
+ - lib/templates/as_procedures.erb
84
86
  - lib/templates/rb_classes.erb
85
87
  - lib/templates/rb_procedures.erb
86
88
  - test/contracts/generating.yml
@@ -112,6 +114,9 @@ files:
112
114
  - examples/rails_app/contracts/generated/animal.rb
113
115
  - examples/rails_app/contracts/generated/aho.rb
114
116
  - examples/rails_app/contracts/generated/user.rb
117
+ - examples/rails_app/contracts/generated/rb/animal.rb
118
+ - examples/rails_app/contracts/generated/rb/user.rb
119
+ - examples/rails_app/contracts/generated/rb/foo.rb
115
120
  - examples/rails_app/contracts/generated/foo.rb
116
121
  - examples/rails_app/contracts/generated/iasdaho.rb
117
122
  has_rdoc: true
@@ -174,5 +179,8 @@ test_files:
174
179
  - examples/rails_app/contracts/generated/animal.rb
175
180
  - examples/rails_app/contracts/generated/aho.rb
176
181
  - examples/rails_app/contracts/generated/user.rb
182
+ - examples/rails_app/contracts/generated/rb/animal.rb
183
+ - examples/rails_app/contracts/generated/rb/user.rb
184
+ - examples/rails_app/contracts/generated/rb/foo.rb
177
185
  - examples/rails_app/contracts/generated/foo.rb
178
186
  - examples/rails_app/contracts/generated/iasdaho.rb