aurita-gui 0.1.0 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/aurita-gui-0.1.0.gem CHANGED
Binary file
data/aurita-gui.gemspec CHANGED
@@ -14,7 +14,7 @@ as stand-alone library in any context (such as rails).
14
14
  As there seems to be a lack of ruby form generators, i decided to release this
15
15
  part of Aurita in a single gem with no dependencies.
16
16
  EOF
17
- s.version = '0.1.0'
17
+ s.version = '0.1.5'
18
18
  s.author = 'Tobias Fuchs'
19
19
  s.email = 'fuchs@atomnode.net'
20
20
  s.date = Time.now
@@ -0,0 +1,18 @@
1
+
2
+ require('aurita-gui/form/options_field')
3
+
4
+ module Aurita
5
+ module GUI
6
+
7
+ # Convenience configuration of Checkbox_Field.
8
+ # Renders one checkbox with :options => [1]
9
+ class Boolean_Field < Checkbox_Field
10
+ def initialize(params, &block)
11
+ params[:options] = [ 1 ]
12
+ super(params, &block)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+
@@ -26,6 +26,9 @@ module GUI
26
26
  def element
27
27
  HTML.input(@attrib)
28
28
  end
29
+ def readonly_element
30
+ element()
31
+ end
29
32
  end
30
33
 
31
34
  class Form < Element
@@ -46,34 +46,125 @@ module GUI
46
46
  # select[4] = { 5 => 'fifth' }
47
47
  #
48
48
  #
49
+ # == Setting option values and labels
50
+ #
51
+ # There are a zillion ways to set option values and labels.
52
+ # The following examples all use Select_Field, but this
53
+ # behaviour applies to all derivates of Options_Field.
54
+ #
55
+ # If there are no option labels set,
56
+ # option values will be displayed directly:
57
+ #
58
+ # s1 = Select_Field.new(:name => :test,
59
+ # :label => 'Priority',
60
+ # :options => (1..10)) # Option labels are 0..10
61
+ #
62
+ # Set option values and labels at once using a hash:
63
+ #
64
+ # s1 = Select_Field.new(:name => :test,
65
+ # :label => 'Pick one',
66
+ # :options => { 1 => 'eins', 2 => 'zwei', 3 => 'drei' })
67
+ #
68
+ # Set option values as array, labels as hash:
69
+ #
70
+ # s1 = Select_Field.new(:name => :test,
71
+ # :label => { 'Pick one' => [ 'foo', 'bar', 'wombat' ] },
72
+ # :options => [ 1,2,3 ])
73
+ # Same as
74
+ #
75
+ # s1 = Select_Field.new(:name => :test,
76
+ # :label => 'Pick one',
77
+ # :option_labels => [ 'foo', 'bar', 'wombat' ],
78
+ # :options => [ 1,2,3 ] )
79
+ # Ranges are ok, too:
80
+ #
81
+ # s1 = Select_Field.new(:name => :test,
82
+ # :label => 'Pick one',
83
+ # :option_labels => [ 'foo', 'bar', 'wombat' ],
84
+ # :options => (1..3))
85
+ #
86
+ # Change option labels using an array.
87
+ # Option labels will be assigned in order,
88
+ # so options[0] has label[0] etc.
89
+ #
90
+ # s1.option_labels = [ 'first', 'second', 'third' ]
91
+ #
92
+ # Change option labels using a hash.
93
+ # Compared to using an array, this is useful
94
+ # in case you don't know the order of option
95
+ # values.
96
+ #
97
+ # s1.option_labels = { 1 => 'ras', 2 => 'dwa', 3 => 'tri' }
98
+ #
99
+ # Overwriting the labels field does the same,
100
+ # but this way, you also can change the
101
+ # field label:
102
+ #
103
+ # s1.label = { 'Select one' => [ 'foo', 'bar', 'wombat' ] }
104
+ #
105
+ # Or
106
+ #
107
+ # s1.label = { 'Select one' => { 1 => 'foo', 2 => 'bar', 3 => 'wombat' } }
108
+ #
109
+ # Of yourse you can replace all option values
110
+ # and their labels at once by overwriting
111
+ # the options field:
112
+ #
113
+ # s1.label = 'Choose'
114
+ # s1.options = { 1 => :foo, 2 => :bar, 3 => :wombat }
49
115
  #
50
116
  class Options_Field < Form_Field
51
117
 
52
- attr_accessor :options, :options_range, :options_labels, :value
118
+ attr_accessor :options, :options_range, :option_labels, :value
53
119
 
54
120
  def initialize(params, &block)
55
- @options = params[:options]
56
- @options_range = params[:options_range]
57
- @options_labels = params[:options_labels]
58
- @options ||= {}
59
- @options_range ||= []
60
- @options_labels ||= []
61
- @option_elements = []
62
- @value = params[:value]
121
+ @options = params[:options]
122
+ @options = @options.to_a if @options.kind_of? Range
123
+ @options_range = params[:options_range]
124
+ @option_labels = params[:option_labels]
125
+ set_option_labels(params[:option_labels]) if params[:option_labels]
126
+ @option_labels ||= []
127
+ @options ||= {}
128
+ @options_range ||= []
129
+ @option_elements ||= []
130
+ @value = params[:value]
131
+
63
132
  if block_given? then
