aurita-gui 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,14 @@
1
+ === 0.2.0 / 2009-01-12
2
+
3
+ * Added magic:
4
+
5
+ HTML.div {
6
+ ul(:class => :list) {
7
+ li { 'first' }
8
+ li { 'second' }
9
+ }
10
+ }
11
+
1
12
  === 0.1.0 / 2009-01-07
2
13
 
3
14
  * First commit.
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+
2
+ - Implement <fieldset>
3
+ - Implement <optgroup>
4
+
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.6'
17
+ s.version = '0.2.0'
18
18
  s.author = 'Tobias Fuchs'
19
19
  s.email = 'fuchs@atomnode.net'
20
20
  s.date = Time.now
@@ -29,7 +29,7 @@ part of Aurita in a single gem with no dependencies.
29
29
 
30
30
  s.has_rdoc = true
31
31
  s.rdoc_options << '--title' << 'Aurita::GUI' <<
32
- '--main' << 'Aurita::GUI::HTML' <<
32
+ '--main' << './cheatsheet.rb' <<
33
33
  '--line-numbers'
34
34
 
35
35
  s.homepage = 'http://aurita.wortundform.de/'
data/cheatsheet.rb ADDED
@@ -0,0 +1,112 @@
1
+
2
+ require('rubygems')
3
+ require('aurita-gui')
4
+ require('test/unit/assertions')
5
+
6
+ include Test::Unit::Assertions
7
+
8
+ include Aurita::GUI
9
+
10
+ #== The basics
11
+
12
+ # This is the most convenient way to build
13
+ # HTML using aurita-gui.
14
+ # (Thanks to oGMo for demanding more magic)
15
+ t1 = HTML.build {
16
+ div(:class => :css_class,
17
+ :onmouseover => "do_something_with(this);") {
18
+ ul(:id => :the_list) {
19
+ li(:class => :first) { 'foo' } +
20
+ li(:class => :second) { 'bar' } +
21
+ li(:class => :third) { 'batz' }
22
+ }
23
+ }
24
+ }
25
+ puts t1.to_s
26
+ puts '-----------------------------------------'
27
+
28
+ # The previous example effectively does the
29
+ # following:
30
+ t2 = HTML.div(:class => :css_class,
31
+ :onmouseover => "do_something_with(this);") {
32
+ HTML.ul(:id => :the_list) {
33
+ [
34
+ HTML.li(:class => :first) { 'foo' },
35
+ HTML.li(:class => :second) { 'bar' },
36
+ HTML.li(:class => :third) { 'batz' }
37
+ ]
38
+ }
39
+ }
40
+ assert_equal(t1.to_s, t2.to_s)
41
+
42
+ # Element is not a full Enumerable implementation (yet),
43
+ # but it offers random access operators ...
44
+
45
+ assert_equal(t1[0].tag, :ul) # First element of div is <ul>
46
+
47
+ t1[0][1] = HTML.li(:class => :changed) { 'wombat' }
48
+ puts t1.to_s
49
+ puts '-----------------------------------------'
50
+
51
+ # ... as well as #each ...
52
+
53
+ t1[0].each { |element|
54
+ element.id = 'each_change'
55
+ }
56
+
57
+ puts t1.to_s
58
+
59
+ # ... empty? and length. More to come in future releases.
60
+
61
+ assert_equal(t1[0].length, 3) # List has 3 entries
62
+ assert_equal(t1[0].empty?, false) # List has 3 entries
63
+
64
+
65
+ #== Form generation
66
+
67
+ puts '-----------------------------------------'
68
+ form = Form.new(:name => :the_form,
69
+ :id => :the_form_id,
70
+ :action => :where_to_send)
71
+ # You can either set all attributes in the
72
+ # constructor call ...
73
+ text = Input_Field.new(:name => :description,
74
+ :class => :the_css_class,
75
+ :onfocus => "alert('input focussed');",
76
+ :value => 'some text')
77
+ # Or set them afterwards:
78
+ text.onblur = "alert('i lost focus :(');"
79
+
80
+ # Enable / disable:
81
+ text.disable!
82
+ text.enable!
83
+ puts text.to_s
84
+ puts '-----------------------------------------'
85
+
86
+ # Set an element to readonly mode (display value only):
87
+ text.readonly!
88
+ puts text.to_s
89
+ puts '-----------------------------------------'
90
+ # And back to editable mode:
91
+ text.editable!
92
+
93
+ # Add it to the form:
94
+ form.add(text)
95
+ puts form.to_s
96
+ puts '-----------------------------------------'
97
+
98
+ # Access it again, via name:
99
+ assert_equal(form[:description], text)
100
+ # Or by using its index:
101
+ assert_equal(form[0], text)
102
+
103
+ # This is useful!
104
+ form[:description].value = 'change value'
105
+
106
+ checkbox = Checkbox_Field.new(:name => :enable_me,
107
+ :value => :foo,
108
+ :label => 'Check me',
109
+ :options => [ :foo, :bar ] )
110
+ form.add(checkbox)
111
+ puts form.to_s
112
+
@@ -59,7 +59,8 @@ module GUI
59
59
  if block_given? then
