peto 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,8 +14,8 @@ Contract file is simple YAML format.
14
14
  errors: [human not found, invalid id]
15
15
 
16
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
17
+ * human.rb(or human.as) : human's structure class
18
+ * foo.rb(or foo.as) : procedure class. this has find_human, find_human_response, find_human_error_human_not_found, find_human_error_invalid_id
19
19
 
20
20
  Use Foo class methods to create hash and send it as JSON string for RPC.
21
21
 
@@ -41,8 +41,7 @@ contract file (foo.yml):
41
41
  args: [user:user]
42
42
 
43
43
  invoke command:
44
- % mkdir generated
45
- % peto foo.yml -o generated/
44
+ % peto foo.yml rb -o generated/
46
45
 
47
46
  use foo.rb:
48
47
  $: << "generated/"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.6
1
+ 0.2.7
@@ -8,7 +8,7 @@ class String
8
8
  def to_method_name
9
9
  underscore.split(" ").join("_")
10
10
  end
11
- def to_class_type
11
+ def to_rb_class_type
12
12
  case self
13
13
  when "integer"
14
14
  "Fixnum"
@@ -16,6 +16,25 @@ class String
16
16
  classify
17
17
  end
18
18
  end
19
+ def to_as_class_type
20
+ case self
21
+ when "integer"
22
+ "int"
23
+ when "Number"
24
+ "int"
25
+ when "Boolean"
26
+ "Boolean"
27
+ when "string"
28
+ "String"
29
+ when "array"
30
+ "Array"
31
+ else
32
+ self.capitalize
33
+ end
34
+ end
35
+ def to_class_type(language)
36
+ send(:"to_#{language}_class_type")
37
+ end
19
38
  end
20
39
 
21
40
  def rb_primitive_types
@@ -24,11 +43,20 @@ end
24
43
  def as_primitive_types
25
44
  ["int", "String", "Number", "uint", "Boolean"]
26
45
  end
46
+ def as_default_value(type)
47
+ case type
48
+ when "int"
49
+ "0"
50
+ else
51
+ "null"
52
+ end
53
+ end
27
54
 
28
55
  module Peto
29
56
  class Generator
30
- def initialize(contract)
57
+ def initialize(contract, language)
31
58
  @contract = contract
59
+ @language = language
32
60
  end
33
61
  attr_reader :contract
34
62
 
@@ -44,7 +72,7 @@ module Peto
44
72
  end
45
73
 
46
74
  def class_name
47
- @contract["name"].to_class_type
75
+ @contract["name"].to_class_type(@language)
48
76
  end
49
77
 
50
78
  def args(string_args)
@@ -58,14 +86,14 @@ module Peto
58
86
  array_type = options.delete(:array_type)
59
87
  {
60
88
  :name => name,
61
- :type => type.to_class_type,
62
- :array_type => array_type.nil? ? nil : array_type.to_class_type,
89
+ :type => type.to_class_type(@language),
90
+ :array_type => array_type.nil? ? nil : array_type.to_class_type(@language),
63
91
  }
64
92
  end
65
93
 
66
94
  def each_types
67
95
  @contract["types"].each do |name, args|
68
- yield name.to_class_type, args(args)
96
+ yield name.to_class_type(@language), args(args)
69
97
  end
70
98
  end
71
99
 
@@ -19,14 +19,25 @@ module Peto
19
19
 
20
20
  def parse(language)
21
21
  (@contract["types"]||{}).inject({}) {|result, type|
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"))
22
+ result.merge!(Generator.new(@contract, language).generate_class(TEMPLATE_DIR + "/#{language}_classes.erb", type))
23
+ }.merge!(Generator.new(@contract, language).generate_procedure(TEMPLATE_DIR + "/#{language}_procedures.erb"))
24
+ end
25
+
26
+ def class_filename(name, language)
27
+ case language
28
+ when "rb"
29
+ "#{name}.#{language}"
30
+ when "as"
31
+ "peto/#{name.capitalize}.#{language}"
32
+ else
33
+ raise "invalid language #{language.inspect}"
34
+ end
24
35
  end
25
36
 
26
37
  def generate(language, output_dir=nil)
27
38
  raise "language is nil" if language.nil?
28
39
  parse(language).each do |name, content|
29
- filepath = File.join(output_dir||File::dirname(@filename), language, "#{name}.#{language}")
40
+ filepath = File.join(output_dir||File::dirname(@filename), language, class_filename(name, language))
30
41
  write(filepath, content)
