ruby_ext_direct 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/.gitignore +7 -0
  2. data/Gemfile +4 -0
  3. data/README +5 -0
  4. data/Rakefile +1 -0
  5. data/examples/API/Gemfile +29 -0
  6. data/examples/API/app/actions/api_action.rb +18 -0
  7. data/examples/API/app/actions/home_action.rb +11 -0
  8. data/examples/API/app/actions/router_action.rb +18 -0
  9. data/examples/API/application.rb +37 -0
  10. data/examples/API/config/database.yml +6 -0
  11. data/examples/API/config/routes.rb +6 -0
  12. data/examples/API/config.ru +25 -0
  13. data/examples/API/public/NamesApp/app/store/NamesStore.js +48 -0
  14. data/examples/API/public/NamesApp/app/view/namesWindow.js +21 -0
  15. data/examples/API/public/NamesApp/app/view/ui/namesWindow.js +58 -0
  16. data/examples/API/public/NamesApp/designer.html +19 -0
  17. data/examples/API/public/NamesApp/designer.js +32 -0
  18. data/examples/API/public/NamesApp/designer_includeOrder.txt +3 -0
  19. data/examples/API/public/namesApp.xds +200 -0
  20. data/examples/rack/config.ru +40 -0
  21. data/examples/rack/exposed_classes/countries.rb +10 -0
  22. data/examples/rack/exposed_classes/names.rb +82 -0
  23. data/examples/rack/html/.DS_Store +0 -0
  24. data/examples/rack/html/NamesApp/app/store/NamesStore.js +56 -0
  25. data/examples/rack/html/NamesApp/app/view/namesWindow.js +49 -0
  26. data/examples/rack/html/NamesApp/app/view/ui/namesWindow.js +92 -0
  27. data/examples/rack/html/NamesApp/designer.html +19 -0
  28. data/examples/rack/html/NamesApp/designer.js +32 -0
  29. data/examples/rack/html/NamesApp/designer_includeOrder.txt +3 -0
  30. data/examples/rack/html/names.xds +275 -0
  31. data/lib/ext_direct/api.rb +53 -0
  32. data/lib/ext_direct/router.rb +97 -0
  33. data/lib/ext_direct/version.rb +3 -0
  34. data/lib/ext_direct.rb +13 -0
  35. data/ruby_ext_direct.gemspec +24 -0
  36. data/test/test_ext_direct.rb +129 -0
  37. metadata +92 -0