64
133
  yield.each { |option|
65
134
  add_option(option)
66
135
  }
67
- elsif params[:options] then
136
+ elsif params[:options] and !params[:option_labels] then
68
137
  add_option(params[:options])
69
138
  end
70
139
  params.delete(:options)
71
- params.delete(:value) # Value is determined via selected value
140
+ # Option fields don't have a value attribute themselves
141
+ params.delete(:value)
72
142
  params.delete(:options_range)
73
- params.delete(:options_labels)
143
+ params.delete(:option_labels)
74
144
  super(params)
75
145
  end
76
146
 
147
+ def option_labels=(labels)
148
+ @option_labels = labels
149
+ renamed_options = {}
150
+ if @options.kind_of? Hash then
151
+ index = 0
152
+ @options.each_pair { |value,label|
153
+ renamed_options[value] = labels[value]
154
+ index += 1
155
+ }
156
+ elsif @options.kind_of? Array then
157
+ index = 0
158
+ @options.each { |value|
159
+ renamed_options[value] = labels[index]
160
+ index += 1
161
+ }
162
+ end
163
+ @options = renamed_options
164
+ @option_elements = [ @options ]
165
+ end
166
+ alias set_option_labels option_labels=
167
+
77
168
  def add_option(option={})
78
169
  if option.kind_of? Array then
79
170
  @option_elements += option
@@ -28,6 +28,11 @@ module GUI
28
28
  option_elements()
29
29
  }
30
30
  end
31
+ def readonly_element
32
+ HTML.div(@attrib) {
33
+ @options[@value]
34
+ }
35
+ end
31
36
  end
32
37
 
33
38
  end
@@ -0,0 +1,59 @@
1
+
2
+ require('rubygems')
3
+ require('aurita-gui')
4
+
5
+ include Aurita::GUI
6
+
7
+ # If there are no option labels set,
8
+ # option values will be displayed directly:
9
+ s1 = Select_Field.new(:name => :test,
10
+ :label => 'Priority',
11
+ :options => (1..10))
12
+
13
+ # Set option values and labels at once using a hash:
14
+ s1 = Select_Field.new(:name => :test,
15
+ :label => 'Pick one',
16
+ :options => { 1 => 'eins', 2 => 'zwei', 3 => 'drei' })
17
+
18
+ # Set option values as array, labels as hash:
19
+ s1 = Select_Field.new(:name => :test,
20
+ :label => { 'Pick one' => [ 'foo', 'bar', 'wombat' ] },
21
+ :options => [ 1,2,3 ])
22
+ # Same as
23
+ puts '----------------------'
24
+ s1 = Select_Field.new(:name => :test,
25
+ :label => 'Pick one',
26
+ :option_labels => [ 'foo', 'bar', 'wombat' ] ,
27
+ :options => [ 1..3 ] )
28
+ puts '----------------------'
29
+ puts s1
30
+ # Ranges are ok, too:
31
+ s1 = Select_Field.new(:name => :test,
32
+ :label => 'Pick one',
33
+ :option_labels => [ 'foo', 'bar', 'wombat' ],
34
+ :options => (1..3) )
35
+
36
+ # Change option labels using an array.
37
+ # Option labels will be assigned in order,
38
+ # so options[0] has label[0] etc.
39
+ s1.option_labels = [ 'first', 'second', 'third' ]
40
+
41
+ # Change option labels using a hash.
42
+ # Compared to using an array, this is useful
43
+ # in case you don't know the order of option
44
+ # values.
45
+ s1.option_labels = { 1 => 'ras', 2 => 'dwa', 3 => 'tri' }
46
+
47
+ # Overwriting the labels field does the same,
48
+ # but this way, you also can change the
49
+ # field label:
50
+ s1.label = { 'Select one' => [ 'foo', 'bar', 'wombat' ] }
51
+ # Or
52
+ s1.label = { 'Select one' => { 1 => 'foo', 2 => 'bar', 3 => 'wombat' } }
53
+
54
+ # Of yourse you can replace all option values
55
+ # and their labels at once by overwriting
56
+ # the options field:
57
+ s1.label = 'Choose'
58
+ s1.options = { 1 => :foo, 2 => :bar, 3 => :wombat }
59
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aurita-gui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Fuchs
@@ -44,12 +44,14 @@ files:
44
44
  - lib/aurita-gui/form/form_field.rb
45
45
  - lib/aurita-gui/form/textarea_field.rb
46
46
  - lib/aurita-gui/form/datetime_field.rb
47
+ - lib/aurita-gui/form/boolean_field.rb
47
48
  - lib/aurita-gui/form/fieldset.rb
48
49
  - lib/aurita-gui/form/select_field.rb
49
50
  - lib/aurita-gui/form/radio_field.rb
50
51
  - lib/aurita-gui/form/hidden_field.rb
51
52
  - lib/aurita-gui/form/input_field.rb
52
53
  - lib/aurita-gui/form/date_field.rb
54
+ - test/tc_options.rb
53
55
  - test/tc_table.rb
54
56
  - test/tc_form.rb
55
57
  has_rdoc: true