60
60
  @content = yield
61
61
  else
62
- @content = params[:content].to_s
62
+ @content = params[:content]
63
+ @content = [ @content ] unless @content.kind_of? Array
63
64
  end
64
65
  params.delete(:content)
65
66
  @tag = params[:tag]
@@ -84,11 +85,17 @@ module GUI
84
85
  # Render this element to a string and append another
85
86
  # element.
86
87
  def +(other)
87
- return string << other.string if other.kind_of? Element
88
- return string << other
88
+ # return string << other.string if other.kind_of? Element
89
+ return [ self, other ]
89
90
  end
90
91
  alias << +
91
92
 
93
+ def to_ary
94
+ [ self ]
95
+ end
96
+ alias to_a to_ary
97
+
98
+
92
99
  # Redirect methods to setting or retreiving tag
93
100
  # attributes.
94
101
  def method_missing(meth, value=nil)
@@ -113,12 +120,20 @@ module GUI
113
120
  end
114
121
 
115
122
  # Do not redirect random access operators.
116
- def [](a)
117
- raise ::Exception.new('Undefined method [] for ' << self.class.to_s)
123
+ def [](index)
124
+ return @content[index]
125
+ # raise ::Exception.new('Undefined method [] for ' << self.class.to_s)
118
126
  end
119
127
  # Do not redirect random access operators.
120
- def []=(a,b)
121
- raise ::Exception.new('Undefined method []= for ' << self.class.to_s)
128
+ def []=(index,element)
129
+ @content[index] = element
130
+ # raise ::Exception.new('Undefined method []= for ' << self.class.to_s)
131
+ end
132
+ def length
133
+ @content.length
134
+ end
135
+ def empty?
136
+ @content.length == 0
122
137
  end
123
138
 
124
139
  # Static helper definition for clearing
@@ -153,6 +168,10 @@ module GUI
153
168
 
154
169
  end
155
170
  alias to_s string
171
+
172
+ def each(&block)
173
+ @content.each(&block)
174
+ end
156
175
 
157
176
  end # class
158
177
 
@@ -14,6 +14,9 @@ require('aurita-gui/form/textarea_field')
14
14
  require('aurita-gui/form/date_field')
15
15
  require('aurita-gui/form/datetime_field')
16
16
  require('aurita-gui/form/file_field')
17
+ require('aurita-gui/form/boolean_field')
18
+ require('aurita-gui/form/text_field')
19
+ require('aurita-gui/form/password_field')
17
20
 
18
21
  module Aurita
19
22
  module GUI
@@ -25,7 +28,8 @@ module GUI
25
28
  def initialize(field)
26
29
  label_params = { :for => field.dom_id }
27
30
  label_params[:id] = field.dom_id.to_s + '_label'