@@ -0,0 +1,82 @@
1
+ require 'rubygems'
2
+ require 'yaml'
3
+
4
+ require 'pp'
5
+ class Names
6
+ def load
7
+ names ||= init
8
+ {'total' => names.size, 'data' => names}
9
+ end
10
+
11
+ def create(data)
12
+ highest_id = 0
13
+ names ||= init
14
+
15
+ names.each do |n|
16
+ highest_id = n['id'].to_i if n['id'].to_i == 0 || n['id'].to_i > highest_id
17
+ end
18
+
19
+ data.each do |d|
20
+ d['id'] = highest_id + 1
21
+ end
22
+
23
+ names += data
24
+
25
+ store(names)
26
+
27
+ {'success' => true, 'data' => data, 'message' => 'saved'}
28
+ end
29
+
30
+
31
+ def update(data)
32
+ names ||= init
33
+ new_names = []
34
+ highest_id = 0
35
+
36
+
37
+ data.each do |d|
38
+ updated = false
39
+ new_names = names.map do |n|
40
+ highest_id = n['id'] if n['id'].to_i == 0 || n['id'].to_i < highest_id
41
+ if n['id'].eql?(d['id'])
42
+ updated = true
43
+ d
44
+ else
45
+ n
46
+ end
47
+ end
48
+
49
+ unless updated
50
+ data['id'] = highest_id += 1
51
+ new_names += data
52
+ end
53
+ names = new_names
54
+ end
55
+
56
+ store(new_names)
57
+
58
+ {'success' => true, 'data' => data, 'message' => 'updated'}
59
+ end
60
+
61
+ private
62
+ def init
63
+ names = []
64
+ if File.exists?('data.yml')
65
+ names = YAML::load_file('data.yml')
66
+ else
67
+ names = [
68
+ {'id' => 1, 'country' => 'be', 'name' => 'Mehmet Celik', 'address' => 'Country road 8, Takemehome'},
69
+ {'id' => 2, 'country' => 'be', 'name' => 'Kris Wellens', 'address' => 'Country road 88, Takemehome'}
70
+ ]
71
+ store(names)
72
+ end
73
+
74
+ names
75
+ end
76
+
77
+ def store(names)
78
+ File.open('data.yml', 'w') do |f|
79
+ f.puts YAML::dump(names)
80
+ end
81
+ end
82
+ end
Binary file
@@ -0,0 +1,56 @@
1
+ /*
2
+ * File: app/store/NamesStore.js
3
+ * Date: Wed Sep 07 2011 15:18:03 GMT+0200 (CEST)
4
+ *
5
+ * This file was generated by Ext Designer version 1.2.0.
6
+ * http://www.sencha.com/products/designer/
7
+ *
8
+ * This file will be auto-generated each and everytime you export.
9
+ *
10
+ * Do NOT hand edit this file.
11
+ */
12
+
13
+ Ext.define('NamesApp.store.NamesStore', {
14
+ extend: 'Ext.data.Store',
15
+
16
+ constructor: function(cfg) {
17
+ var me = this;
18
+ cfg = cfg || {};
19
+ me.callParent([Ext.apply({
20
+ autoLoad: true,
21
+ storeId: 'NamesStore',
22
+ proxy: {
23
+ type: 'direct',
24
+ api: {
25
+ read: Names.load,
26
+ update: Names.update,
27
+ create: Names.create
28
+ },
29
+ reader: {
30
+ type: 'json',
31
+ idProperty: 'id',
32
+ messageProperty: 'message',
33
+ root: 'data'
34
+ }
35
+ },
36
+ fields: [
37
+ {
38
+ name: 'id',
39
+ type: 'int'
40
+ },
41
+ {
42
+ name: 'name',
43
+ type: 'string'
44
+ },
45
+ {
46
+ name: 'address',
47
+ type: 'string'
48
+ },
49
+ {
50
+ name: 'country',
51
+ type: 'string'
52
+ }
53
+ ]
54
+ }, cfg)]);
55
+ }
56
+ });
@@ -0,0 +1,49 @@
1
+ /*
2
+ * File: app/view/namesWindow.js
3
+ * Date: Tue Sep 06 2011 11:39:42 GMT+0200 (CEST)
4
+ *
5
+ * This file was generated by Ext Designer version 1.2.0.
6
+ * http://www.sencha.com/products/designer/
7
+ *
8
+ * This file will be generated the first time you export.
9
+ *
10
+ * You should implement event handling and custom methods in this
11
+ * class.
12
+ */
13
+
14
+ Ext.define('NamesApp.view.namesWindow', {
15
+ extend: 'NamesApp.view.ui.namesWindow',
16
+
17
+ initComponent: function() {
18
+ var me = this;
19
+ me.callParent(arguments);
20
+ },
21
+ initEvents: function(){
22
+ var me = this;
23
+
24
+ me.down('tool[type=plus]').on('click',function(){
25
+ me.addNewRow();
26
+ });
27
+
28
+ me.down('tool[type=refresh]').on('click', function(){
29
+ me.down('#namesGrid').getStore().load();
30
+ me.down('#namesGrid').getView().refresh();
31
+ });
32
+
33
+ me.down('tool[type=save]').on('click', function(){
34
+ me.down('#namesGrid').getStore().save();
35
+ me.down('#namesGrid').getView().refresh();
36
+ });
37
+
38
+ me.down('#namesGrid').on('edit', function(editor, e){
39
+ // result = editor.record.save();
40
+ editor.record.commit();
41
+ me.down('#namesGrid').getStore().save();
42
+ me.down('#namesGrid').getView().refresh();
43
+ });
44
+ },
45
+ addNewRow:function(){
46
+ var store = Ext.data.StoreManager.lookup('NamesStore');
47
+ store.insert(0,{});
48
+ }
49
+ });
@@ -0,0 +1,92 @@
1
+ /*
2
+ * File: app/view/ui/namesWindow.js
3
+ * Date: Wed Sep 07 2011 15:18:03 GMT+0200 (CEST)
4
+ *
5
+ * This file was generated by Ext Designer version 1.2.0.
6
+ * http://www.sencha.com/products/designer/
7
+ *
8
+ * This file will be auto-generated each and everytime you export.
9
+ *
10
+ * Do NOT hand edit this file.
11
+ */
12
+
13
+ Ext.define('NamesApp.view.ui.namesWindow', {
14
+ extend: 'Ext.window.Window',
15
+
16
+ height: 541,
17
+ id: 'namesWindow',
18
+ width: 797,
19
+ layout: {
20
+ type: 'fit'
21
+ },
22
+ title: 'All Names',
23
+
24
+ initComponent: function() {
25
+ var me = this;
26
+ me.items = [
27
+ {
28
+ xtype: 'gridpanel',
29
+ id: 'namesGrid',
30
+ title: 'Names Grid',
31
+ store: 'NamesStore',
32
+ viewConfig: {
33
+
34
+ },
35
+ columns: [
36
+ {
37
+ xtype: 'gridcolumn',
38
+ width: 272,
39
+ dataIndex: 'name',
40
+ editor: {
41
+ xtype: 'textfield',
42
+ allowBlank: false
43
+ },
44
+ text: 'Name'
45
+ },
46
+ {
47
+ xtype: 'gridcolumn',
48
+ width: 393,
49
+ dataIndex: 'address',
50
+ editor: {
51
+ xtype: 'textfield',
52
+ allowBlank: false
53
+ },
54
+ text: 'Address'
55
+ },
56
+ {
57
+ xtype: 'gridcolumn',
58
+ dataIndex: 'country',
59
+ editor: {
60
+ xtype: 'textfield',
61
+ allowBlank: false
62
+ },
63
+ text: 'Country'
64
+ }
65
+ ],
66
+ plugins: [
67
+ Ext.create('Ext.grid.plugin.RowEditing', {
68
+ clicksToEdit: 1
69
+ })
70
+ ]
71
+ }
72
+ ];
73
+ me.tools = [
74
+ {
75
+ xtype: 'tool',
76
+ tooltip: 'Add row',
77
+ type: 'plus'
78
+ },
79
+ {
80
+ xtype: 'tool',
81
+ tooltip: 'Save',
82
+ type: 'save'
83
+ },
84
+ {
85
+ xtype: 'tool',
86
+ tooltip: 'Refresh',
87
+ type: 'refresh'
88
+ }
89
+ ];
90
+ me.callParent(arguments);
91
+ }
92
+ });
@@ -0,0 +1,19 @@
1
+ <!DOCTYPE html>
2
+
3
+ <!-- Auto Generated with Ext Designer -->
4
+ <!-- Modifications to this file will be overwritten. -->
5
+ <html>
6
+ <head>
7
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
+ <title>names.xds</title>
9
+ <link rel="stylesheet" type="text/css" href="http://extjs.cachefly.net/ext-4.0.2a/resources/css/ext-all.css"/>
10
+ <script type="text/javascript" src="http://extjs.cachefly.net/ext-4.0.2a/ext-all-debug.js"></script>
11
+ <script type="text/javascript">
12
+ Ext.ns("REMOTING_API");
13
+ REMOTING_API = {"descriptor":"REMOTING_API","url":"/router","type":"remoting","actions":{"Countries":[{"name":"countries","len":0},{"name":"get","len":0}],"Names":[{"name":"names","len":0},{"name":"load","len":0},{"name":"create","len":1},{"name":"update","len":1}]}};
14
+ Ext.Direct.addProvider(REMOTING_API);
15
+ </script>
16
+ <script type="text/javascript" src="designer.js"></script>
17
+ </head>
18
+ <body></body>
19
+ </html>
@@ -0,0 +1,32 @@
1
+ /*
2
+ * File: designer.js
3
+ * Date: Wed Sep 07 2011 15:18:03 GMT+0200 (CEST)
4
+ *
5
+ * This file was generated by Ext Designer version 1.2.0.
6
+ * http://www.sencha.com/products/designer/
7
+ *
8
+ * This file will be auto-generated each and everytime you export.
9
+ *
10
+ * Do NOT hand edit this file.
11
+ */
12
+
13
+ Ext.Loader.setConfig({
14
+ enabled: true
15
+ });
16
+
17
+ Ext.application({
18
+ name: 'NamesApp',
19
+
20
+ stores: [
21
+ 'NamesStore'
22
+ ],
23
+
24
+ launch: function() {
25
+ Ext.QuickTips.init();
26
+
27
+ var cmp1 = Ext.create('NamesApp.view.namesWindow', {
28
+ renderTo: Ext.getBody()
29
+ });
30
+ cmp1.show();
31
+ }
32
+ });
@@ -0,0 +1,3 @@
1
+ <script type="text/javascript" src="app/view/ui/namesWindow.js"></script>
2
+ <script type="text/javascript" src="app/view/namesWindow.js"></script>
3
+ <script type="text/javascript" src="app/store/NamesStore.js"></script>
@@ -0,0 +1,275 @@
1
+ {
2
+ "name": "names.xds",
3
+ "settings": {
4
+ "urlPrefix": "http://localhost/",
5
+ "directAPI": "http://localhost:9292/api",
6
+ "spacesToIndent": "4",
7
+ "codeGenFormat": "Class",
8
+ "exportPath": "./NamesApp",
9
+ "extPath": "http://extjs.cachefly.net/ext-4.0.2a/",
10
+ "lineEnding": "LF",
11
+ "instantiateStore": true,
12
+ "exportXDSFiles": true,
13
+ "genTimestamps": true,
14
+ "appName": "NamesApp"
15
+ },
16
+ "xdsVersion": "1.2.0",
17
+ "components": [
18
+ {
19
+ "id": "ExtBox1-ext-gen2626",
20
+ "type": "window",
21
+ "reference": {
22
+ "name": "items",
23
+ "type": "array"
24
+ },
25
+ "codeClass": null,
26
+ "userConfig": {
27
+ "height": 541,
28
+ "id": "namesWindow",
29
+ "width": 797,
30
+ "layout": "fit",
31
+ "title": "All Names",
32
+ "designer|userClassName": "namesWindow"
33
+ },
34
+ "cn": [
35
+ {
36
+ "id": "ExtBox1-ext-gen2817",
37
+ "type": "gridpanel",
38
+ "reference": {
39
+ "name": "items",
40
+ "type": "array"
41
+ },
42
+ "codeClass": null,
43
+ "userConfig": {
44
+ "id": "namesGrid",
45
+ "title": "Names Grid",
46
+ "store": "NamesStore",
47
+ "designer|userClassName": "MyGridPanel"
48
+ },
49
+ "cn": [
50
+ {
51
+ "id": "ExtBox1-ext-gen2854",
52
+ "type": "gridview",
53
+ "reference": {
54
+ "name": "viewConfig",
55
+ "type": "object"
56
+ },
57
+ "codeClass": null,
58
+ "userConfig": {
59
+ "designer|userClassName": "MyGridView"
60
+ }
61
+ },
62
+ {
63
+ "id": "ExtBox1-ext-gen3732",
64
+ "type": "gridcolumn",
65
+ "reference": {
66
+ "name": "columns",
67
+ "type": "array"
68
+ },
69
+ "codeClass": null,
70
+ "userConfig": {
71
+ "width": 272,
72
+ "dataIndex": "name",
73
+ "editor": "{xtype: 'textfield',\nallowBlank:false}",
74
+ "text": "Name",
75
+ "designer|userClassName": "MyColumn"
76
+ }
77
+ },
78
+ {
79
+ "id": "ExtBox1-ext-gen3741",
80
+ "type": "gridcolumn",
81
+ "reference": {
82
+ "name": "columns",
83
+ "type": "array"
84
+ },
85
+ "codeClass": null,
86
+ "userConfig": {
87
+ "width": 393,
88
+ "dataIndex": "address",
89
+ "editor": "{xtype: 'textfield',\nallowBlank:false}",
90
+ "text": "Address",
91
+ "designer|userClassName": "MyColumn1"
92
+ }
93
+ },
94
+ {
95
+ "id": "ExtBox1-ext-gen3750",
96
+ "type": "gridcolumn",
97
+ "reference": {
98
+ "name": "columns",
99
+ "type": "array"
100
+ },
101
+ "codeClass": null,
102
+ "userConfig": {
103
+ "dataIndex": "country",
104
+ "editor": "{xtype: 'textfield',\nallowBlank:false}",
105
+ "text": "Country",
106
+ "designer|userClassName": "MyColumn2"
107
+ }
108
+ },
109
+ {
110
+ "id": "ExtBox1-ext-gen5035",
111
+ "type": "gridroweditingplugin",
112
+ "reference": {
113
+ "name": "plugins",
114
+ "type": "array"
115
+ },
116
+ "codeClass": "Ext.grid.plugin.RowEditing",
117
+ "userConfig": {
118
+ "clicksToEdit": 1,
119
+ "designer|userClassName": "MyRowEditingPlugin"
120
+ }
121
+ }
122
+ ]
123
+ },
124
+ {
125
+ "id": "ExtBox1-ext-gen6904",
126
+ "type": "tool",
127
+ "reference": {
128
+ "name": "tools",
129
+ "type": "array"
130
+ },
131
+ "codeClass": null,
132
+ "userConfig": {
133
+ "tooltip": "Add row",
134
+ "type": "plus",
135
+ "designer|userClassName": "MyTool2"
136
+ }
137
+ },
138
+ {
139
+ "id": "ExtBox1-ext-gen6486",
140
+ "type": "tool",
141
+ "reference": {
142
+ "name": "tools",
143
+ "type": "array"
144
+ },
145
+ "codeClass": null,
146
+ "userConfig": {
147
+ "tooltip": "Save",
148
+ "type": "save",
149
+ "designer|userClassName": "MyTool"
150
+ }
151
+ },
152
+ {
153
+ "id": "ExtBox1-ext-gen6611",
154
+ "type": "tool",
155
+ "reference": {
156
+ "name": "tools",
157
+ "type": "array"
158
+ },
159
+ "codeClass": null,
160
+ "userConfig": {
161
+ "tooltip": "Refresh",
162
+ "type": "refresh",
163
+ "designer|userClassName": "MyTool1"
164
+ }
165
+ }
166
+ ]
167
+ }
168
+ ],
169
+ "stores": [
170
+ {
171
+ "id": "ExtBox1-ext-gen1666",
172
+ "type": "directstore",
173
+ "reference": {
174
+ "name": "items",
175
+ "type": "array"
176
+ },
177
+ "codeClass": null,
178
+ "userConfig": {
179
+ "autoLoad": true,
180
+ "storeId": "NamesStore",
181
+ "designer|userClassName": "NamesStore"
182
+ },
183
+ "cn": [
184
+ {
185
+ "id": "ExtBox1-ext-gen1670",
186
+ "type": "directproxy",
187
+ "reference": {
188
+ "name": "proxy",
189
+ "type": "object"
190
+ },
191
+ "codeClass": null,
192
+ "userConfig": {
193
+ "api": "{read:Names.load,\nupdate:Names.update,\ncreate:Names.create}",
194
+ "directFn": null,
195
+ "designer|userClassName": "Direct Proxy"
196
+ },
197
+ "cn": [
198
+ {
199
+ "id": "ExtBox1-ext-gen1676",
200
+ "type": "jsonreader",
201
+ "reference": {
202
+ "name": "reader",
203
+ "type": "object"
204
+ },
205
+ "codeClass": null,
206
+ "userConfig": {
207
+ "idProperty": "id",
208
+ "messageProperty": "message",
209
+ "root": "data",
210
+ "designer|userClassName": "Json Reader"
211
+ }
212
+ }
213
+ ]
214
+ },
215
+ {
216
+ "id": "ExtBox1-ext-gen5836",
217
+ "type": "datafield",
218
+ "reference": {
219
+ "name": "fields",
220
+ "type": "array"
221
+ },
222
+ "codeClass": null,
223
+ "userConfig": {
224
+ "name": "id",
225
+ "type": "int",
226
+ "designer|userClassName": "MyField3"
227
+ }
228
+ },
229
+ {
230
+ "id": "ExtBox1-ext-gen2110",
231
+ "type": "datafield",
232
+ "reference": {
233
+ "name": "fields",
234
+ "type": "array"
235
+ },
236
+ "codeClass": null,
237
+ "userConfig": {
238
+ "name": "name",
239
+ "type": "string",
240
+ "designer|userClassName": "MyField"
241
+ }
242
+ },
243
+ {
244
+ "id": "ExtBox1-ext-gen2117",
245
+ "type": "datafield",
246
+ "reference": {
247
+ "name": "fields",
248
+ "type": "array"
249
+ },
250
+ "codeClass": null,
251
+ "userConfig": {
252
+ "name": "address",
253
+ "type": "string",
254
+ "designer|userClassName": "MyField1"
255
+ }
256
+ },
257
+ {
258
+ "id": "ExtBox1-ext-gen2124",
259
+ "type": "datafield",
260
+ "reference": {
261
+ "name": "fields",
262
+ "type": "array"
263
+ },
264
+ "codeClass": null,
265
+ "userConfig": {
266
+ "name": "country",
267
+ "type": "string",
268
+ "designer|userClassName": "MyField2"
269
+ }
270
+ }
271
+ ]
272
+ }
273
+ ],
274
+ "framework": "ext40"
275
+ }
@@ -0,0 +1,53 @@
1
+ require 'json'
2
+
3
+ module ExtDirect
4
+ class Api
5
+ #@exposed_api = {}
6
+ attr_reader :exposed_api
7
+
8
+ def self.expose(class_to_expose, options = {})
9
+ @exposed_api = {} if @exposed_api.nil?
10
+
11
+ methods = []
12
+
13
+ if options.include?(:only)
14
+ raw_methods = options[:only] || []
15
+ else
16
+ raw_methods = class_to_expose.instance_methods(false) - (options[:except] || [])
17
+ end
18
+
19
+ raw_methods.uniq!
20
+ raw_methods.each do |m|
21
+ name = m
22
+ len = class_to_expose.instance_method(m).parameters.size
23
+ methods << {:name => name, :len => len}
24
+ end
25
+
26
+ @exposed_api.store(class_to_expose.name, methods)
27
+ end
28
+
29
+ def self.expose_all(class_dir)
30
+ @exposed_api = {}
31
+
32
+ Dir.glob("#{class_dir}/**/*.rb").each do |r|
33
+ rr = r.split("#{class_dir}/")[1].gsub('.rb','')
34
+ puts "#{class_dir}/#{rr}"
35
+ require "#{class_dir}/#{rr}"
36
+
37
+ klass = self.class.const_get(rr.split('_').map{|c| c.capitalize}.join(''))
38
+ self.expose klass
39
+ end
40
+ end
41
+
42
+ def self.to_json
43
+ api = self.to_raw
44
+ "REMOTING_API = #{api.to_json};"
45
+ end
46
+
47
+ def self.to_raw
48
+ api = {:url => '/router',
49
+ :type => 'remoting',
50
+ :actions => @exposed_api}
51
+ end
52
+ end
53
+ end