peto 0.2.1 → 0.2.2

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.
Files changed (37) hide show
  1. data/README.rdoc +72 -1
  2. data/Rakefile +1 -0
  3. data/VERSION +1 -1
  4. data/examples/rails_app/app/controllers/application_controller.rb +9 -0
  5. data/examples/rails_app/app/controllers/foo_controller.rb +9 -0
  6. data/examples/rails_app/app/helpers/application_helper.rb +2 -0
  7. data/examples/rails_app/app/helpers/foo_helper.rb +2 -0
  8. data/examples/rails_app/config/application.rb +48 -0
  9. data/examples/rails_app/config/boot.rb +13 -0
  10. data/examples/rails_app/config/environment.rb +5 -0
  11. data/examples/rails_app/config/environments/development.rb +26 -0
  12. data/examples/rails_app/config/environments/production.rb +49 -0
  13. data/examples/rails_app/config/environments/test.rb +35 -0
  14. data/examples/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  15. data/examples/rails_app/config/initializers/inflections.rb +10 -0
  16. data/examples/rails_app/config/initializers/mime_types.rb +5 -0
  17. data/examples/rails_app/config/initializers/secret_token.rb +7 -0
  18. data/examples/rails_app/config/initializers/session_store.rb +8 -0
  19. data/examples/rails_app/config/routes.rb +60 -0
  20. data/examples/rails_app/contracts/animal.rb +27 -0
  21. data/examples/rails_app/contracts/foo.rb +36 -0
  22. data/examples/rails_app/contracts/generated/animal.rb +27 -0
  23. data/examples/rails_app/contracts/generated/foo.rb +59 -0
  24. data/examples/rails_app/contracts/generated/user.rb +31 -0
  25. data/examples/rails_app/contracts/user.rb +31 -0
  26. data/examples/rails_app/db/schema.rb +15 -0
  27. data/examples/rails_app/db/seeds.rb +7 -0
  28. data/examples/rails_app/test/functional/foo_controller_test.rb +9 -0
  29. data/examples/rails_app/test/performance/browsing_test.rb +9 -0
  30. data/examples/rails_app/test/test_helper.rb +18 -0
  31. data/examples/rails_app/test/unit/helpers/foo_helper_test.rb +4 -0
  32. data/lib/peto/generator.rb +1 -0
  33. data/lib/peto/rails/rails_controller_helper.rb +58 -0
  34. data/lib/peto/rails/rails_controller_test_helper.rb +18 -0
  35. data/lib/templates/rb_procedures.erb +8 -5
  36. metadata +62 -9
  37. data/lib/peto/rails/helper.rb +0 -22
data/README.rdoc CHANGED
@@ -17,9 +17,11 @@ contract file (foo.yml):
17
17
  args: [user:user]
18
18
 
19
19
  invoke command:
20
- % peto foo.yml
20
+ % mkdir generated
21
+ % peto foo.yml -o generated/
21
22
 
22
23
  use foo.rb:
24
+ $: << "generated/"
23
25
  require "foo"
24
26
  require "animal"
25
27
  require "user"
@@ -28,6 +30,7 @@ use foo.rb:
28
30
  dog = Peto::Animal.new(:name => "dog")
29
31
  user = Peto::User.new(:name => "alice", :age => 23, :animals=>[cat, dog])
30
32
 
33
+ # generating procedure hash
31
34
  p Peto::Foo.set_user(user) # => { :procedure=>"set_user",
32
35
  :args=>{ :user=>{ :name=>"alice",
33
36
  :age=>23,
@@ -36,6 +39,74 @@ use foo.rb:
36
39
  Peto::Animal({:name=>"dog"})
37
40
  ]}}}
38
41
 