28
- @content = [ HTML.label(label_params) { field.label }, field ]
31
+ label = field.label
32
+ @content = [ HTML.label(label_params) { label }, field ]
29
33
  params = { :tag => :li,
30
34
  :content => @content,
31
35
  :id => field.dom_id + '_wrap',
@@ -71,6 +75,7 @@ module GUI
71
75
  @element_map = {}
72
76
  @values ||= {}
73
77
  @title = false
78
+ @custom_fields = false
74
79
  if block_given? then
75
80
  yield.each { |e| add(e) }
76
81
  end
@@ -139,6 +144,7 @@ module GUI
139
144
  # form.fields = [ :name, :description, :date ]
140
145
  #
141
146
  def fields=(attrib_array)
147
+ @custom_fields = true
142
148
  @fields = attrib_array.flatten.collect { |fieldname| fieldname.to_s }
143
149
  @elements.each { |field|
144
150
  @element_map[field.name.to_s] = field
@@ -167,7 +173,7 @@ module GUI
167
173
  # Return array of field names currently
168
174
  # available for rendering.
169
175
  def fields()
170
- if !@fields || @fields.length == 0 then
176
+ if !@custom_fields || @fields.length == 0 then
171
177
  @elements.each { |field|
172
178
  @fields << field.name.to_s
173
179
  @element_map[field.name.to_s] = field
@@ -197,14 +203,16 @@ module GUI
197
203
  end
198
204
  end
199
205
  }
200
- @content = HTML.ul(:class => :form_fields) { @content }
206
+ cont = @content
207
+ @content = HTML.ul(:class => :form_fields) { cont }
201
208
  return @content
202
209
  end
203
210
 
204
211
  # Render this form to an HTML.form instance.
205
212
  # Wraps result of #content.
206
213
  def element
207
- HTML.form(@attrib) { content() }
214
+ cont = content()
215
+ HTML.form(@attrib) { cont }
208
216
  end
209
217
 
210
218
  # Render this form to a string
@@ -4,19 +4,43 @@ require('aurita-gui/form/options_field')
4
4
  module Aurita
5
5
  module GUI
6
6
 
7
+ # Factory for checkbox input fields, specialization
8
+ # of Options_Field.
9
+ # For usage see documentation of Options_Fiels.
10
+ #
7
11
  class Checkbox_Field < Options_Field
8
12
  def option_elements
9
13
  options = []
10
14
  @option_elements.each { |option|
11
15
  if option.kind_of? Hash then
16
+ # Options have been set as value/label pair, like
17
+ #
18
+ # @options = { 1 => 'first', 2 => 'second' }
19
+ #
12
20
  option.each_pair { |k,v|
13
- options << HTML.input(:type => :checkbox, :value => k, :name => @attrib[:name] ) { v }
21
+ box_attribs = { :type => :checkbox,
22
+ :value => k,
23
+ :name => @attrib[:name] }
24
+ box_attribs[:checked] = true if (v.to_s == @value.to_s)
25
+ options << HTML.input(box_attribs)
14
26
  }
15
27
  elsif option.kind_of? Element then
28
+ # Option element has been built externally, like in
29
+ #
30
+ # @options = [ HTML.option(:class => :custom, :label => 'built manually') ]
31
+ #
16
32
  option.name = @attrib[:name]
17
33
  option.tag = :input
18
34
  option.type = :checkbox
19
35
  options << option
36
+ else
37
+ # For @options = [ :foo, :bar, 'wombat' ],
38
+ #
39
+ box_attribs = { :type => :checkbox,
40
+ :value => option.to_s,
41
+ :name => @attrib[:name] }
42
+ box_attribs[:checked] = true if (option.to_s == @value.to_s)
43
+ options << HTML.input(box_attribs)
20
44
  end
21
45
  }
22
46
  options
@@ -4,10 +4,21 @@ require('aurita-gui/form/form_field')
4
4
  module Aurita
5
5
  module GUI
6
6
 
7
+ # Factory for input fields.
8
+ # Default type is :text, accoring to W3C, possible
9
+ # types are :password, :radio, :checkbox.
10
+ #
11
+ # For radio and checkbox input fields, there are
12
+ # classes Radio_Field and Checkbox_Field, providing
13
+ # more convenience than building option fields
14
+ # manually.
15
+ # See also Options_Field on this topic.
16
+ #
7
17
  # Usage:
8
18
  #
9
- # i = Input_Field.new(:name => :description,
10
- # :label => 'Description',
19
+ # i = Input_Field.new(:type => :text,
20
+ # :name => :description,
21
+ # :label => 'Enter a discription',
11
22
  # :value => 'Lorem ipsum dolor')
12
23
  #
13
24
  class Input_Field < Form_Field
@@ -0,0 +1,23 @@
1
+
2
+ require('aurita-gui/form/form_field')
3
+
4
+ module Aurita
5
+ module GUI
6
+
7
+ # Wrapper for Input_Field, defaults type to :password.
8
+ #
9
+ # Usage:
10
+ #
11
+ # i = Password_Field.new(:name => :pass_confirm,
12
+ # :label => 'Confirm password',
13
+ # :value => 'oldpass') # Possible, but not recommended
14
+ #
15
+ class Password_Field < Input_Field
16
+ def initialize(params, &block)
17
+ params[:type] = :password unless params[:type]
18
+ super(params)
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -4,8 +4,29 @@ require('aurita-gui/form/options_field')
4
4
  module Aurita
5
5
  module GUI
6
6
 
7
+ # Factory for radio input fields, specialization
8
+ # of Options_Field.
9
+ # Example:
10
+ #
11
+ # r = Radio_Field.new(:name => :color,
12
+ # :value => :red,
13
+ # :label => 'Select a color',
14
+ # :options => { :red => 'red color', :blue => 'blue color' })
15
+ #
16
+ # Same as
17
+ #
18
+ # r = Radio_Field.new(:name => :color, :value => :red, :label => 'Select a color') {
19
+ # :red => 'red color', :blue => 'blue_color
20
+ # }
21
+ #
22
+ # For usage details see documentation of Options_Fiels.
23
+ #
7
24
  class Radio_Field < Options_Field
8
25
 
26
+ # Returns array of option elements for this
27
+ # radio field.
28
+ # Each option is a radio input field with
29
+ # @name as common name attribute.
9
30
  def option_elements
10
31
  options = []
11
32
  @option_elements.each { |option|
@@ -23,6 +44,8 @@ module GUI
23
44
  options
24
45
  end
25
46
 
47
+ # Renders this radio field to a HTML.ul element,
48
+ # including options from #option_elements.
26
49
  def element
27
50
  HTML.ul(:class => :radio_options) {
28
51
  option_elements().map { |o| HTML.li() { o } }
@@ -0,0 +1,23 @@
1
+
2
+ require('aurita-gui/form/form_field')
3
+
4
+ module Aurita
5
+ module GUI
6
+
7
+ # Wrapper for Input_Field, sets type to :text.
8
+ #
9
+ # Usage:
10
+ #
11
+ # i = Text_Field.new(:name => :description,
12
+ # :label => 'Description',
13
+ # :value => 'Lorem ipsum dolor')
14
+ #
15
+ class Text_Field < Input_Field
16
+ def initialize(params, &block)
17
+ params[:type] = :text unless params[:type]
18
+ super(params)
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -29,30 +29,58 @@ module GUI
29
29
  # HTML.a(:href => 'http://domain.com') { 'click me' }
30
30
  # # --> '<a href="http://domain.com">click me</a>'
31
31
  #
32
+
33
+ XHTML_TAGS = [ :html, :div, :p, :input, :select, :option, :ul, :ol, :li ]
34
+
32
35
  class HTML
33
36
 
37
+ # Statically defined as <br /> must not have any attributes.
34
38
  def self.br
35
- '<br />'
39
+ Element.new(:tag => :br)
36
40
  end
37
41
 
38
42
  def self.render(meth_name, attrib_hash={}, &block)
39
43
  raise ::Exception.new('Missing attributes for HTML.' << meth_name.inspect) unless attrib_hash
40
44
  attrib_hash[:tag] = meth_name
41
- label = yield if block_given?
42
- label = label.to_s unless label.kind_of? String
43
- attrib_hash[:content] = label
45
+ cont = yield if block_given?
46
+ # cont = self.class_eval(&block)
47
+ attrib_hash[:content] = cont
44
48
  Element.new(attrib_hash)
45
49
  end
50
+
51
+ def self.build(&block)
52
+ raise ::Exception.new('Missing block for HTML.render') unless block_given?
53
+ self.class_eval(&block)
54
+ end
55
+
46
56
 
47
57
  def self.a(attrib_hash={}, &block)
48
58
  render(:a, attrib_hash, &block)
49
59
  end
50
60
 
61
+ # p is defined in Kernel, so we have to
62
+ # redirect it manually (method_missing won't be
63
+ # triggered for it)
64
+ def self.p(attrib_hash={}, &block)
65
+ render(:p, attrib_hash, &block)
66
+ end
67
+
51
68
  def self.method_missing(meth_name, attrib_hash={}, &block)
52
- render(meth_name, attrib_hash, &block)
69
+ render(meth_name, attrib_hash, &block)
53
70
  end
54
71
 
55
- end
72
+ =begin
73
+ for t in XHTML_TAGS do
74
+ meth = <<EOC
75
+ def self.#{t.to_s}(attribs=nil, &block)
76
+ render(:#{t.to_s}, attribs, &block)
77
+ end
78
+ EOC
79
+ class_eval(meth)
80
+ end
81
+ =end
82
+
83
+ end # class
56
84
 
57
85
  # A minimalistic generator for javascript calls.
58
86
  # Usage:
@@ -111,8 +139,9 @@ module GUI
111
139
  args_string = args.join(',')
112
140
  meth.to_s << '(' << args_string + ');'
113
141
  end
114
-
115
- end
142
+
143
+ end # class
144
+
116
145
 
117
146
  end # module
118
147
  end # module
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.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Fuchs
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-09 00:00:00 +01:00
12
+ date: 2009-01-12 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -22,13 +22,13 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
 
24
24
  files:
25
+ - cheatsheet.rb
25
26
  - History.txt
26
27
  - lib
27
- - aurita-gui-0.1.0.gem
28
28
  - aurita-gui.gemspec
29
29
  - bin
30
+ - TODO
30
31
  - test
31
- - aurita-gui-0.1.5.gem
32
32
  - LICENSE
33
33
  - lib/aurita-gui
34
34
  - lib/aurita-gui.rb
@@ -38,6 +38,7 @@ files:
38
38
  - lib/aurita-gui/table.rb
39
39
  - lib/aurita-gui/form
40
40
  - lib/aurita-gui/element.rb
41
+ - lib/aurita-gui/form/password_field.rb
41
42
  - lib/aurita-gui/form/options_field.rb
42
43
  - lib/aurita-gui/form/file_field.rb
43
44
  - lib/aurita-gui/form/form_error.rb
@@ -50,6 +51,7 @@ files:
50
51
  - lib/aurita-gui/form/select_field.rb
51
52
  - lib/aurita-gui/form/radio_field.rb
52
53
  - lib/aurita-gui/form/hidden_field.rb
54
+ - lib/aurita-gui/form/text_field.rb
53
55
  - lib/aurita-gui/form/input_field.rb
54
56
  - lib/aurita-gui/form/date_field.rb
55
57
  - test/tc_options.rb
@@ -62,7 +64,7 @@ rdoc_options:
62
64
  - --title
63
65
  - Aurita::GUI
64
66
  - --main
65
- - Aurita::GUI::HTML
67
+ - ./cheatsheet.rb
66
68
  - --line-numbers
67
69
  require_paths:
68
70
  - lib
data/aurita-gui-0.1.0.gem DELETED
Binary file
data/aurita-gui-0.1.5.gem DELETED
Binary file