31
42
  end
32
43
  end
@@ -1,16 +1,16 @@
1
1
  <%- name = @target[:name].camelize -%>
2
2
  <%- args = @target[:args] -%>
3
- import peto.*;
4
-
5
3
  package peto
6
4
  {
7
- class <%= name %>
5
+ import peto.*;
6
+
7
+ public class <%= name %>
8
8
  {
9
- public static function create(hash_args):<%=name%>
9
+ public static function create(hash_args:Object):<%=name%>
10
10
  {
11
11
  var instance:<%= name %> = new <%= name %>;
12
12
  <%- args.each do |arg| -%>
13
- <%- if rb_primitive_types.include?(arg[:type].constantize) -%>
13
+ <%- if as_primitive_types.include?(arg[:type]) -%>
14
14
  instance.<%=arg[:name]%> = hash_args.<%= arg[:name]%>;
15
15
  <%- elsif arg[:array_type] -%>
16
16
  for each ( var arg:Object in hash_args.<%= arg[:name]%>)
@@ -22,7 +22,7 @@ package peto
22
22
  return instance;
23
23
  }
24
24
 
25
- public function <%=name%>(<%= args.map{|arg| "#{arg[:name]}:#{arg[:type]}"}.join(",") %>)
25
+ public function <%=name%>(<%= args.map{|arg| "#{arg[:name]}:#{arg[:type]}=#{as_default_value(arg[:type])}"}.join(",") %>)
26
26
  {
27
27
  <%- args.each do |arg| -%>
28
28
  this.<%= arg[:name] %> = <%= arg[:name] %>;
@@ -30,14 +30,14 @@ package peto
30
30
  }
31
31
 
32
32
  <%- args.each do |arg| -%>
33
- private var <%= arg[:name] %>:<%= arg[:type] %>;
33
+ private var m_<%= arg[:name] %>:<%= arg[:type] %>;
34
34
  public function get <%= arg[:name] %>():<%= arg[:type] %>
35
35
  {
36
- return <%= arg[:name] %>;
36
+ return m_<%= arg[:name] %>;
37
37
  }
38
- public function set <%= arg[:name] %>(value:<%= arg[:type] %>)
38
+ public function set <%= arg[:name] %>(value:<%= arg[:type] %>):void
39
39
  {
40
- <%= arg[:name] %> = value;
40
+ m_<%= arg[:name] %> = value;
41
41
  }
42
42
  <%- end -%>
43
43
 
@@ -45,13 +45,13 @@ package peto
45
45
  {
46
46
  var hash:Object = new Object;
47
47
  <%- args.each do |arg| -%>
48
- <%- if rb_primitive_types.include?(arg[:type].constantize) -%>
49
- hash_args["<%= arg[:name]%>"] = <%=arg[:name]%>;
48
+ <%- if as_primitive_types.include?(arg[:type]) -%>
49
+ hash["<%= arg[:name]%>"] = <%=arg[:name]%>;
50
50
  <%- elsif arg[:array_type] -%>
51
51
  for each ( var arg:<%= arg[:array_type]%> in <%= arg[:name]%>)
52
- hash_args["<%= arg[:name]%>"] = <%=arg[:name]%>.toHash():
52
+ hash["<%= arg[:name]%>"] = <%=arg[:name]%>.toHash();
53
53
  <%- else -%>
54
- hash_args["<%= arg[:name]%>"] = <%=arg[:name]%>.toHash():
54
+ hash["<%= arg[:name]%>"] = <%=arg[:name]%>.toHash();
55
55
  <%- end -%>
56
56
  <%- end -%>
57
57
  return hash;
@@ -6,12 +6,11 @@
6
6
  args.map{|arg| "#{arg[:name]}:#{arg[:type]}"}.join(",")
7
7
  end
8
8
  -%>
9
-
10
- import peto.*;
11
-
12
9
  package peto
13
10
  {
14
- class <%= class_name %>
11
+ import peto.*;
12
+
13
+ public class <%= class_name %>
15
14
  {
16
15
  <%- each_procedures do |name, args| -%>
17
16
  // <%=name%> methods
@@ -21,9 +20,11 @@ package peto
21
20
  return {
22
21
  procedure : "<%=name%>",
23
22
  args : {
24
- <%- args.each do |arg| -%>
25
- <%=arg[:name]%> : hashize(<%=arg[:name]%>),
26
- <%- end -%>
23
+ <%= args.map {|arg|
24
+ as_primitive_types.include?(arg[:type]) ?
25
+ "#{arg[:name]} : #{arg[:name]}" :
26
+ "#{arg[:name]} : #{arg[:name]}.toHash()"
27
+ }.join(",")%>
27
28
  }
28
29
  }
29
30
  }
@@ -32,24 +33,15 @@ package peto
32
33
  {
33
34
  var args:Array = new Array();
34
35
  <%- args.each do |arg| -%>
36
+ <%- if as_primitive_types.include?(arg[:type])-%>
37
+ args.push(hash_args.<%=arg[:name]%>);
38
+ <%- else -%>
35
39
  args.push(<%=arg[:type]%>.create(hash_args.<%=arg[:name]%>));
36
40
  <%- end -%>
41
+ <%- end -%>
37
42
  return args;
38
43
  }
39
44
 
40
45
  <%- 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
46
  }
55
47
  }
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 6
9
- version: 0.2.6
8
+ - 7
9
+ version: 0.2.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - Toshiyuki Hirooka
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-03 00:00:00 +09:00
17
+ date: 2010-11-04 00:00:00 +09:00
18
18
  default_executable: peto
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -111,14 +111,9 @@ files:
111
111
  - examples/rails_app/test/unit/helpers/foo_helper_test.rb
112
112
  - examples/rails_app/test/test_helper.rb
113
113
  - examples/rails_app/test/functional/foo_controller_test.rb
114
- - examples/rails_app/contracts/generated/animal.rb
115
- - examples/rails_app/contracts/generated/aho.rb
116
- - examples/rails_app/contracts/generated/user.rb
117
114
  - examples/rails_app/contracts/generated/rb/animal.rb
118
115
  - examples/rails_app/contracts/generated/rb/user.rb
119
116
  - examples/rails_app/contracts/generated/rb/foo.rb
120
- - examples/rails_app/contracts/generated/foo.rb
121
- - examples/rails_app/contracts/generated/iasdaho.rb
122
117
  has_rdoc: true
123
118
  homepage: http://github.com/tosik/peto
124
119
  licenses: []
@@ -176,11 +171,6 @@ test_files:
176
171
  - examples/rails_app/test/unit/helpers/foo_helper_test.rb
177
172
  - examples/rails_app/test/test_helper.rb
178
173
  - examples/rails_app/test/functional/foo_controller_test.rb
179
- - examples/rails_app/contracts/generated/animal.rb
180
- - examples/rails_app/contracts/generated/aho.rb
181
- - examples/rails_app/contracts/generated/user.rb
182
174
  - examples/rails_app/contracts/generated/rb/animal.rb
183
175
  - examples/rails_app/contracts/generated/rb/user.rb
184
176
  - examples/rails_app/contracts/generated/rb/foo.rb
185
- - examples/rails_app/contracts/generated/foo.rb
186
- - examples/rails_app/contracts/generated/iasdaho.rb
@@ -1,27 +0,0 @@
1
- require "peto/mixin/peto_class"
2
-
3
- module Peto
4
- class Aho
5
- include PetoClass
6
- def initialize(args={})
7
- @name = nil
8
-
9
- set_by_hash(args)
10
- raise_errors unless valid?
11
- end
12
-
13
- attr_reader :name
14
-
15
- def members
16
- [:name]
17
- end
18
-
19
- def types
20
- {:name => String}
21
- end
22
-
23
- def arrays
24
- {}
25
- end
26
- end
27
- end
@@ -1,39 +0,0 @@
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
@@ -1,93 +0,0 @@
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 atomic_type?(var.class)
86
- var.to_hash
87
- end
88
-
89
- def self.atomic_type?(type)
90
- [Fixnum,String].include?(type)
91
- end
92
- end
93
- end
@@ -1,27 +0,0 @@
1
- require "peto/mixin/peto_class"
2
-
3
- module Peto
4
- class Iasdaho
5
- include PetoClass
6
- def initialize(args={})
7
- @name = nil
8
-
9
- set_by_hash(args)
10
- raise_errors unless valid?
11
- end
12
-
13
- attr_reader :name
14
-
15
- def members
16
- [:name]
17
- end
18
-
19
- def types
20
- {:name => String}
21
- end
22
-
23
- def arrays
24
- {}
25
- end
26
- end
27
- end
@@ -1,53 +0,0 @@
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