resty-generators 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Kristian Meier
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.
@@ -0,0 +1,54 @@
1
+ require 'rails/generators/named_base'
2
+ module Resty
3
+ module Generators
4
+ class Base < Rails::Generators::NamedBase
5
+
6
+ protected
7
+
8
+ def application_name
9
+ @application_name ||= Rails.application.class.to_s.gsub(/::/,'').sub(/Application$/, '')
10
+ end
11
+
12
+ def java_root
13
+ @java_root ||= File.join('src', 'main', 'java')
14
+ end
15
+
16
+ def base_package
17
+ @base_package ||=
18
+ begin
19
+ fullpath = find_gwt_xml(java_root)
20
+ raise "no gwt module found - maybe run 'rails g resty:setup'" unless fullpath
21
+ fullpath.sub(/#{java_root}./, '').sub(/[a-zA-Z0-9_]+.gwt.xml$/, '').gsub(/[\/\\]/, '.') + "client"
22
+ end
23
+ end
24
+
25
+ def models_base_package
26
+ @models_base_package ||= base_package + ".models"
27
+ end
28
+
29
+ def controllers_base_package
30
+ @controllers_base_package ||= base_package + ".controllers"
31
+ end
32
+
33
+ def action_map
34
+ @action_map ||= {'index' => :get_all, 'show' => :get_single, 'create' => :post, 'update' => :put, 'destroy' => :delete}
35
+ end
36
+
37
+ def type_map
38
+ @type_map ||= {:integer => 'int', :boolean => 'bool', :string => 'String', :float => 'Double', :date => 'java.util.Date', :datetime => 'java.util.Date', :number => 'long', :fixnum => 'long'}
39
+ end
40
+
41
+ def find_gwt_xml(basedir)
42
+ Dir[File.join(basedir, "*")].each do |path|
43
+ if File.directory?(path)
44
+ result = find_gwt_xml(path)
45
+ return result if result
46
+ elsif File.file?(path)
47
+ return path if path =~ /.gwt.xml$/
48
+ end
49
+ end
50
+ nil
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,18 @@
1
+ require 'rails/generators/resource_helpers'
2
+ require 'generators/resty/base'
3
+ module Resty
4
+ module Generators
5
+ class ControllerGenerator < Base
6
+ include Rails::Generators::ResourceHelpers
7
+
8
+ source_root File.expand_path('../../templates', __FILE__)
9
+
10
+ argument :actions, :type => :array, :default => [], :banner => "action action"
11
+
12
+ def create_controller_file
13
+ template 'Controller.java', File.join(java_root, controllers_base_package.gsub(/\./, "/"), class_path, "#{controller_class_name}Controller.java")
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails/generators/resource_helpers'
2
+ require 'generators/resty/base'
3
+ module Resty
4
+ module Generators
5
+ class ModelGenerator < Base
6
+
7
+ source_root File.expand_path('../../templates', __FILE__)
8
+
9
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
10
+
11
+ if defined? ::Ixtlan::ModifiedBy
12
+ class_option :modified_by, :type => :boolean
13
+ end
14
+ class_option :timestamps, :type => :boolean
15
+ class_option :parent, :type => :string, :desc => "The parent class for the generated model"
16
+
17
+ def create_model_file
18
+ template 'Model.java', File.join(java_root, models_base_package.gsub(/\./, "/"), class_path, "#{class_name}.java")
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,40 @@
1
+ require 'rails/generators/resource_helpers'
2
+ require 'generators/resty/base'
3
+ module Resty
4
+ module Generators
5
+ class ScaffoldGenerator < Base
6
+ include Rails::Generators::ResourceHelpers
7
+
8
+ source_root File.expand_path('../../templates', __FILE__)
9
+
10
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
11
+
12
+ if defined? ::Ixtlan::ModifiedBy
13
+ class_option :modified_by, :type => :boolean
14
+ end
15
+ class_option :timestamps, :type => :boolean
16
+ class_option :parent, :type => :string, :desc => "The parent class for the generated model"
17
+ class_option :singleton, :type => :boolean
18
+
19
+ def create_model_file
20
+ template 'Model.java', File.join(java_root, models_base_package.gsub(/\./, "/"), class_path, "#{class_name}.java")
21
+ end
22
+
23
+ def create_controller_file
24
+ template 'Controller.java', File.join(java_root, controllers_base_package.gsub(/\./, "/"), class_path, "#{controller_class_name}Controller.java")
25
+ end
26
+
27
+ def actions
28
+ if options[:singleton]
29
+ keys = action_map.keys
30
+ keys.delete('index')
31
+ keys.delete('create')
32
+ keys.delete('destroy')
33
+ keys
34
+ else
35
+ action_map.keys
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,37 @@
1
+ require 'generators/resty/base'
2
+ module Resty
3
+ module Generators
4
+ class SetupGenerator < Base
5
+
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ arguments.clear # clear name argument from NamedBase
9
+
10
+ argument :gwt_module_name, :type => :string, :required => true
11
+
12
+ def name
13
+ gwt_module_name
14
+ end
15
+
16
+ def create_module_file
17
+ template 'module.gwt.xml', File.join(java_root, name.gsub(/\./, "/"), "#{application_name.underscore}.gwt.xml")
18
+ end
19
+
20
+ def create_maven_file
21
+ template 'Mavenfile', File.join("Mavenfile")
22
+ end
23
+
24
+ def create_entry_point_file
25
+ template 'EntryPoint.java', File.join(java_root, base_package.gsub(/\./, "/"), "#{application_name}.java")
26
+ end
27
+
28
+ def create_initializer
29
+ template 'initializer.rb', File.join('config', 'initializers', 'resty.rb')
30
+ end
31
+
32
+ def base_package
33
+ name + ".client"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ package <%= base_package %>;
2
+
3
+ import com.google.gwt.core.client.EntryPoint;
4
+ import com.google.gwt.user.client.ui.Label;
5
+ import com.google.gwt.user.client.ui.RootPanel;
6
+
7
+ /**
8
+ * Entry point classes define <code>onModuleLoad()</code>.
9
+ */
10
+ public class <%= application_name %> implements EntryPoint {
11
+
12
+ /**
13
+ * This is the entry point method.
14
+ */
15
+ public void onModuleLoad() {
16
+ RootPanel.get().add(new Label("hello world"));
17
+ }
18
+
19
+ }
@@ -0,0 +1,16 @@
1
+ jar('org.fusesource.restygwt:restygwt', '1.1').scope = :provided
2
+ plugin('org.codehaus.mojo.gwt-maven-plugin', '2.1.0') do |gwt|
3
+ gwt.with({ :logLevel => "INFO",
4
+ :style => "DETAILED",
5
+ :treeLogger => true,
6
+ :extraJvmArgs => "-Xmx512m",
7
+ :runTarget => "<%= application_name.underscore %>/<%= application_name.underscore %>.html",
8
+ :includes => "**/<%= application_name %>GWTTestSuite.java"
9
+ })
10
+ gwt.execute.goals << ["clean", "compile", "test"]
11
+ end
12
+ #-- Macs need the -d32 -XstartOnFirstThread jvm options -->
13
+ profile("mac") do |mac|
14
+ mac.activation << "<os><family>mac</family></os>"
15
+ plugin('org.codehaus.mojo.gwt-maven-plugin').with(:extraJvmArgs => "-d32 -XstartOnFirstThread -Xmx512m")
16
+ end
@@ -0,0 +1,3 @@
1
+ require 'resty/child_path'
2
+
3
+ Rails.application.config.middleware.use Resty::ChildPath, '<%= application_name.underscore %>'
@@ -0,0 +1,10 @@
1
+ <module rename-to='<%= application_name.underscore %>'>
2
+
3
+ <inherits name='com.google.gwt.user.User' />
4
+ <inherits name="org.fusesource.restygwt.RestyGWT" />
5
+
6
+ <entry-point class='<%= base_package %>.<%= application_name %>' />
7
+
8
+ <stylesheet src='<%= application_name.underscore %>.css' />
9
+
10
+ </module>
@@ -0,0 +1,49 @@
1
+ package <%= controllers_base_package %>;
2
+
3
+ <% if action_map.values.member? :get_all -%>
4
+ import java.util.List;
5
+ <% end -%>
6
+
7
+ import javax.ws.rs.*;
8
+
9
+ import org.fusesource.restygwt.client.*;
10
+
11
+ <% if name -%>
12
+ import <%= models_base_package %>.*;
13
+ <% end -%>
14
+
15
+ @Path("/<%= table_name %><%= options[:singleton] ? '.json' : '' %>")
16
+ public interface <%= controller_class_name %>Controller extends RestService {
17
+
18
+ <% actions.each do |action|
19
+ case action_map[action]
20
+ when :get_all -%>
21
+ @GET @Path('.json')
22
+ void <%= action %>(MethodCallback<List<<%= class_name %>>> callback);
23
+
24
+ // @GET @Path('.json')
25
+ // void <%= action %>(MethodCallback<List<<%= class_name %>>> callback, @QueryParam("limit") int limit, @QueryParam("offset") int offset);
26
+ //
27
+ <% when :get_single -%>
28
+ @GET<% unless options[:singleton] -%> @Path("/{id}.json")<% end %>
29
+ void <%= action %>(<% unless options[:singleton] -%>@PathParam("id") int id, <% end -%>MethodCallback<<%= class_name %>> callback);
30
+
31
+ <% when :post -%>
32
+ @POST @Path('.json')
33
+ void <%= action %>(<%= class_name %> value, MethodCallback<<%= class_name %>> callback);
34
+
35
+ <% when :put -%>
36
+ @PUT<% unless options[:singleton] -%> @Path("/{id}.json")<% end %>
37
+ void <%= action %>(<% unless options[:singleton] -%>@PathParam("id") @Attribute("id") <% end -%><%= class_name %> value, MethodCallback<<%= class_name %>> callback);
38
+
39
+ <% when :delete -%>
40
+ @DELETE @Path("/{id}.json")
41
+ void <%= action %>(@PathParam("id") @Attribute("id") <%= class_name %> value, MethodCallback<Void> callback);
42
+
43
+ <% else -%>
44
+ @GET @Path("/<%= action %>.json")
45
+ void <%= action %>(MethodCallback<Void> callback);
46
+
47
+ <% end
48
+ end -%>
49
+ }
@@ -0,0 +1,23 @@
1
+ package <%= models_base_package %>;
2
+
3
+ public class <%= class_name %> {
4
+
5
+ <% for attribute in attributes -%>
6
+ <% if attribute.type == :has_one -%>
7
+ <%= attribute.name.classify %> <%= attribute.name %>;
8
+ <% elsif attribute.type == :has_many -%>
9
+ public java.util.List<<%= attribute.name.classify %>> <%= attribute.name %>;
10
+ <% else -%>
11
+ public <%= type_map[attribute.type] || attribute.type.to_s.classify %> <%= attribute.name.classify.sub(/^(.)/){ $1.downcase } %>;
12
+ <% end -%>
13
+
14
+ <% end -%>
15
+ <% if options[:timestamps] %>
16
+ //TODO timestamps
17
+
18
+ <% end -%>
19
+ <% if options[:modified_by] %>
20
+ //TODO modified_by
21
+
22
+ <% end -%>
23
+ }
@@ -0,0 +1,18 @@
1
+ module Resty
2
+ class ChildPath
3
+ def initialize(app, rootpath)
4
+ @app = app
5
+ @rootpath = rootpath
6
+ end
7
+
8
+ def call(env)
9
+ ['REQUEST_PATH','PATH_INFO','REQUEST_URI','SCRIPT_NAME'].each do |key|
10
+ if(env[key] =~ /\.json([?].*)?$/)
11
+ env[key].gsub!(/^\/#{@rootpath}\//, "/")
12
+ end
13
+ end
14
+ @app.call(env)
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,44 @@
1
+ require 'rails'
2
+
3
+ module Resty
4
+ class RestyRailtie < Rails::Railtie
5
+
6
+ config.generators do
7
+ require 'rails/generators'
8
+ require 'rails/generators/rails/controller/controller_generator'
9
+ #require 'rails/generators/erb/scaffold/scaffold_generator'
10
+ Rails::Generators::ControllerGenerator.hook_for :resty, :type => :boolean, :default => true do |controller|
11
+ invoke controller, [ class_name, actions ]
12
+ end
13
+ #Erb::Generators::ScaffoldGenerator.source_paths.insert(0, File.expand_path('../../generators/ixtlan/templates', __FILE__))
14
+ end
15
+
16
+ config.after_initialize do
17
+ ActiveRecord::Base.include_root_in_json = false
18
+ # TODO there migt be a way to tell ALL ActiveModel:
19
+ #ActiveModel::Base.include_root_in_json = false
20
+
21
+ # get the time/date format right ;-) and match it with resty
22
+ class DateTime
23
+ def as_json(options = nil)
24
+ strftime('%Y-%m-%dT%H:%M:%S.%s%z')
25
+ end
26
+ end
27
+ class ActiveSupport::TimeWithZone
28
+ def as_json(options = nil)
29
+ strftime('%Y-%m-%dT%H:%M:%S.%s%z')
30
+ end
31
+ end
32
+ class Date
33
+ def as_json(options = nil)
34
+ strftime('%Y-%m-%dT%H:%M:%S.%s%z')
35
+ end
36
+ end
37
+ class Time
38
+ def as_json(options = nil)
39
+ strftime('%Y-%m-%dT%H:%M:%S.%s%z')
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ if defined?(Rails)
2
+ require 'resty/resty_railtie'
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resty-generators
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - mkristian
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-01 00:00:00 +05:30
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - "="
23
+ - !ruby/object:Gem::Version
24
+ version: 3.0.1
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - "="
34
+ - !ruby/object:Gem::Version
35
+ version: 2.0.1
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: cucumber
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - "="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.9.4
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rake
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - "="
56
+ - !ruby/object:Gem::Version
57
+ version: 0.8.7
58
+ type: :development
59
+ version_requirements: *id004
60
+ description: simple authorization framework for rails controllers
61
+ email:
62
+ - m.kristian@web.de
63
+ executables: []
64
+
65
+ extensions: []
66
+
67
+ extra_rdoc_files: []
68
+
69
+ files:
70
+ - MIT-LICENSE
71
+ - lib/resty-generators.rb
72
+ - lib/generators/resty/base.rb
73
+ - lib/generators/resty/model/model_generator.rb
74
+ - lib/generators/resty/scaffold/scaffold_generator.rb
75
+ - lib/generators/resty/controller/controller_generator.rb
76
+ - lib/generators/resty/templates/Controller.java
77
+ - lib/generators/resty/templates/Model.java
78
+ - lib/generators/resty/setup/setup_generator.rb
79
+ - lib/generators/resty/setup/templates/Mavenfile
80
+ - lib/generators/resty/setup/templates/module.gwt.xml
81
+ - lib/generators/resty/setup/templates/EntryPoint.java
82
+ - lib/generators/resty/setup/templates/initializer.rb
83
+ - lib/resty/child_path.rb
84
+ - lib/resty/resty_railtie.rb
85
+ has_rdoc: true
86
+ homepage: http://github.com/mkristian/ixtlan-guard
87
+ licenses:
88
+ - MIT-LICENSE
89
+ post_install_message:
90
+ rdoc_options:
91
+ - --main
92
+ - README.textile
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: "0"
107
+ requirements: []
108
+
109
+ rubyforge_project:
110
+ rubygems_version: 1.5.1
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: guard your controller actions
114
+ test_files: []
115
+