peto 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +9 -9
- data/VERSION +1 -1
- data/bin/peto +17 -2
- data/lib/peto/generator.rb +8 -2
- data/lib/peto/master.rb +32 -2
- data/lib/templates/rb_classes.erb +37 -0
- data/lib/templates/rb_procedures.erb +0 -37
- data/test/test_peto.rb +16 -8
- metadata +4 -3
data/README.rdoc
CHANGED
@@ -2,11 +2,10 @@
|
|
2
2
|
|
3
3
|
== Usage
|
4
4
|
|
5
|
-
installing
|
6
|
-
gem install peto
|
7
|
-
|
8
|
-
contract file (foo.yml)
|
5
|
+
installing:
|
6
|
+
% gem install peto
|
9
7
|
|
8
|
+
contract file (foo.yml):
|
10
9
|
name: foo
|
11
10
|
|
12
11
|
types:
|
@@ -17,12 +16,13 @@ contract file (foo.yml)
|
|
17
16
|
set_user:
|
18
17
|
args: [user:user]
|
19
18
|
|
20
|
-
invoke command
|
21
|
-
peto foo.yml
|
22
|
-
|
23
|
-
use generated.rb
|
19
|
+
invoke command:
|
20
|
+
% peto foo.yml
|
24
21
|
|
25
|
-
|
22
|
+
use foo.rb:
|
23
|
+
require "foo"
|
24
|
+
require "animal"
|
25
|
+
require "user"
|
26
26
|
|
27
27
|
cat = Peto::Animal.new(:name => "cat")
|
28
28
|
dog = Peto::Animal.new(:name => "dog")
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/bin/peto
CHANGED
@@ -4,7 +4,22 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
4
4
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
5
|
require "peto"
|
6
6
|
|
7
|
+
require "optparse"
|
8
|
+
options = {}
|
9
|
+
OptionParser.new do |opt|
|
10
|
+
opt.on("-o output directory") {|value| options[:output] = value }
|
11
|
+
opt.parse!(ARGV)
|
12
|
+
end
|
13
|
+
input = ARGV.pop
|
14
|
+
|
15
|
+
if input.nil?
|
16
|
+
puts "usage:\n % peto contract.yml"
|
17
|
+
exit
|
18
|
+
end
|
19
|
+
|
20
|
+
options[:output] = File.dirname(input) if options[:output].nil?
|
21
|
+
|
7
22
|
peto = Peto::Master.new
|
8
|
-
peto.load(
|
9
|
-
|
23
|
+
peto.load(input)
|
24
|
+
peto.generate
|
10
25
|
|
data/lib/peto/generator.rb
CHANGED
@@ -23,12 +23,18 @@ module Peto
|
|
23
23
|
def initialize(contract)
|
24
24
|
@contract = contract
|
25
25
|
end
|
26
|
+
attr_reader :contract
|
26
27
|
|
27
|
-
def
|
28
|
+
def generate_procedure(template_filename)
|
28
29
|
erb = ERB.new(IO.read(template_filename), nil, "-")
|
29
|
-
erb.result(binding)
|
30
|
+
{ "#{@contract["name"]}" => erb.result(binding) }
|
30
31
|
end
|
31
32
|
|
33
|
+
def generate_class(template_filename, type)
|
34
|
+
erb = ERB.new(IO.read(template_filename), nil, "-")
|
35
|
+
@target = {:name => type.first, :args => args(type.second)}
|
36
|
+
{ "#{@target[:name]}" => erb.result(binding) }
|
37
|
+
end
|
32
38
|
|
33
39
|
def class_name
|
34
40
|
@contract["name"].to_class_type
|
data/lib/peto/master.rb
CHANGED
@@ -3,15 +3,45 @@ require "yaml"
|
|
3
3
|
require "peto/generator"
|
4
4
|
|
5
5
|
module Peto
|
6
|
-
TEMPLATE_DIR = File::dirname(
|
6
|
+
TEMPLATE_DIR = File::dirname(File::expand_path( __FILE__ )) + "/../templates"
|
7
7
|
class Master
|
8
8
|
def load(filename)
|
9
|
+
@work_dir = File::dirname(filename)
|
9
10
|
@contract = YAML.load(IO.read(Pathname(filename)))
|
10
11
|
end
|
11
12
|
attr_reader :contract
|
12
13
|
|
14
|
+
def parse
|
15
|
+
(@contract["types"]||{}).inject({}) {|result, type|
|
16
|
+
result.merge!(Generator.new(@contract).generate_class(TEMPLATE_DIR + "/rb_classes.erb", type))
|
17
|
+
}.merge!(Generator.new(@contract).generate_procedure(TEMPLATE_DIR + "/rb_procedures.erb"))
|
18
|
+
end
|
19
|
+
|
13
20
|
def generate
|
14
|
-
|
21
|
+
parse.each do |name, content|
|
22
|
+
filepath = File.join(@work_dir, "#{name}.rb")
|
23
|
+
write(filepath, content)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def write(filepath, content)
|
28
|
+
print " "
|
29
|
+
if File.exist?(filepath)
|
30
|
+
if File.read(filepath) == content
|
31
|
+
print "identical"
|
32
|
+
else
|
33
|
+
print "update "
|
34
|
+
end
|
35
|
+
else
|
36
|
+
print "create "
|
37
|
+
end
|
38
|
+
print " "
|
39
|
+
|
40
|
+
open(filepath, "w") do |file|
|
41
|
+
file.write(content)
|
42
|
+
end
|
43
|
+
|
44
|
+
puts filepath
|
15
45
|
end
|
16
46
|
end
|
17
47
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "peto/mixin/peto_class"
|
2
|
+
|
3
|
+
<%- name = @target[:name].camelize -%>
|
4
|
+
<%- args = @target[:args] -%>
|
5
|
+
module Peto
|
6
|
+
class <%= name %>
|
7
|
+
include PetoClass
|
8
|
+
def initialize(args={})
|
9
|
+
<%- args.each do |arg| -%>
|
10
|
+
<%- if arg[:array_type] -%>
|
11
|
+
@<%= arg[:name] %> = [] # for <%= arg[:array_type] %>
|
12
|
+
<%- else -%>
|
13
|
+
@<%= arg[:name] %> = nil
|
14
|
+
<%- end -%>
|
15
|
+
<%- end -%>
|
16
|
+
|
17
|
+
set_by_hash(args)
|
18
|
+
raise_errors unless valid?
|
19
|
+
end
|
20
|
+
|
21
|
+
<%- args.each do |arg| -%>
|
22
|
+
attr_reader :<%= arg[:name] %>
|
23
|
+
<%- end -%>
|
24
|
+
|
25
|
+
def members
|
26
|
+
[<%= args.map{|arg| ":#{arg[:name]}"}.join(",") %>]
|
27
|
+
end
|
28
|
+
|
29
|
+
def types
|
30
|
+
{<%= args.map{|arg| ":#{arg[:name]} => #{arg[:type]}"}.join(",") %>}
|
31
|
+
end
|
32
|
+
|
33
|
+
def arrays
|
34
|
+
{<%= args.select{|arg|arg[:array_type]}.map{|arg| ":#{arg[:name]} => #{arg[:array_type]}"}.join(",") %>}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -1,42 +1,5 @@
|
|
1
|
-
require "peto/mixin/peto_class"
|
2
1
|
require "peto/mixin/peto_errorable"
|
3
2
|
|
4
|
-
module Peto
|
5
|
-
<%- each_types do |name, args| -%>
|
6
|
-
class <%= name %>
|
7
|
-
include PetoClass
|
8
|
-
def initialize(args={})
|
9
|
-
<%- args.each do |arg| -%>
|
10
|
-
<%- if arg[:array_type] -%>
|
11
|
-
@<%= arg[:name] %> = [] # for <%= arg[:array_type] %>
|
12
|
-
<%- else -%>
|
13
|
-
@<%= arg[:name] %> = nil
|
14
|
-
<%- end -%>
|
15
|
-
<%- end -%>
|
16
|
-
|
17
|
-
set_by_hash(args)
|
18
|
-
raise_errors unless valid?
|
19
|
-
end
|
20
|
-
|
21
|
-
<%- args.each do |arg| -%>
|
22
|
-
attr_reader :<%= arg[:name] %>
|
23
|
-
<%- end -%>
|
24
|
-
|
25
|
-
def members
|
26
|
-
[<%= args.map{|arg| ":#{arg[:name]}"}.join(",") %>]
|
27
|
-
end
|
28
|
-
|
29
|
-
def types
|
30
|
-
{<%= args.map{|arg| ":#{arg[:name]} => #{arg[:type]}"}.join(",") %>}
|
31
|
-
end
|
32
|
-
|
33
|
-
def arrays
|
34
|
-
{<%= args.select{|arg|arg[:array_type]}.map{|arg| ":#{arg[:name]} => #{arg[:array_type]}"}.join(",") %>}
|
35
|
-
end
|
36
|
-
end
|
37
|
-
<%- end -%>
|
38
|
-
end
|
39
|
-
|
40
3
|
module Peto
|
41
4
|
class <%= class_name %>
|
42
5
|
extend PetoErrorable
|
data/test/test_peto.rb
CHANGED
@@ -9,21 +9,27 @@ class TestPeto < Test::Unit::TestCase
|
|
9
9
|
@peto = Peto::Master.new
|
10
10
|
end
|
11
11
|
|
12
|
-
context "
|
12
|
+
context "loads contract yaml file" do
|
13
13
|
setup do
|
14
14
|
@peto.load("test/contracts/loading.yml")
|
15
15
|
end
|
16
|
-
|
17
|
-
|
16
|
+
context ".contract" do
|
17
|
+
should "return loaded contract" do
|
18
|
+
assert_equal({ "name" => "loading" }, @peto.contract)
|
19
|
+
end
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
21
|
-
context "
|
23
|
+
context "parse procedures" do
|
22
24
|
setup do
|
23
25
|
@peto.load("test/contracts/generating.yml")
|
24
26
|
end
|
25
27
|
should "returns string by loaded contract" do
|
26
|
-
assert_equal
|
28
|
+
assert_equal Hash, @peto.parse.class
|
29
|
+
@peto.parse.each do |name, content|
|
30
|
+
assert_equal String, name.class
|
31
|
+
assert_equal String, content.class
|
32
|
+
end
|
27
33
|
end
|
28
34
|
end
|
29
35
|
end
|
@@ -32,10 +38,12 @@ class TestPeto < Test::Unit::TestCase
|
|
32
38
|
setup do
|
33
39
|
@peto = Peto::Master.new
|
34
40
|
@peto.load("test/contracts/generating.yml")
|
35
|
-
@generated = @peto.
|
41
|
+
@generated = @peto.parse
|
36
42
|
end
|
37
|
-
should "
|
38
|
-
|
43
|
+
should "be readable as ruby" do
|
44
|
+
@generated.each do |filepath, content|
|
45
|
+
Closed.class_eval(content)
|
46
|
+
end
|
39
47
|
assert_equal({
|
40
48
|
:procedure => "do_a",
|
41
49
|
:args => {
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Toshiyuki Hirooka
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- lib/peto/mixin/peto_class.rb
|
54
54
|
- lib/peto/mixin/peto_errorable.rb
|
55
55
|
- lib/peto/rails/helper.rb
|
56
|
+
- lib/templates/rb_classes.erb
|
56
57
|
- lib/templates/rb_procedures.erb
|
57
58
|
- test/contracts/generating.yml
|
58
59
|
- test/contracts/loading.yml
|