42
+ == in Rails
43
+
44
+ === setup
45
+
46
+ create your new application
47
+ % rails new myapp
48
+
49
+ add autoload_paths to config/application.rb
50
+ config.autoload_paths += %W(#{config.root}/generated)
51
+
52
+ add including to app/controller/application_controller.rb
53
+ require "peto/rails/rails_controller_helper"
54
+
55
+ class ApplicationController < ActionController::Base
56
+ include Peto::RailsControllerHelper
57
+ # ...
58
+ end
59
+
60
+ add to test/test_helper.rb
61
+ require "peto/rails/rails_controller_test_helper"
62
+
63
+ class ActionController::TestCase
64
+ include Peto::RailsControllerTestHelper
65
+ end
66
+
67
+ add match to config/routes.rb
68
+ match ':controller(/:action)'
69
+
70
+ === using
71
+
72
+ % # on your rails home
73
+ % create contract/foo.yml
74
+ % mkdir generated
75
+ % peto contract/foo.yml -o generated/
76
+
77
+ add subaction to app/controller/foo_controller.rb
78
+
79
+ # this is subaction
80
+ def set_user(args)
81
+ respond(123, "foo bar baz")
82
+ end
83
+
84
+ subaction is an action method in imitation of rails controller.
85
+ this has args to use likes params.
86
+
87
+ post procedure name and parameters to application, be called subaction.
88
+ in test/functional/foo_controller_test.rb
89
+ test "set_user" do
90
+ peto_post("set_user", Peto::User.new(:name=>"alice"))
91
+ assert_response :success
92
+ assert_peto_response("a"=>123, "b"=>"foo bar baz")
93
+ end
94
+
95
+ run server
96
+
97
+ % rails server
98
+
99
+ post next json to http://server/foo/
100
+ {
101
+ "procedure" : "set_user",
102
+ "args" : {
103
+ "user" : {
104
+ "name" : "alice"
105
+ }
106
+ }
107
+ }
108
+
109
+
39
110
  == Note on Patches/Pull Requests
40
111
 
41
112
  * Fork the project.
data/Rakefile CHANGED
@@ -11,6 +11,7 @@ begin
11
11
  gem.homepage = "http://github.com/tosik/peto"
12
12
  gem.authors = ["Toshiyuki Hirooka"]
13
13
  gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ gem.files.exclude "examples"
14
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
16
  end
16
17
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -0,0 +1,9 @@
1
+ $: << "../lib"
2
+ $: << "../works"
3
+ require "peto/rails/rails_controller_helper"
4
+
5
+ class ApplicationController < ActionController::Base
6
+ include Peto::RailsControllerHelper
7
+
8
+ protect_from_forgery
9
+ end
@@ -0,0 +1,9 @@
1
+ require "foo"
2
+ require "user"
3
+ require "animal"
4
+
5
+ class FooController < ApplicationController
6
+ def set_user(args)
7
+ respond(123, "foo bar baz")
8
+ end
9
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module FooHelper
2
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ # If you have a Gemfile, require the gems listed there, including any gems
6
+ # you've limited to :test, :development, or :production.
7
+ Bundler.require(:default, Rails.env) if defined?(Bundler)
8
+
9
+ module RailsApp
10
+ class Application < Rails::Application
11
+ # Settings in config/environments/* take precedence over those specified here.
12
+ # Application configuration should go into files in config/initializers
13
+ # -- all .rb files in that directory are automatically loaded.
14
+
15
+ # Custom directories with classes and modules you want to be autoloadable.
16
+ # config.autoload_paths += %W(#{config.root}/extras)
17
+
18
+ # local peto lib. you dont need it if you install peto by gem.
19
+ config.autoload_paths += %W(#{config.root}/../../lib)
20
+
21
+ # generated files by peto
22
+ config.autoload_paths += %W(#{config.root}/contracts/generated)
23
+
24
+ # Only load the plugins named here, in the order given (default is alphabetical).
25
+ # :all can be used as a placeholder for all plugins not explicitly named.
26
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
27
+
28
+ # Activate observers that should always be running.
29
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
30
+
31
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
32
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
33
+ # config.time_zone = 'Central Time (US & Canada)'
34
+
35
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
36
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
37
+ # config.i18n.default_locale = :de
38
+
39
+ # JavaScript files you want as :defaults (application.js is always included).
40
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
41
+
42
+ # Configure the default encoding used in templates for Ruby 1.9.
43
+ config.encoding = "utf-8"
44
+
45
+ # Configure sensitive parameters which will be filtered from the log file.
46
+ config.filter_parameters += [:password]
47
+ end
48
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+
3
+ # Set up gems listed in the Gemfile.
4
+ gemfile = File.expand_path('../../Gemfile', __FILE__)
5
+ begin
6
+ ENV['BUNDLE_GEMFILE'] = gemfile
7
+ require 'bundler'
8
+ Bundler.setup
9
+ rescue Bundler::GemNotFound => e
10
+ STDERR.puts e.message
11
+ STDERR.puts "Try running `bundle install`."
12
+ exit!
13
+ end if File.exist?(gemfile)
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ RailsApp::Application.initialize!
@@ -0,0 +1,26 @@
1
+ Hoge::Application.configure do
2
+ # Settings specified here will take precedence over those in config/environment.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the webserver when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_view.debug_rjs = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Don't care if the mailer can't send
18
+ config.action_mailer.raise_delivery_errors = false
19
+
20
+ # Print deprecation notices to the Rails logger
21
+ config.active_support.deprecation = :log
22
+
23
+ # Only use best-standards-support built into browsers
24
+ config.action_dispatch.best_standards_support = :builtin
25
+ end
26
+
@@ -0,0 +1,49 @@
1
+ Hoge::Application.configure do
2
+ # Settings specified here will take precedence over those in config/environment.rb
3
+
4
+ # The production environment is meant for finished, "live" apps.
5
+ # Code is not reloaded between requests
6
+ config.cache_classes = true
7
+
8
+ # Full error reports are disabled and caching is turned on
9
+ config.consider_all_requests_local = false
10
+ config.action_controller.perform_caching = true
11
+
12
+ # Specifies the header that your server uses for sending files
13
+ config.action_dispatch.x_sendfile_header = "X-Sendfile"
14
+
15
+ # For nginx:
16
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
17
+
18
+ # If you have no front-end server that supports something like X-Sendfile,
19
+ # just comment this out and Rails will serve the files
20
+
21
+ # See everything in the log (default is :info)
22
+ # config.log_level = :debug
23
+
24
+ # Use a different logger for distributed setups
25
+ # config.logger = SyslogLogger.new
26
+
27
+ # Use a different cache store in production
28
+ # config.cache_store = :mem_cache_store
29
+
30
+ # Disable Rails's static asset server
31
+ # In production, Apache or nginx will already do this
32
+ config.serve_static_assets = false
33
+
34
+ # Enable serving of images, stylesheets, and javascripts from an asset server
35
+ # config.action_controller.asset_host = "http://assets.example.com"
36
+
37
+ # Disable delivery errors, bad email addresses will be ignored
38
+ # config.action_mailer.raise_delivery_errors = false
39
+
40
+ # Enable threaded mode
41
+ # config.threadsafe!
42
+
43
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
44
+ # the I18n.default_locale when a translation can not be found)
45
+ config.i18n.fallbacks = true
46
+
47
+ # Send deprecation notices to registered listeners
48
+ config.active_support.deprecation = :notify
49
+ end
@@ -0,0 +1,35 @@
1
+ RailsApp::Application.configure do
2
+ # Settings specified here will take precedence over those in config/environment.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Log error messages when you accidentally call methods on nil.
11
+ config.whiny_nils = true
12
+
13
+ # Show full error reports and disable caching
14
+ config.consider_all_requests_local = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Raise exceptions instead of rendering exception templates
18
+ config.action_dispatch.show_exceptions = false
19
+
20
+ # Disable request forgery protection in test environment
21
+ config.action_controller.allow_forgery_protection = false
22
+
23
+ # Tell Action Mailer not to deliver emails to the real world.
24
+ # The :test delivery method accumulates sent emails in the
25
+ # ActionMailer::Base.deliveries array.
26
+ config.action_mailer.delivery_method = :test
27
+
28
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
29
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
30
+ # like if you have constraints or database-specific column types
31
+ # config.active_record.schema_format = :sql
32
+
33
+ # Print deprecation notices to the stderr
34
+ config.active_support.deprecation = :stderr
35
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,10 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ RailsApp::Application.config.secret_token = '8e9ae8e11e65624137d5e3a6679648eae8b82c6e46cf4590b0d220d61bd408f73fc6a1566d8841faabcf4b18204d8e30570ead8dacfc3ac20950cd845b62b239'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ RailsApp::Application.config.session_store :cookie_store, :key => '_hoge_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rake db:sessions:create")
8
+ # RailsApp::Application.config.session_store :active_record_store
@@ -0,0 +1,60 @@
1
+ RailsApp::Application.routes.draw do
2
+ # The priority is based upon order of creation:
3
+ # first created -> highest priority.
4
+
5
+ # Sample of regular route:
6
+ # match 'products/:id' => 'catalog#view'
7
+ # Keep in mind you can assign values other than :controller and :action
8
+
9
+ # Sample of named route:
10
+ # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
11
+ # This route can be invoked with purchase_url(:id => product.id)
12
+
13
+ # Sample resource route (maps HTTP verbs to controller actions automatically):
14
+ # resources :products
15
+
16
+ # Sample resource route with options:
17
+ # resources :products do
18
+ # member do
19
+ # get 'short'
20
+ # post 'toggle'
21
+ # end
22
+ #
23
+ # collection do
24
+ # get 'sold'
25
+ # end
26
+ # end
27
+
28
+ # Sample resource route with sub-resources:
29
+ # resources :products do
30
+ # resources :comments, :sales
31
+ # resource :seller
32
+ # end
33
+
34
+ # Sample resource route with more complex sub-resources
35
+ # resources :products do
36
+ # resources :comments
37
+ # resources :sales do
38
+ # get 'recent', :on => :collection
39
+ # end
40
+ # end
41
+
42
+ # Sample resource route within a namespace:
43
+ # namespace :admin do
44
+ # # Directs /admin/products/* to Admin::ProductsController
45
+ # # (app/controllers/admin/products_controller.rb)
46
+ # resources :products
47
+ # end
48
+
49
+ # You can have the root of your site routed with "root"
50
+ # just remember to delete public/index.html.
51
+ # root :to => "welcome#index"
52
+
53
+ # See how all your routes lay out with "rake routes"
54
+
55
+ # This is a legacy wild controller route that's not recommended for RESTful applications.
56
+ # Note: This route will make all actions in every controller accessible via GET requests.
57
+ # match ':controller(/:action(/:id(.:format)))'
58
+
59
+ match ':controller(/:action)'
60
+ end
@@ -0,0 +1,27 @@
1
+ require "peto/mixin/peto_class"
2
+
3
+ module Peto
4
+ class Animal
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
@@ -0,0 +1,36 @@
1
+ require "peto/mixin/peto_errorable"
2
+
3
+ module Peto
4
+ class Foo
5
+ extend PetoErrorable
6
+ def self.set_user(user)
7
+ invalid_type("user", User, user) unless user.class == User
8
+ raise_errors unless errors.empty?
9
+
10
+ return {
11
+ :procedure => "set_user",
12
+ :args => {
13
+ :user => hashize(user),
14
+ }
15
+ }
16
+ end
17
+
18
+ def self.set_user_error_invalid_user(message)
19
+ invalid_type("message", String, message) unless message.class == String
20
+ raise_errors unless errors.empty?
21
+
22
+ return {
23
+ :procedure => "set_user_error_invalid_user",
24
+ :args => {
25
+ :message => hashize(message),
26
+ }
27
+ }
28
+ end
29
+
30
+
31
+ def self.hashize(var)
32
+ return var if [Fixnum, String].include?(var.class)
33
+ var.to_hash
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ require "peto/mixin/peto_class"
2
+
3
+ module Peto
4
+ class Animal
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
@@ -0,0 +1,59 @@
1
+ require "peto/mixin/peto_errorable"
2
+
3
+ module Peto
4
+ class Foo
5
+ extend PetoErrorable
6
+ def self.set_user(user)
7
+ set_user_valid?(user)
8
+ return {
9
+ :procedure => "set_user",
10
+ :args => {
11
+ :user => hashize(user),
12
+ }
13
+ }
14
+ end
15
+
16
+ def self.set_user_valid?(user)
17
+ invalid_type("user", User, user) unless user.class == User
18
+ raise_errors unless errors.empty?
19
+ end
20
+
21
+ def self.set_user_response(a,b)
22
+ set_user_response_valid?(a,b)
23
+ return {
24
+ :procedure => "set_user_response",
25
+ :args => {
26
+ :a => hashize(a),
27
+ :b => hashize(b),
28
+ }
29
+ }
30
+ end
31
+
32
+ def self.set_user_response_valid?(a,b)
33
+ invalid_type("a", Fixnum, a) unless a.class == Fixnum
34
+ invalid_type("b", String, b) unless b.class == String
35
+ raise_errors unless errors.empty?
36
+ end
37
+
38
+ def self.set_user_error_invalid_user(message)
39
+ set_user_error_invalid_user_valid?(message)
40
+ return {
41
+ :procedure => "set_user_error_invalid_user",
42
+ :args => {
43
+ :message => hashize(message),
44
+ }
45
+ }
46
+ end
47
+
48
+ def self.set_user_error_invalid_user_valid?(message)
49
+ invalid_type("message", String, message) unless message.class == String
50
+ raise_errors unless errors.empty?
51
+ end
52
+
53
+
54
+ def self.hashize(var)
55
+ return var if [Fixnum, String].include?(var.class)
56
+ var.to_hash
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,31 @@
1
+ require "peto/mixin/peto_class"
2
+
3
+ module Peto
4
+ class User
5
+ include PetoClass
6
+ def initialize(args={})
7
+ @name = nil
8
+ @age = nil
9
+ @animals = [] # for Animal
10
+
11
+ set_by_hash(args)
12
+ raise_errors unless valid?
13
+ end
14
+
15
+ attr_reader :name
16
+ attr_reader :age
17
+ attr_reader :animals
18
+
19
+ def members
20
+ [:name,:age,:animals]
21
+ end
22
+
23
+ def types
24
+ {:name => String,:age => Fixnum,:animals => Array}
25
+ end
26
+
27
+ def arrays
28
+ {:animals => Animal}
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require "peto/mixin/peto_class"
2
+
3
+ module Peto
4
+ class User
5
+ include PetoClass
6
+ def initialize(args={})
7
+ @name = nil
8
+ @age = nil
9
+ @animals = [] # for Animal
10
+
11
+ set_by_hash(args)
12
+ raise_errors unless valid?
13
+ end
14
+
15
+ attr_reader :name
16
+ attr_reader :age
17
+ attr_reader :animals
18
+
19
+ def members
20
+ [:name,:age,:animals]
21
+ end
22
+
23
+ def types
24
+ {:name => String,:age => Fixnum,:animals => Array}
25
+ end
26
+
27
+ def arrays
28
+ {:animals => Animal}
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # Note that this schema.rb definition is the authoritative source for your
6
+ # database schema. If you need to create the application database on another
7
+ # system, you should be using db:schema:load, not running all the migrations
8
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
9
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
10
+ #
11
+ # It's strongly recommended to check this file into your version control system.
12
+
13
+ ActiveRecord::Schema.define(:version => 0) do
14
+
15
+ end
@@ -0,0 +1,7 @@
1
+ # This file should contain all the record creation needed to seed the database with its default values.
2
+ # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
3
+ #
4
+ # Examples:
5
+ #
6
+ # cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
7
+ # Mayor.create(:name => 'Daley', :city => cities.first)
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class FooControllerTest < ActionController::TestCase
4
+ test "set_user" do
5
+ peto_post("set_user", Peto::User.new(:name=>"aho"))
6
+ assert_response :success
7
+ assert_peto_response("a"=>123, "b"=>"foo bar baz")
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+ require 'rails/performance_test_help'
3
+
4
+ # Profiling results for each test method are written to tmp/performance.
5
+ class BrowsingTest < ActionDispatch::PerformanceTest
6
+ def test_homepage
7
+ get '/'
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ require File.expand_path('../../config/environment', __FILE__)
3
+ require 'rails/test_help'
4
+
5
+ class ActiveSupport::TestCase
6
+ # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
7
+ #
8
+ # Note: You'll currently still have to declare fixtures explicitly in integration tests
9
+ # -- they do not yet inherit this setting
10
+ fixtures :all
11
+
12
+ # Add more helper methods to be used by all tests here...
13
+ end
14
+
15
+ require "peto/rails/rails_controller_test_helper"
16
+ class ActionController::TestCase
17
+ include Peto::RailsControllerTestHelper
18
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class FooHelperTest < ActionView::TestCase
4
+ end
@@ -65,6 +65,7 @@ module Peto
65
65
  def each_procedures
66
66
  (@contract["procedures"]||[]).each do |name, procedure|
67
67
  yield name.to_method_name, args(procedure["args"])
68
+ yield "#{name} response".to_method_name, args(procedure["returns"])
68
69
  (procedure["errors"]||[]).each do |error|
69
70
  yield "#{name} error #{error}".to_method_name, [arg("message", "string")]
70
71
  end
@@ -0,0 +1,58 @@
1
+
2
+ require "active_support/inflector"
3
+ require "pp"
4
+
5
+ module Peto
6
+ module RailsControllerHelper
7
+ def contract
8
+ self.class.to_s.sub("Controller", "")
9
+ end
10
+
11
+ def peto_class
12
+ "Peto::#{contract.to_s.camelize}".constantize
13
+ end
14
+
15
+ def parse_caller(at)
16
+ if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
17
+ {:file => $1, :line => $2.to_i, :method => $3.to_sym}
18
+ end
19
+ end
20
+
21
+
22
+ # respond
23
+
24
+ def procedure_name
25
+ parse_caller(caller[(0..caller.length).zip(caller).find {|index,at|
26
+ parse_caller(at)[:method] == :call_subaction
27
+ }.first - 1])[:method]
28
+ end
29
+
30
+ def call_procedure_response(response_type, *args)
31
+ peto_class.send(:"#{procedure_name}_#{response_type}", *args)
32
+ end
33
+
34
+ def respond(*args)
35
+ render(:json => call_procedure_response(:response, *args).to_json)
36
+ end
37
+
38
+ def error(error_name, *messages)
39
+ call_procedure_response(:"error_#{error_name}", messages.join(","))
40
+ end
41
+
42
+
43
+ # subaction
44
+
45
+ def index
46
+ call_subaction(params[:procedure], params[:args])
47
+ end
48
+
49
+ def valid_args?(procedure, args)
50
+ peto_class.send(:"#{procedure}_valid?", args)
51
+ end
52
+
53
+ def call_subaction(procedure, args)
54
+ valid_args?(procedure, args)
55
+ send(procedure, args)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,18 @@
1
+
2
+ module Peto
3
+ module RailsControllerTestHelper
4
+ def peto_post(procedure, args)
5
+ post :index, {:procedure=>procedure.to_s, :args=>args}
6
+ end
7
+
8
+ def decoded_response
9
+ @decoded_response ||= ActiveSupport::JSON.decode(@response.body)
10
+ end
11
+
12
+ def assert_peto_response(except)
13
+ assert_equal decoded_response["args"], except
14
+ end
15
+ end
16
+ end
17
+
18
+
@@ -5,11 +5,7 @@ module Peto
5
5
  extend PetoErrorable
6
6
  <%- each_procedures do |name, args| -%>
7
7
  def self.<%= name %>(<%= args.map{|arg| arg[:name]}.join(",") %>)
8
- <%- args.each do |arg| -%>
9
- invalid_type("<%= arg[:name] %>", <%= arg[:type] %>, <%= arg[:name] %>) unless <%= arg[:name] %>.class == <%= arg[:type] %>
10
- <%- end -%>
11
- raise_errors unless errors.empty?
12
-
8
+ <%=name%>_valid?(<%= args.map{|arg| arg[:name]}.join(",") %>)
13
9
  return {
14
10
  :procedure => "<%=name%>",
15
11
  :args => {
@@ -20,6 +16,13 @@ module Peto
20
16
  }
21
17
  end
22
18
 
19
+ def self.<%=name%>_valid?(<%= args.map{|arg| arg[:name]}.join(",") %>)
20
+ <%- args.each do |arg| -%>
21
+ invalid_type("<%= arg[:name] %>", <%= arg[:type] %>, <%= arg[:name] %>) unless <%= arg[:name] %>.class == <%= arg[:type] %>
22
+ <%- end -%>
23
+ raise_errors unless errors.empty?
24
+ end
25
+
23
26
  <%- end -%>
24
27
 
25
28
  def self.hashize(var)
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: peto
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 2
9
- - 1
10
- version: 0.2.1
8
+ - 2
9
+ version: 0.2.2
11
10
  platform: ruby
12
11
  authors:
13
12
  - Toshiyuki Hirooka
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-11-01 00:00:00 +09:00
17
+ date: 2010-11-03 00:00:00 +09:00
19
18
  default_executable: peto
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 3
30
28
  segments:
31
29
  - 0
32
30
  version: "0"
@@ -54,13 +52,42 @@ files:
54
52
  - lib/peto/master.rb
55
53
  - lib/peto/mixin/peto_class.rb
56
54
  - lib/peto/mixin/peto_errorable.rb
57
- - lib/peto/rails/helper.rb
55
+ - lib/peto/rails/rails_controller_helper.rb
56
+ - lib/peto/rails/rails_controller_test_helper.rb
58
57
  - lib/templates/rb_classes.erb
59
58
  - lib/templates/rb_procedures.erb
60
59
  - test/contracts/generating.yml
61
60
  - test/contracts/loading.yml
62
61
  - test/helper.rb
63
62
  - test/test_peto.rb
63
+ - examples/rails_app/config/environments/development.rb
64
+ - examples/rails_app/config/environments/test.rb
65
+ - examples/rails_app/config/environments/production.rb
66
+ - examples/rails_app/config/application.rb
67
+ - examples/rails_app/config/routes.rb
68
+ - examples/rails_app/config/boot.rb
69
+ - examples/rails_app/config/initializers/secret_token.rb
70
+ - examples/rails_app/config/initializers/session_store.rb
71
+ - examples/rails_app/config/initializers/inflections.rb
72
+ - examples/rails_app/config/initializers/backtrace_silencers.rb
73
+ - examples/rails_app/config/initializers/mime_types.rb
74
+ - examples/rails_app/config/environment.rb
75
+ - examples/rails_app/db/schema.rb
76
+ - examples/rails_app/db/seeds.rb
77
+ - examples/rails_app/app/controllers/application_controller.rb
78
+ - examples/rails_app/app/controllers/foo_controller.rb
79
+ - examples/rails_app/app/helpers/foo_helper.rb
80
+ - examples/rails_app/app/helpers/application_helper.rb
81
+ - examples/rails_app/test/performance/browsing_test.rb
82
+ - examples/rails_app/test/unit/helpers/foo_helper_test.rb
83
+ - examples/rails_app/test/test_helper.rb
84
+ - examples/rails_app/test/functional/foo_controller_test.rb
85
+ - examples/rails_app/contracts/animal.rb
86
+ - examples/rails_app/contracts/user.rb
87
+ - examples/rails_app/contracts/generated/animal.rb
88
+ - examples/rails_app/contracts/generated/user.rb
89
+ - examples/rails_app/contracts/generated/foo.rb
90
+ - examples/rails_app/contracts/foo.rb
64
91
  has_rdoc: true
65
92
  homepage: http://github.com/tosik/peto
66
93
  licenses: []
@@ -75,7 +102,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
102
  requirements:
76
103
  - - ">="
77
104
  - !ruby/object:Gem::Version
78
- hash: 3
79
105
  segments:
80
106
  - 0
81
107
  version: "0"
@@ -84,7 +110,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
110
  requirements:
85
111
  - - ">="
86
112
  - !ruby/object:Gem::Version
87
- hash: 3
88
113
  segments:
89
114
  - 0
90
115
  version: "0"
@@ -96,5 +121,33 @@ signing_key:
96
121
  specification_version: 3
97
122
  summary: contract code generator
98
123
  test_files:
99
- - test/test_peto.rb
100
124
  - test/helper.rb
125
+ - test/test_peto.rb
126
+ - examples/rails_app/config/environments/development.rb
127
+ - examples/rails_app/config/environments/test.rb
128
+ - examples/rails_app/config/environments/production.rb
129
+ - examples/rails_app/config/application.rb
130
+ - examples/rails_app/config/routes.rb
131
+ - examples/rails_app/config/boot.rb
132
+ - examples/rails_app/config/initializers/secret_token.rb
133
+ - examples/rails_app/config/initializers/session_store.rb
134
+ - examples/rails_app/config/initializers/inflections.rb
135
+ - examples/rails_app/config/initializers/backtrace_silencers.rb
136
+ - examples/rails_app/config/initializers/mime_types.rb
137
+ - examples/rails_app/config/environment.rb
138
+ - examples/rails_app/db/schema.rb
139
+ - examples/rails_app/db/seeds.rb
140
+ - examples/rails_app/app/controllers/application_controller.rb
141
+ - examples/rails_app/app/controllers/foo_controller.rb
142
+ - examples/rails_app/app/helpers/foo_helper.rb
143
+ - examples/rails_app/app/helpers/application_helper.rb
144
+ - examples/rails_app/test/performance/browsing_test.rb
145
+ - examples/rails_app/test/unit/helpers/foo_helper_test.rb
146
+ - examples/rails_app/test/test_helper.rb
147
+ - examples/rails_app/test/functional/foo_controller_test.rb
148
+ - examples/rails_app/contracts/animal.rb
149
+ - examples/rails_app/contracts/user.rb
150
+ - examples/rails_app/contracts/generated/animal.rb
151
+ - examples/rails_app/contracts/generated/user.rb
152
+ - examples/rails_app/contracts/generated/foo.rb
153
+ - examples/rails_app/contracts/foo.rb
@@ -1,22 +0,0 @@
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