peto 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/examples/rails_app/config/application.rb +1 -1
- data/examples/rails_app/config/environment.rb +4 -0
- data/examples/rails_app/contracts/generated/animal.rb +13 -1
- data/examples/rails_app/contracts/generated/foo.rb +35 -1
- data/examples/rails_app/contracts/generated/user.rb +23 -1
- data/lib/peto/generator.rb +4 -0
- data/lib/peto/mixin/peto_class.rb +2 -2
- data/lib/peto/rails/rails_controller_helper.rb +8 -3
- data/lib/templates/rb_classes.erb +23 -1
- data/lib/templates/rb_procedures.erb +26 -4
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.5
|
@@ -17,7 +17,7 @@ module RailsApp
|
|
17
17
|
|
18
18
|
# generated files by peto
|
19
19
|
config.autoload_paths += %W(#{config.root}/contracts/generated)
|
20
|
-
|
20
|
+
config.autoload_paths += %W(#{config.root}/../../lib)
|
21
21
|
|
22
22
|
# Only load the plugins named here, in the order given (default is alphabetical).
|
23
23
|
# :all can be used as a placeholder for all plugins not explicitly named.
|
@@ -3,15 +3,27 @@ require "peto/mixin/peto_class"
|
|
3
3
|
module Peto
|
4
4
|
class Animal
|
5
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
|
+
|
6
13
|
def initialize(args={})
|
7
14
|
@name = nil
|
8
15
|
|
9
|
-
|
16
|
+
set_args(args)
|
10
17
|
raise_errors unless valid?
|
11
18
|
end
|
12
19
|
|
13
20
|
attr_reader :name
|
14
21
|
|
22
|
+
def name=(value)
|
23
|
+
@name = value
|
24
|
+
raise_errors unless valid?
|
25
|
+
end
|
26
|
+
|
15
27
|
def members
|
16
28
|
[:name]
|
17
29
|
end
|
@@ -1,8 +1,12 @@
|
|
1
1
|
require "peto/mixin/peto_errorable"
|
2
2
|
|
3
|
+
|
3
4
|
module Peto
|
4
5
|
class Foo
|
5
6
|
extend PetoErrorable
|
7
|
+
|
8
|
+
# set_user methods
|
9
|
+
|
6
10
|
def self.set_user(user)
|
7
11
|
set_user_valid?(user)
|
8
12
|
return {
|
@@ -18,6 +22,15 @@ module Peto
|
|
18
22
|
raise_errors unless errors.empty?
|
19
23
|
end
|
20
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
|
+
|
21
34
|
def self.set_user_response(a,b)
|
22
35
|
set_user_response_valid?(a,b)
|
23
36
|
return {
|
@@ -35,6 +48,16 @@ module Peto
|
|
35
48
|
raise_errors unless errors.empty?
|
36
49
|
end
|
37
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
|
+
|
38
61
|
def self.set_user_error_invalid_user(message)
|
39
62
|
set_user_error_invalid_user_valid?(message)
|
40
63
|
return {
|
@@ -50,10 +73,21 @@ module Peto
|
|
50
73
|
raise_errors unless errors.empty?
|
51
74
|
end
|
52
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
|
+
|
53
83
|
|
54
84
|
def self.hashize(var)
|
55
|
-
return var if
|
85
|
+
return var if atomic_type?(var.class)
|
56
86
|
var.to_hash
|
57
87
|
end
|
88
|
+
|
89
|
+
def self.atomic_type?(type)
|
90
|
+
[Fixnum,String].include?(type)
|
91
|
+
end
|
58
92
|
end
|
59
93
|
end
|
@@ -3,12 +3,21 @@ require "peto/mixin/peto_class"
|
|
3
3
|
module Peto
|
4
4
|
class User
|
5
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
|
+
|
6
15
|
def initialize(args={})
|
7
16
|
@name = nil
|
8
17
|
@age = nil
|
9
18
|
@animals = [] # for Animal
|
10
19
|
|
11
|
-
|
20
|
+
set_args(args)
|
12
21
|
raise_errors unless valid?
|
13
22
|
end
|
14
23
|
|
@@ -16,6 +25,19 @@ module Peto
|
|
16
25
|
attr_reader :age
|
17
26
|
attr_reader :animals
|
18
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
|
+
|
19
41
|
def members
|
20
42
|
[:name,:age,:animals]
|
21
43
|
end
|
data/lib/peto/generator.rb
CHANGED
@@ -3,8 +3,8 @@ require "peto/mixin/peto_errorable"
|
|
3
3
|
module PetoClass
|
4
4
|
include PetoErrorable
|
5
5
|
|
6
|
-
def
|
7
|
-
|
6
|
+
def set_args(args)
|
7
|
+
args.each do |key, value|
|
8
8
|
var = instance_variable_get("@#{key}")
|
9
9
|
invalid_type(key, types[key], value) unless value.class == types[key] || value.nil?
|
10
10
|
raise_errors unless errors.empty?
|
@@ -50,9 +50,14 @@ module Peto
|
|
50
50
|
peto_class.send(:"#{procedure}_valid?", args)
|
51
51
|
end
|
52
52
|
|
53
|
-
def
|
54
|
-
|
55
|
-
|
53
|
+
def hash_to_args(procedure, hash_args)
|
54
|
+
peto_class.send(:"#{procedure}_hash_to_args", hash_args)
|
55
|
+
end
|
56
|
+
|
57
|
+
def call_subaction(procedure, hash_args)
|
58
|
+
args = hash_to_args(procedure, hash_args)
|
59
|
+
valid_args?(procedure, *args) # raise error
|
60
|
+
send(procedure, *args)
|
56
61
|
end
|
57
62
|
end
|
58
63
|
end
|
@@ -5,6 +5,21 @@ require "peto/mixin/peto_class"
|
|
5
5
|
module Peto
|
6
6
|
class <%= name %>
|
7
7
|
include PetoClass
|
8
|
+
|
9
|
+
def self.create(hash_args)
|
10
|
+
instance = new
|
11
|
+
<%- args.each do |arg| -%>
|
12
|
+
<%- if atomic_types.include?(arg[:type].constantize) -%>
|
13
|
+
instance.<%=arg[:name]%> = hash_args["<%= arg[:name]%>"]
|
14
|
+
<%- elsif arg[:array_type] -%>
|
15
|
+
instance.<%=arg[:name]%> = hash_args["<%= arg[:name]%>"].map {|arg| <%=arg[:array_type]%>.create(arg) }
|
16
|
+
<%- else -%>
|
17
|
+
instance.<%=arg[:name]%> = <%=arg[:type]%>.create(hash_args["<%= arg[:name]%>"])
|
18
|
+
<%- end -%>
|
19
|
+
<%- end -%>
|
20
|
+
return instance
|
21
|
+
end
|
22
|
+
|
8
23
|
def initialize(args={})
|
9
24
|
<%- args.each do |arg| -%>
|
10
25
|
<%- if arg[:array_type] -%>
|
@@ -14,7 +29,7 @@ module Peto
|
|
14
29
|
<%- end -%>
|
15
30
|
<%- end -%>
|
16
31
|
|
17
|
-
|
32
|
+
set_args(args)
|
18
33
|
raise_errors unless valid?
|
19
34
|
end
|
20
35
|
|
@@ -22,6 +37,13 @@ module Peto
|
|
22
37
|
attr_reader :<%= arg[:name] %>
|
23
38
|
<%- end -%>
|
24
39
|
|
40
|
+
<%- args.each do |arg| -%>
|
41
|
+
def <%= arg[:name] %>=(value)
|
42
|
+
@<%=arg[:name]%> = value
|
43
|
+
raise_errors unless valid?
|
44
|
+
end
|
45
|
+
<%- end -%>
|
46
|
+
|
25
47
|
def members
|
26
48
|
[<%= args.map{|arg| ":#{arg[:name]}"}.join(",") %>]
|
27
49
|
end
|
@@ -1,11 +1,20 @@
|
|
1
1
|
require "peto/mixin/peto_errorable"
|
2
2
|
|
3
|
+
<%-
|
4
|
+
def comma_args(args)
|
5
|
+
args.map{|arg| arg[:name]}.join(",")
|
6
|
+
end
|
7
|
+
-%>
|
8
|
+
|
3
9
|
module Peto
|
4
10
|
class <%= class_name %>
|
5
11
|
extend PetoErrorable
|
12
|
+
|
6
13
|
<%- each_procedures do |name, args| -%>
|
7
|
-
|
8
|
-
|
14
|
+
# <%=name%> methods
|
15
|
+
|
16
|
+
def self.<%= name %>(<%= comma_args(args) %>)
|
17
|
+
<%=name%>_valid?(<%= comma_args(args) %>)
|
9
18
|
return {
|
10
19
|
:procedure => "<%=name%>",
|
11
20
|
:args => {
|
@@ -16,18 +25,31 @@ module Peto
|
|
16
25
|
}
|
17
26
|
end
|
18
27
|
|
19
|
-
def self.<%=name%>_valid?(<%= args
|
28
|
+
def self.<%=name%>_valid?(<%= comma_args(args) %>)
|
20
29
|
<%- args.each do |arg| -%>
|
21
30
|
invalid_type("<%= arg[:name] %>", <%= arg[:type] %>, <%= arg[:name] %>) unless <%= arg[:name] %>.class == <%= arg[:type] %>
|
22
31
|
<%- end -%>
|
23
32
|
raise_errors unless errors.empty?
|
24
33
|
end
|
25
34
|
|
35
|
+
def self.<%=name%>_hash_to_args(hash_args)
|
36
|
+
args = []
|
37
|
+
<%- args.each do |arg| -%>
|
38
|
+
args.push(<%=arg[:type]%>.create(hash_args["<%=arg[:name]%>"]))
|
39
|
+
<%- end -%>
|
40
|
+
return args
|
41
|
+
end
|
42
|
+
|
43
|
+
|
26
44
|
<%- end -%>
|
27
45
|
|
28
46
|
def self.hashize(var)
|
29
|
-
return var if
|
47
|
+
return var if atomic_type?(var.class)
|
30
48
|
var.to_hash
|
31
49
|
end
|
50
|
+
|
51
|
+
def self.atomic_type?(type)
|
52
|
+
[<%=atomic_types.join(",")%>].include?(type)
|
53
|
+
end
|
32
54
|
end
|
33
55
|
end
|