iruby 0.2.8 → 0.2.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES +17 -0
- data/README.md +29 -9
- data/iruby.gemspec +1 -0
- data/lib/iruby.rb +1 -0
- data/lib/iruby/backend.rb +5 -0
- data/lib/iruby/display.rb +4 -0
- data/lib/iruby/formatter.rb +2 -1
- data/lib/iruby/input.rb +41 -0
- data/lib/iruby/input/README.ipynb +450 -0
- data/lib/iruby/input/autoload.rb +24 -0
- data/lib/iruby/input/builder.rb +67 -0
- data/lib/iruby/input/button.rb +47 -0
- data/lib/iruby/input/cancel.rb +32 -0
- data/lib/iruby/input/checkbox.rb +56 -0
- data/lib/iruby/input/date.rb +26 -0
- data/lib/iruby/input/field.rb +30 -0
- data/lib/iruby/input/file.rb +57 -0
- data/lib/iruby/input/form.rb +77 -0
- data/lib/iruby/input/label.rb +21 -0
- data/lib/iruby/input/popup.rb +38 -0
- data/lib/iruby/input/radio.rb +47 -0
- data/lib/iruby/input/select.rb +45 -0
- data/lib/iruby/input/textarea.rb +22 -0
- data/lib/iruby/input/widget.rb +34 -0
- data/lib/iruby/session/ffi_rzmq.rb +14 -1
- data/lib/iruby/session/rbczmq.rb +11 -1
- data/lib/iruby/version.rb +1 -1
- metadata +35 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: edce35917d38e538781f2ab3827063693e6c6417
|
4
|
+
data.tar.gz: 9048d037809e1c69ff223c9363a2e743a5466931
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 319cc7a36eadd9c88ee9aad9cf9a3ae845d32d17efc8c00ec228ae89988b1273ffb2e26e0721ff3337454cd7e7d27ca36d9885fef05b4d9198d14268c5b1785b
|
7
|
+
data.tar.gz: c0ff35cf5547c57343919a427041ed4fb297f3947f26b795252522bf85d7850f7ad44a23c73807606394a9078a68503f3bbaf36074bade8ced1e4b6da22aae91
|
data/CHANGES
CHANGED
@@ -1,3 +1,20 @@
|
|
1
|
+
0.2.9 (2016-05-02)
|
2
|
+
|
3
|
+
Bug Fixes:
|
4
|
+
* Fix an error where a NoMethodError was being raised where a table rendered using an Array of Hashes has more than `maxcols` columns. (@CGamesPlay)
|
5
|
+
* Patch PryBackend to throw unterminated string and unexpected end-of-file syntax errors (@kylekyle)
|
6
|
+
|
7
|
+
Enhnacements:
|
8
|
+
* Add an IRuby::Input class which provides widgets for getting inputs from users. (@kylekyle)
|
9
|
+
* Add data_uri dependency (@kylekyle)
|
10
|
+
* Added a clear_output display function (@mrkn)
|
11
|
+
* Doc fixes for installation (@kozo2, @generall)
|
12
|
+
|
13
|
+
0.2.8 (2015-12-06)
|
14
|
+
|
15
|
+
* Add compatibility with ffi-rzmq
|
16
|
+
* Windows support
|
17
|
+
|
1
18
|
0.2.7 (2015-07-02)
|
2
19
|
|
3
20
|
* Fix problem with autoloaded constants in Display, problem with sciruby gem
|
data/README.md
CHANGED
@@ -29,11 +29,13 @@ Now you can run iruby with:
|
|
29
29
|
#### Windows
|
30
30
|
At first install IPython/Jupyter. I recommend an installation using [Enthought Canopy](https://www.enthought.com/).
|
31
31
|
|
32
|
-
|
32
|
+
Please update your PATH to include build tools or download the DevKit from http://rubyinstaller.org/downloads and follow [this instructions](http://github.com/oneclick/rubyinstaller/wiki/Development-Kit).
|
33
33
|
|
34
|
-
|
34
|
+
After that install ZeroMQ Legacy Stable Release 3.2.4 [x86 build for Windows XP SP3 and newer](http://miru.hk/archive/ZeroMQ-3.2.4~miru1.0-x86.exe).
|
35
35
|
|
36
|
-
|
36
|
+
Rename `libzmq-v100-mt-3_2_4.dll` to `libzmq.dll`.
|
37
|
+
|
38
|
+
Add the dll path to the PATH system variable.
|
37
39
|
|
38
40
|
Run two commands below:
|
39
41
|
|
@@ -46,14 +48,12 @@ Now you can run iruby with:
|
|
46
48
|
|
47
49
|
#### Mac
|
48
50
|
I recommend an installation using [Anaconda](https://store.continuum.io/cshop/anaconda/).
|
49
|
-
I have not checked the installation to MacOS X, but four lines below were necessary in v0.1.x.
|
50
51
|
|
51
|
-
|
52
|
-
brew install zeromq
|
53
|
-
gem install ffi-rzmq
|
54
|
-
gem install iruby
|
52
|
+
After that, run three commands shown below.
|
55
53
|
|
56
|
-
|
54
|
+
brew install libtool autoconf automake autogen
|
55
|
+
gem install rbczmq
|
56
|
+
gem install iruby
|
57
57
|
|
58
58
|
#### FreeBSD
|
59
59
|
|
@@ -93,6 +93,26 @@ Then, install iruby and related ports and gems.
|
|
93
93
|
$ gem build iruby.gemspec
|
94
94
|
$ sudo gem install iruby-0.2.7.gem
|
95
95
|
```
|
96
|
+
### Installation for jRuby
|
97
|
+
|
98
|
+
Since jRuby is fully compatible with Ruby version 2.2, it is possible to use iruby with jRuby.
|
99
|
+
It can be helpful if you want to use java classes in your iruby notebook.
|
100
|
+
This will require the following software:
|
101
|
+
* jRuby version >= 9.0.4.0
|
102
|
+
* ZeroMQ version >= 4.1.4 (You may have to build it from source.)
|
103
|
+
* ffi-rzmq gem
|
104
|
+
* this iruby gem
|
105
|
+
|
106
|
+
After installation, make sure that your `env` is set up to jruby.
|
107
|
+
```shell
|
108
|
+
$ env ruby -v
|
109
|
+
```
|
110
|
+
If you use RVM, it is enough to switch the current version to jruby.
|
111
|
+
If you have already used iruby with a different version, you need to generate a new kernel:
|
112
|
+
```shell
|
113
|
+
$ iruby register --force
|
114
|
+
```
|
115
|
+
After that you can use iruby with jRuby in usual way.
|
96
116
|
|
97
117
|
### After the installation
|
98
118
|
|
data/iruby.gemspec
CHANGED
data/lib/iruby.rb
CHANGED
data/lib/iruby/backend.rb
CHANGED
@@ -66,6 +66,11 @@ module IRuby
|
|
66
66
|
reset
|
67
67
|
raise SystemExit
|
68
68
|
end
|
69
|
+
unless @pry.eval_string.empty?
|
70
|
+
syntax_error = @pry.eval_string
|
71
|
+
@pry.reset_eval_string
|
72
|
+
@pry.evaluate_ruby syntax_error
|
73
|
+
end
|
69
74
|
raise @pry.last_exception if @pry.last_result_is_exception?
|
70
75
|
@pry.push_initial_binding unless @pry.current_binding # ensure that we have a binding
|
71
76
|
@pry.last_result
|
data/lib/iruby/display.rb
CHANGED
data/lib/iruby/formatter.rb
CHANGED
data/lib/iruby/input.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module IRuby
|
2
|
+
module Input
|
3
|
+
# autoload so that erector is not a required
|
4
|
+
# runtime dependency of IRuby
|
5
|
+
autoload :Builder, 'iruby/input/autoload'
|
6
|
+
|
7
|
+
def input prompt='Input'
|
8
|
+
result = form{input label: prompt}
|
9
|
+
result[:input] unless result.nil?
|
10
|
+
end
|
11
|
+
|
12
|
+
def password prompt='Password'
|
13
|
+
result = form{password label: prompt}
|
14
|
+
result[:password] unless result.nil?
|
15
|
+
end
|
16
|
+
|
17
|
+
def form &block
|
18
|
+
builder = Builder.new &block
|
19
|
+
form = InputForm.new(
|
20
|
+
fields: builder.fields,
|
21
|
+
buttons: builder.buttons
|
22
|
+
)
|
23
|
+
form.widget_display
|
24
|
+
builder.process_result form.submit
|
25
|
+
end
|
26
|
+
|
27
|
+
def popup title='Input', &block
|
28
|
+
builder = Builder.new &block
|
29
|
+
form = InputForm.new fields: builder.fields
|
30
|
+
popup = Popup.new(
|
31
|
+
title: title,
|
32
|
+
form: form,
|
33
|
+
buttons: builder.buttons
|
34
|
+
)
|
35
|
+
popup.widget_display
|
36
|
+
builder.process_result form.submit
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
extend Input
|
41
|
+
end
|
@@ -0,0 +1,450 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "markdown",
|
5
|
+
"metadata": {},
|
6
|
+
"source": [
|
7
|
+
"# IRuby Input\n",
|
8
|
+
"\n",
|
9
|
+
"The `IRuby::Input` class makes it easier for IRuby users to get input from users. For example:"
|
10
|
+
]
|
11
|
+
},
|
12
|
+
{
|
13
|
+
"cell_type": "code",
|
14
|
+
"execution_count": null,
|
15
|
+
"metadata": {
|
16
|
+
"collapsed": false
|
17
|
+
},
|
18
|
+
"outputs": [],
|
19
|
+
"source": [
|
20
|
+
"name = IRuby.input 'Enter your name'"
|
21
|
+
]
|
22
|
+
},
|
23
|
+
{
|
24
|
+
"cell_type": "markdown",
|
25
|
+
"metadata": {},
|
26
|
+
"source": [
|
27
|
+
"The following input methods are supported on the `IRuby` module:\n",
|
28
|
+
"\n",
|
29
|
+
"| method | description |\n",
|
30
|
+
"| -------- | -------- |\n",
|
31
|
+
"| `input(prompt)` | Prompts the user for a line of input |\n",
|
32
|
+
"| `password(prompt)` | Prompts the user for a password |\n",
|
33
|
+
"| `form(&block)` | Presents a form to the user |\n",
|
34
|
+
"| `popup(title,&block)` | Displays a form to the user as a popup |"
|
35
|
+
]
|
36
|
+
},
|
37
|
+
{
|
38
|
+
"cell_type": "markdown",
|
39
|
+
"metadata": {},
|
40
|
+
"source": [
|
41
|
+
"## Forms\n",
|
42
|
+
"\n",
|
43
|
+
"Forms are groups of inputs to be collected from the user. For example:"
|
44
|
+
]
|
45
|
+
},
|
46
|
+
{
|
47
|
+
"cell_type": "code",
|
48
|
+
"execution_count": null,
|
49
|
+
"metadata": {
|
50
|
+
"collapsed": false
|
51
|
+
},
|
52
|
+
"outputs": [],
|
53
|
+
"source": [
|
54
|
+
"result = IRuby.form do \n",
|
55
|
+
" input :username\n",
|
56
|
+
" password :password\n",
|
57
|
+
" button\n",
|
58
|
+
"end"
|
59
|
+
]
|
60
|
+
},
|
61
|
+
{
|
62
|
+
"cell_type": "markdown",
|
63
|
+
"metadata": {},
|
64
|
+
"source": [
|
65
|
+
"The following methods are available to build forms: \n",
|
66
|
+
"\n",
|
67
|
+
"| method | description |\n",
|
68
|
+
"| -------- | -------- |\n",
|
69
|
+
"| `input(key=:input)` | Prompts the user for a line of input |\n",
|
70
|
+
"| `textarea(key=:textarea),` | Adds a textarea to the form |\n",
|
71
|
+
"| `password(key=:password)` | Prompts the user for a password |\n",
|
72
|
+
"| `button(key=:done, color: :blue)` | Adds a submit button to the form |\n",
|
73
|
+
"| `cancel(prompt='Cancel')` | Adds a cancel button to the form |\n",
|
74
|
+
"| `text(string)` | Adds text to the form |\n",
|
75
|
+
"| `html(&block)` | Inserts HTML from the given [erector block](https://github.com/erector/erector) |\n",
|
76
|
+
"| `file(key=:file)` | Adds a file input to the form |\n",
|
77
|
+
"| `date(key=:date)` | Adds a date picker to the form |\n",
|
78
|
+
"| `select(*options)` | Adds a dropdown select input to the form |\n",
|
79
|
+
"| `radio(*options)` | Adds a radio select input to the form |\n",
|
80
|
+
"| `checkbox(*options)` | Adds checkbox inputs to the form |"
|
81
|
+
]
|
82
|
+
},
|
83
|
+
{
|
84
|
+
"cell_type": "markdown",
|
85
|
+
"metadata": {},
|
86
|
+
"source": [
|
87
|
+
"## Popups\n",
|
88
|
+
" \n",
|
89
|
+
"Popups are just forms in a bootstrap modal. They are useful when users **Run All** in a notebook with a lot of inputs. The popups always appear in the same spot, so users don't have to scroll down to find the next input. \n",
|
90
|
+
"\n",
|
91
|
+
"Popups accept a `title` argument, for example: "
|
92
|
+
]
|
93
|
+
},
|
94
|
+
{
|
95
|
+
"cell_type": "code",
|
96
|
+
"execution_count": null,
|
97
|
+
"metadata": {
|
98
|
+
"collapsed": false
|
99
|
+
},
|
100
|
+
"outputs": [],
|
101
|
+
"source": [
|
102
|
+
"result = IRuby.popup 'Please enter your name' do \n",
|
103
|
+
" input\n",
|
104
|
+
" button\n",
|
105
|
+
"end"
|
106
|
+
]
|
107
|
+
},
|
108
|
+
{
|
109
|
+
"cell_type": "markdown",
|
110
|
+
"metadata": {},
|
111
|
+
"source": [
|
112
|
+
"## Submit and cancel\n",
|
113
|
+
"\n",
|
114
|
+
"The enter key will submit an input or form and the escape key will cancel it. Canceled inputs are returned as `nil`. Inputs are automatically canceled if destroyed. An input can be destroyed by clearing its cell's output. The `cancel` button will cancel a form and all other buttons will submit it. \n",
|
115
|
+
"\n",
|
116
|
+
"After a form destroyed, the cell's output is cleared. Be careful not to prompt for input in a block that has previous output you would like to keep. Output is cleared to prevent forms from interferring with one another and to ensure that inputs are not inadvertently saved to the notebook. "
|
117
|
+
]
|
118
|
+
},
|
119
|
+
{
|
120
|
+
"cell_type": "code",
|
121
|
+
"execution_count": null,
|
122
|
+
"metadata": {
|
123
|
+
"collapsed": false
|
124
|
+
},
|
125
|
+
"outputs": [],
|
126
|
+
"source": [
|
127
|
+
"result = IRuby.popup 'Confirm' do \n",
|
128
|
+
" text 'Are you sure you want to continue?'\n",
|
129
|
+
" cancel 'No'\n",
|
130
|
+
" button 'Yes'\n",
|
131
|
+
"end"
|
132
|
+
]
|
133
|
+
},
|
134
|
+
{
|
135
|
+
"cell_type": "markdown",
|
136
|
+
"metadata": {},
|
137
|
+
"source": [
|
138
|
+
"## Custom keys\n",
|
139
|
+
"\n",
|
140
|
+
"Every widget has an entry in the final results hash. A custom key can be passed as the first parameter to the hash. If no key is provided, the widget name is used as the key. The `cancel` widget has no key; it's first parameter is its label. "
|
141
|
+
]
|
142
|
+
},
|
143
|
+
{
|
144
|
+
"cell_type": "code",
|
145
|
+
"execution_count": null,
|
146
|
+
"metadata": {
|
147
|
+
"collapsed": false
|
148
|
+
},
|
149
|
+
"outputs": [],
|
150
|
+
"source": [
|
151
|
+
"result = IRuby.form do\n",
|
152
|
+
" input :username\n",
|
153
|
+
" password :password\n",
|
154
|
+
"end"
|
155
|
+
]
|
156
|
+
},
|
157
|
+
{
|
158
|
+
"cell_type": "markdown",
|
159
|
+
"metadata": {},
|
160
|
+
"source": [
|
161
|
+
"## Custom labels\n",
|
162
|
+
"\n",
|
163
|
+
"Field labels appear to the left of the field. Button labels appear as the text on the button. `cancel` labels are passed as the first argument. All other widgets' labels are set using the `label` parameter. "
|
164
|
+
]
|
165
|
+
},
|
166
|
+
{
|
167
|
+
"cell_type": "code",
|
168
|
+
"execution_count": null,
|
169
|
+
"metadata": {
|
170
|
+
"collapsed": false
|
171
|
+
},
|
172
|
+
"outputs": [],
|
173
|
+
"source": [
|
174
|
+
"result = IRuby.form do \n",
|
175
|
+
" input :name, label: 'Please enter your name'\n",
|
176
|
+
" cancel 'None of your business!'\n",
|
177
|
+
" button :submit, label: 'All done'\n",
|
178
|
+
"end"
|
179
|
+
]
|
180
|
+
},
|
181
|
+
{
|
182
|
+
"cell_type": "markdown",
|
183
|
+
"metadata": {},
|
184
|
+
"source": [
|
185
|
+
"## Buttons\n",
|
186
|
+
"\n",
|
187
|
+
"Buttons do not appear in the final hash unless they are clicked. If clicked, their value is `true`. Here are the various colors a button can be:"
|
188
|
+
]
|
189
|
+
},
|
190
|
+
{
|
191
|
+
"cell_type": "code",
|
192
|
+
"execution_count": null,
|
193
|
+
"metadata": {
|
194
|
+
"collapsed": false
|
195
|
+
},
|
196
|
+
"outputs": [],
|
197
|
+
"source": [
|
198
|
+
"result = IRuby.form do \n",
|
199
|
+
" IRuby::Input::Button::COLORS.each_key do |color|\n",
|
200
|
+
" button color, color: color\n",
|
201
|
+
" end\n",
|
202
|
+
"end"
|
203
|
+
]
|
204
|
+
},
|
205
|
+
{
|
206
|
+
"cell_type": "markdown",
|
207
|
+
"metadata": {},
|
208
|
+
"source": [
|
209
|
+
"## Textareas\n",
|
210
|
+
"\n",
|
211
|
+
"Textareas are multiline inputs that are convenient for larger inputs. If you need a line return when typing in a textarea, use shift+enter since enter will submit the form."
|
212
|
+
]
|
213
|
+
},
|
214
|
+
{
|
215
|
+
"cell_type": "code",
|
216
|
+
"execution_count": null,
|
217
|
+
"metadata": {
|
218
|
+
"collapsed": false
|
219
|
+
},
|
220
|
+
"outputs": [],
|
221
|
+
"source": [
|
222
|
+
"result = IRuby.form do \n",
|
223
|
+
" text 'Enter email addresses, one per line (use shift+enter for newlines)'\n",
|
224
|
+
" textarea :emails\n",
|
225
|
+
"end"
|
226
|
+
]
|
227
|
+
},
|
228
|
+
{
|
229
|
+
"cell_type": "markdown",
|
230
|
+
"metadata": {},
|
231
|
+
"source": [
|
232
|
+
"## Text and HTML\n",
|
233
|
+
"\n",
|
234
|
+
"You can insert lines of text or custom html using their respective helpers:"
|
235
|
+
]
|
236
|
+
},
|
237
|
+
{
|
238
|
+
"cell_type": "code",
|
239
|
+
"execution_count": null,
|
240
|
+
"metadata": {
|
241
|
+
"collapsed": false
|
242
|
+
},
|
243
|
+
"outputs": [],
|
244
|
+
"source": [
|
245
|
+
"result = IRuby.form do \n",
|
246
|
+
" html { h1 'Choose a Stooge' }\n",
|
247
|
+
" text 'Choose your favorite stooge'\n",
|
248
|
+
" select :stooge, 'Moe', 'Larry', 'Curly'\n",
|
249
|
+
" button\n",
|
250
|
+
"end"
|
251
|
+
]
|
252
|
+
},
|
253
|
+
{
|
254
|
+
"cell_type": "markdown",
|
255
|
+
"metadata": {},
|
256
|
+
"source": [
|
257
|
+
"## Dropdowns\n",
|
258
|
+
"\n",
|
259
|
+
"Selects are dropdowns of options. By default, the first supplied option is selected. If you don't want any default value, use `nil` as the first parameter. "
|
260
|
+
]
|
261
|
+
},
|
262
|
+
{
|
263
|
+
"cell_type": "code",
|
264
|
+
"execution_count": null,
|
265
|
+
"metadata": {
|
266
|
+
"collapsed": false
|
267
|
+
},
|
268
|
+
"outputs": [],
|
269
|
+
"source": [
|
270
|
+
"result = IRuby.form do \n",
|
271
|
+
" text 'Do you have a favorite Stooge?' \n",
|
272
|
+
" select :stooge, nil, 'Moe', 'Larry', 'Curly'\n",
|
273
|
+
"end"
|
274
|
+
]
|
275
|
+
},
|
276
|
+
{
|
277
|
+
"cell_type": "markdown",
|
278
|
+
"metadata": {},
|
279
|
+
"source": [
|
280
|
+
"## Radio selects and checkboxes\n",
|
281
|
+
"\n",
|
282
|
+
"Like selects, radio selects and checkboxes take multiple arguments, each one an option. If the first argument is a symbol, it is used as the key. \n",
|
283
|
+
"\n",
|
284
|
+
"Note that the `checkbox` widget will always return `nil` or an array. "
|
285
|
+
]
|
286
|
+
},
|
287
|
+
{
|
288
|
+
"cell_type": "code",
|
289
|
+
"execution_count": null,
|
290
|
+
"metadata": {
|
291
|
+
"collapsed": false
|
292
|
+
},
|
293
|
+
"outputs": [],
|
294
|
+
"source": [
|
295
|
+
"result = IRuby.form do \n",
|
296
|
+
" radio :children, *(0..12), label: 'How many children do you have?'\n",
|
297
|
+
" checkbox :gender, 'Male', 'Female', 'Intersex', label: 'Select the genders of your children'\n",
|
298
|
+
" button\n",
|
299
|
+
"end"
|
300
|
+
]
|
301
|
+
},
|
302
|
+
{
|
303
|
+
"cell_type": "markdown",
|
304
|
+
"metadata": {},
|
305
|
+
"source": [
|
306
|
+
"## Files\n",
|
307
|
+
"\n",
|
308
|
+
"Since file widgets capture the enter key, you should include a button when creating forms that contain only a file input:"
|
309
|
+
]
|
310
|
+
},
|
311
|
+
{
|
312
|
+
"cell_type": "code",
|
313
|
+
"execution_count": null,
|
314
|
+
"metadata": {
|
315
|
+
"collapsed": true
|
316
|
+
},
|
317
|
+
"outputs": [],
|
318
|
+
"source": [
|
319
|
+
"IRuby.form do \n",
|
320
|
+
" file :avatar, label: 'Choose an Avatar'\n",
|
321
|
+
" button :submit\n",
|
322
|
+
"end"
|
323
|
+
]
|
324
|
+
},
|
325
|
+
{
|
326
|
+
"cell_type": "markdown",
|
327
|
+
"metadata": {},
|
328
|
+
"source": [
|
329
|
+
"File widgets return a hash with three keys: \n",
|
330
|
+
"\n",
|
331
|
+
"* `data`: The contents of the file as a string\n",
|
332
|
+
"* `content_type`: The type of file, such as `text/plain` or `image/jpeg`\n",
|
333
|
+
"* `name`: The name of the uploaded file"
|
334
|
+
]
|
335
|
+
},
|
336
|
+
{
|
337
|
+
"cell_type": "markdown",
|
338
|
+
"metadata": {},
|
339
|
+
"source": [
|
340
|
+
"## Example\n",
|
341
|
+
"\n",
|
342
|
+
"Here is an example form that uses every built-in widget. "
|
343
|
+
]
|
344
|
+
},
|
345
|
+
{
|
346
|
+
"cell_type": "code",
|
347
|
+
"execution_count": null,
|
348
|
+
"metadata": {
|
349
|
+
"collapsed": false,
|
350
|
+
"scrolled": false
|
351
|
+
},
|
352
|
+
"outputs": [],
|
353
|
+
"source": [
|
354
|
+
"result = IRuby.form do \n",
|
355
|
+
" html { h1 'The Everything Form' }\n",
|
356
|
+
" text 'Marvel at the strange and varied inputs!'\n",
|
357
|
+
" date\n",
|
358
|
+
" file\n",
|
359
|
+
" input :username\n",
|
360
|
+
" password\n",
|
361
|
+
" textarea\n",
|
362
|
+
" radio *(1..10)\n",
|
363
|
+
" checkbox 'Fish', 'Cat', 'Dog', label: 'Animals'\n",
|
364
|
+
" select :color, *IRuby::Input::Button::COLORS.keys\n",
|
365
|
+
" cancel \n",
|
366
|
+
" button \n",
|
367
|
+
"end"
|
368
|
+
]
|
369
|
+
},
|
370
|
+
{
|
371
|
+
"cell_type": "markdown",
|
372
|
+
"metadata": {},
|
373
|
+
"source": [
|
374
|
+
"## Writing your own widget\n",
|
375
|
+
"\n",
|
376
|
+
"Most form methods are `IRuby::Input::Widget` instances. A `Widget` is an [`Erector::Widget`](https://github.com/erector/erector) with some additional helpers. Here is the `cancel` widget:\n",
|
377
|
+
"\n",
|
378
|
+
"```ruby\n",
|
379
|
+
"module IRuby\n",
|
380
|
+
" module Input\n",
|
381
|
+
" class Cancel < Widget\n",
|
382
|
+
" needs :label\n",
|
383
|
+
"\n",
|
384
|
+
" builder :cancel do |label='Cancel'|\n",
|
385
|
+
" add_button Cancel.new(label: label)\n",
|
386
|
+
" end\n",
|
387
|
+
"\n",
|
388
|
+
" def widget_css\n",
|
389
|
+
" \".iruby-cancel { margin-left: 5px; }\"\n",
|
390
|
+
" end\n",
|
391
|
+
"\n",
|
392
|
+
" def widget_js\n",
|
393
|
+
" <<-JS\n",
|
394
|
+
" $('.iruby-cancel').click(function(){\n",
|
395
|
+
" $('#iruby-form').remove();\n",
|
396
|
+
" });\n",
|
397
|
+
" JS\n",
|
398
|
+
" end\n",
|
399
|
+
"\n",
|
400
|
+
" def widget_html\n",
|
401
|
+
" button(\n",
|
402
|
+
" @label,\n",
|
403
|
+
" type: 'button', \n",
|
404
|
+
" :'data-dismiss' => 'modal',\n",
|
405
|
+
" class: \"btn btn-danger pull-right iruby-cancel\"\n",
|
406
|
+
" )\n",
|
407
|
+
" end\n",
|
408
|
+
" end\n",
|
409
|
+
" end\n",
|
410
|
+
"end\n",
|
411
|
+
"```\n",
|
412
|
+
"\n",
|
413
|
+
"The following methods are available for widgets to use or override: \n",
|
414
|
+
"\n",
|
415
|
+
"| method | description |\n",
|
416
|
+
"| -------- | -------- |\n",
|
417
|
+
"| `widget_js` | Returns the widget's Javascript |\n",
|
418
|
+
"| `widget_css` | Returns the widget's CSS |\n",
|
419
|
+
"| `widget_html` | Returns the widget's |\n",
|
420
|
+
"| `builder(method,&block)` | Class method to add form building helpers. |\n",
|
421
|
+
"\n",
|
422
|
+
"The following methods are available in the `builder` block:\n",
|
423
|
+
"\n",
|
424
|
+
"| method | description |\n",
|
425
|
+
"| -------- | -------- |\n",
|
426
|
+
"| `add_field(field)` | Adds a widget to the form's field area |\n",
|
427
|
+
"| `add_button(button)` | Adds a button to the form's button area |\n",
|
428
|
+
"| `process(key,&block)` | Register a custom processing block for the given key in the results hash |\n",
|
429
|
+
"| `unique_key(key)` | Returns a unique key for the given key. Use this to make sure that there are no key collisions in the final results hash. |\n",
|
430
|
+
"\n",
|
431
|
+
"A canceled form always returns `nil`. Otherwise, the form collects any element with a `data-iruby-key` and non-falsey `data-iruby-value` and passes those to the processor proc registered for the key. See the `File` widget for a more involved example of processing results."
|
432
|
+
]
|
433
|
+
}
|
434
|
+
],
|
435
|
+
"metadata": {
|
436
|
+
"kernelspec": {
|
437
|
+
"display_name": "Ruby 2.2.1",
|
438
|
+
"language": "ruby",
|
439
|
+
"name": "ruby"
|
440
|
+
},
|
441
|
+
"language_info": {
|
442
|
+
"file_extension": ".rb",
|
443
|
+
"mimetype": "application/x-ruby",
|
444
|
+
"name": "ruby",
|
445
|
+
"version": "2.2.3"
|
446
|
+
}
|
447
|
+
},
|
448
|
+
"nbformat": 4,
|
449
|
+
"nbformat_minor": 0
|
450
|
+
}
|