peto 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Toshiyuki Hirooka
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = peto
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Toshiyuki Hirooka. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "peto"
8
+ gem.summary = %Q{contract code generator}
9
+ gem.description = %Q{contract code generator for multi-language}
10
+ gem.email = "toshi.hirooka@gmail.com"
11
+ gem.homepage = "http://github.com/tosik/peto"
12
+ gem.authors = ["Toshiyuki Hirooka"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "peto #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/peto ADDED
@@ -0,0 +1,8 @@
1
+ #!/bin/ruby
2
+
3
+ require "peto"
4
+
5
+ peto = Peto::Master.new
6
+ peto.load(ARGV.first)
7
+ puts peto.generate
8
+
@@ -0,0 +1,151 @@
1
+ require "peto/mixin/peto_class"
2
+ require "peto/mixin/peto_errorable"
3
+
4
+ module Peto
5
+ class TypeA
6
+ include PetoClass
7
+ def initialize(args={})
8
+ @foo = nil
9
+ @bar = nil
10
+ @baz = [] # for Fixnum
11
+
12
+ set_by_hash(args)
13
+ raise_errors unless valid?
14
+ end
15
+
16
+ attr_reader :foo
17
+ attr_reader :bar
18
+ attr_reader :baz
19
+
20
+ def members
21
+ [:foo,:bar,:baz]
22
+ end
23
+
24
+ def types
25
+ {:foo => Fixnum,:bar => String,:baz => Array}
26
+ end
27
+
28
+ def arrays
29
+ {:baz => Fixnum}
30
+ end
31
+ end
32
+ class TypeB
33
+ include PetoClass
34
+ def initialize(args={})
35
+ @foo = nil
36
+
37
+ set_by_hash(args)
38
+ raise_errors unless valid?
39
+ end
40
+
41
+ attr_reader :foo
42
+
43
+ def members
44
+ [:foo]
45
+ end
46
+
47
+ def types
48
+ {:foo => String}
49
+ end
50
+
51
+ def arrays
52
+ {}
53
+ end
54
+ end
55
+ class TypeC
56
+ include PetoClass
57
+ def initialize(args={})
58
+ @foo = [] # for String
59
+
60
+ set_by_hash(args)
61
+ raise_errors unless valid?
62
+ end
63
+
64
+ attr_reader :foo
65
+
66
+ def members
67
+ [:foo]
68
+ end
69
+
70
+ def types
71
+ {:foo => Array}
72
+ end
73
+
74
+ def arrays
75
+ {:foo => String}
76
+ end
77
+ end
78
+ end
79
+
80
+ module Peto
81
+ class Generating
82
+ extend PetoErrorable
83
+ def self.do_a(a,b,c)
84
+ invalid_type("a", Fixnum, a) unless a.class == Fixnum
85
+ invalid_type("b", String, b) unless b.class == String
86
+ invalid_type("c", TypeB, c) unless c.class == TypeB
87
+ raise_errors unless errors.empty?
88
+
89
+ return {
90
+ :procedure => "do_a",
91
+ :args => {
92
+ :a => hashize(a),
93
+ :b => hashize(b),
94
+ :c => hashize(c),
95
+ }
96
+ }
97
+ end
98
+
99
+ def self.do_a_error_not_found(message)
100
+ invalid_type("message", String, message) unless message.class == String
101
+ raise_errors unless errors.empty?
102
+
103
+ return {
104
+ :procedure => "do_a_error_not_found",
105
+ :args => {
106
+ :message => hashize(message),
107
+ }
108
+ }
109
+ end
110
+
111
+ def self.do_a_error_invalid_id(message)
112
+ invalid_type("message", String, message) unless message.class == String
113
+ raise_errors unless errors.empty?
114
+
115
+ return {
116
+ :procedure => "do_a_error_invalid_id",
117
+ :args => {
118
+ :message => hashize(message),
119
+ }
120
+ }
121
+ end
122
+
123
+ def self.do_b(a)
124
+ invalid_type("a", TypeA, a) unless a.class == TypeA
125
+ raise_errors unless errors.empty?
126
+
127
+ return {
128
+ :procedure => "do_b",
129
+ :args => {
130
+ :a => hashize(a),
131
+ }
132
+ }
133
+ end
134
+
135
+ def self.do_c()
136
+ raise_errors unless errors.empty?
137
+
138
+ return {
139
+ :procedure => "do_c",
140
+ :args => {
141
+ }
142
+ }
143
+ end
144
+
145
+
146
+ def self.hashize(var)
147
+ return var if [Fixnum, String].include?(var.class)
148
+ var.to_hash
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,68 @@
1
+
2
+ require "erb"
3
+ require "active_support/inflector"
4
+ require "active_support/core_ext/array/access"
5
+ require "pp"
6
+
7
+ class String
8
+ def to_method_name
9
+ underscore.split(" ").join("_")
10
+ end
11
+ def to_class_type
12
+ case self
13
+ when "s32"
14
+ "Fixnum"
15
+ else
16
+ classify
17
+ end
18
+ end
19
+ end
20
+
21
+ module Peto
22
+ class Generator
23
+ def initialize(contract)
24
+ @contract = contract
25
+ end
26
+
27
+ def generate(template_filename)
28
+ erb = ERB.new(IO.read(template_filename), nil, "-")
29
+ erb.result(binding)
30
+ end
31
+
32
+
33
+ def class_name
34
+ @contract["name"].to_class_type
35
+ end
36
+
37
+ def args(string_args)
38
+ string_args.map do |str|
39
+ splitted = str.split(":")
40
+ arg(splitted.first, splitted.second, :array_type => splitted.third)
41
+ end
42
+ end
43
+
44
+ def arg(name, type, options={})
45
+ array_type = options.delete(:array_type)
46
+ {
47
+ :name => name,
48
+ :type => type.to_class_type,
49
+ :array_type => array_type.nil? ? nil : array_type.to_class_type,
50
+ }
51
+ end
52
+
53
+ def each_types
54
+ @contract["types"].each do |name, args|
55
+ yield name.to_class_type, args(args)
56
+ end
57
+ end
58
+
59
+ def each_procedures
60
+ @contract["procedures"].each do |name, procedure|
61
+ yield name.to_method_name, args(procedure["args"])
62
+ procedure["errors"].each do |error|
63
+ yield "#{name} error #{error}".to_method_name, [arg("message", "string")]
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,17 @@
1
+ require "pathname"
2
+ require "yaml"
3
+ require "peto/generator"
4
+
5
+ module Peto
6
+ class Master
7
+ def load(filename)
8
+ @contract = YAML.load(IO.read(Pathname(filename)))
9
+ end
10
+ attr_reader :contract
11
+
12
+ def generate
13
+ Generator.new(@contract).generate("lib/templates/rb_procedures.erb")
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,40 @@
1
+ require "peto/mixin/peto_errorable"
2
+
3
+ module PetoClass
4
+ include PetoErrorable
5
+
6
+ def set_by_hash(hash)
7
+ hash.each do |key, value|
8
+ var = instance_variable_get("@#{key}")
9
+ invalid_type(key, types[key], value) unless value.class == types[key] || value.nil?
10
+ raise_errors unless errors.empty?
11
+ instance_variable_set("@#{key}", value)
12
+ end
13
+ end
14
+
15
+ def inspect
16
+ "#{self.class}(#{to_hash})"
17
+ end
18
+
19
+ def to_hash
20
+ inners = members.inject({}) {|result, member|
21
+ result[member] = instance_variable_get("@#{member}")
22
+ result
23
+ }
24
+ end
25
+
26
+ def valid?
27
+ to_hash.each do |key, value|
28
+ var = instance_variable_get("@#{key}")
29
+ invalid_type(key, types[key], value) unless value.class == types[key] || value.nil?
30
+ end
31
+
32
+ arrays.each do |array, klass|
33
+ var = instance_variable_get("@#{array}")
34
+ invalid_type(array, klass, var.first) unless var.empty? || var.first.class == klass
35
+ end
36
+
37
+ errors.empty?
38
+ end
39
+ end
40
+
@@ -0,0 +1,17 @@
1
+ module PetoErrorable
2
+ def errors
3
+ @errors ||= []
4
+ end
5
+
6
+ def add_error(error)
7
+ errors << error
8
+ end
9
+
10
+ def raise_errors
11
+ raise errors.join("\n")
12
+ end
13
+
14
+ def invalid_type(name, expect, real)
15
+ add_error(["invalid type : #{name}.class is expected #{expect}, but was #{real.class}"])
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+
2
+ require "active_support/inflector"
3
+
4
+ module Peto
5
+ module RailsHelper
6
+ def contract
7
+ :procedures
8
+ end
9
+
10
+ def call_procedure_response(procedure_name, *args)
11
+ "Peto::#{contract.to_s.camelize}".constantize.send("#{procedure_name}", args.join(","))
12
+ end
13
+
14
+ def respond(*args)
15
+ call_procedure_response(:response, *args)
16
+ end
17
+
18
+ def error(error_name, *messages)
19
+ call_procedure_response("error_#{error_name}", messages.join(","))
20
+ end
21
+ end
22
+ end
data/lib/peto.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "peto/master"
2
+ require "peto/generator"
3
+
@@ -0,0 +1,67 @@
1
+ require "peto/mixin/peto_class"
2
+ require "peto/mixin/peto_errorable"
3
+
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
+ module Peto
41
+ class <%= class_name %>
42
+ extend PetoErrorable
43
+ <%- each_procedures do |name, args| -%>
44
+ def self.<%= name %>(<%= args.map{|arg| arg[:name]}.join(",") %>)
45
+ <%- args.each do |arg| -%>
46
+ invalid_type("<%= arg[:name] %>", <%= arg[:type] %>, <%= arg[:name] %>) unless <%= arg[:name] %>.class == <%= arg[:type] %>
47
+ <%- end -%>
48
+ raise_errors unless errors.empty?
49
+
50
+ return {
51
+ :procedure => "<%=name%>",
52
+ :args => {
53
+ <%- args.each do |arg| -%>
54
+ :<%=arg[:name]%> => hashize(<%=arg[:name]%>),
55
+ <%- end -%>
56
+ }
57
+ }
58
+ end
59
+
60
+ <%- end -%>
61
+
62
+ def self.hashize(var)
63
+ return var if [Fixnum, String].include?(var.class)
64
+ var.to_hash
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,24 @@
1
+
2
+ name: generating
3
+
4
+ types:
5
+ type_a: [foo:s32, bar:string, baz:array:s32]
6
+ type_b: [foo:string]
7
+ type_c: [foo:array:string]
8
+
9
+ procedures:
10
+ do_a:
11
+ args: [a:s32, b:string, c:type_b]
12
+ returns: [d:s32, e:type_a]
13
+ errors: [not found, invalid id]
14
+
15
+ do_b:
16
+ args: [a:type_a]
17
+ returns: []
18
+ errors: []
19
+
20
+ do_c:
21
+ args: []
22
+ returns: []
23
+ errors: []
24
+
@@ -0,0 +1,3 @@
1
+
2
+ name: loading
3
+
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require "pp"
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'peto'
9
+
10
+ class Test::Unit::TestCase
11
+ end
data/test/test_peto.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'helper'
2
+
3
+ class Closed
4
+ end
5
+
6
+ class TestPeto < Test::Unit::TestCase
7
+ context "Master instance" do
8
+ setup do
9
+ @peto = Peto::Master.new
10
+ end
11
+
12
+ context "load contract yaml file" do
13
+ setup do
14
+ @peto.load("test/contracts/loading.yml")
15
+ end
16
+ should ".contract returns loaded contract" do
17
+ assert_equal({ "name" => "loading" }, @peto.contract)
18
+ end
19
+ end
20
+
21
+ context "generate procedures" do
22
+ setup do
23
+ @peto.load("test/contracts/generating.yml")
24
+ end
25
+ should "returns string by loaded contract" do
26
+ assert_equal String, @peto.generate.class
27
+ end
28
+ end
29
+ end
30
+
31
+ context "Generated ruby script" do
32
+ setup do
33
+ @peto = Peto::Master.new
34
+ @peto.load("test/contracts/generating.yml")
35
+ @generated = @peto.generate
36
+ end
37
+ should "is readable as ruby" do
38
+ Closed.class_eval(@generated)
39
+ assert_equal({
40
+ :procedure => "do_a",
41
+ :args => {
42
+ :a => 1,
43
+ :b => "two",
44
+ :c => {:foo=>"foo"},
45
+ }
46
+ }, Closed::Peto::Generating.do_a(1, "two", Closed::Peto::TypeB.new(:foo=>"foo")))
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: peto
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Toshiyuki Hirooka
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-31 00:00:00 +09:00
18
+ default_executable: peto
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: thoughtbot-shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ description: contract code generator for multi-language
34
+ email: toshi.hirooka@gmail.com
35
+ executables:
36
+ - peto
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - LICENSE
41
+ - README.rdoc
42
+ files:
43
+ - .document
44
+ - .gitignore
45
+ - LICENSE
46
+ - README.rdoc
47
+ - Rakefile
48
+ - VERSION
49
+ - bin/peto
50
+ - lib/peto.rb
51
+ - lib/peto/generator.rb
52
+ - lib/peto/master.rb
53
+ - lib/peto/mixin/peto_class.rb
54
+ - lib/peto/mixin/peto_errorable.rb
55
+ - lib/peto/rails/helper.rb
56
+ - lib/templates/rb_procedures.erb
57
+ - test/contracts/generating.yml
58
+ - test/contracts/loading.yml
59
+ - test/helper.rb
60
+ - test/test_peto.rb
61
+ - examples/generated.rb
62
+ has_rdoc: true
63
+ homepage: http://github.com/tosik/peto
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --charset=UTF-8
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.7
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: contract code generator
94
+ test_files:
95
+ - test/helper.rb
96
+ - test/test_peto.rb
97
+ - examples/generated.rb