aurita-gui 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ === 0.3.1 / 2009-01-15
2
+
3
+ * Added specs for Element and HTML.
4
+ * Polished rendering.
5
+
1
6
  === 0.2.0 / 2009-01-12
2
7
 
3
8
  * Added magic:
data/TODO CHANGED
@@ -2,6 +2,9 @@
2
2
  - Implement <fieldset>
3
3
  - Implement <optgroup>
4
4
 
5
+ - Implement element.sibling[:dom_id] --> element
6
+ - Implement XPath1.0 method (parent, sibling, descendant, following ...)
7
+
5
8
  - Implement field visibility using
6
9
  field.visible = true/false, wrapped in
7
10
  def form.fields=(fields)
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.3.0'
17
+ s.version = '0.3.1'
18
18
  s.author = 'Tobias Fuchs'
19
19
  s.email = 'fuchs@atomnode.net'
20
20
  s.date = Time.now
@@ -25,7 +25,7 @@ part of Aurita in a single gem with no dependencies.
25
25
  'lib/aurita-gui/*',
26
26
  'lib/aurita-gui/form/*',
27
27
  'bin/*',
28
- 'test/*'].to_a
28
+ 'spec/*'].to_a
29
29
 
30
30
  s.has_rdoc = true
31
31
  s.rdoc_options << '--title' << 'Aurita::GUI' <<
data/lib/aurita-gui.rb CHANGED
@@ -3,4 +3,4 @@ require('aurita-gui/element')
3
3
  require('aurita-gui/html')
4
4
  require('aurita-gui/form')
5
5
  require('aurita-gui/table')
6
-
6
+ require('aurita-gui/button')
@@ -46,35 +46,43 @@ module GUI
46
46
 
47
47
  @@element_count = 0
48
48
 
49
- attr_accessor :attrib, :type, :content, :parent, :tag
49
+ attr_accessor :attrib, :content, :parent, :tag, :force_closing_tag
50
50
 
51
51
  def initialize(params={}, &block)
52
+
52
53
  @@element_count += 1
53
54
  @id = @@element_count
54
55
  @parent = params[:parent]
55
- @force_close = params[:force_closing_tag]
56
+ @force_closing_tag = params[:force_closing_tag]
57
+ params[:tag] = :div if params[:tag].nil?
58
+ @tag = params[:tag]
59
+ if [ :div, :label, :button, :textarea ].include? @tag then
60
+ @force_closing_tag = true
61
+ end
56
62
  params.delete(:parent)
57
63
  params.delete(:force_closing_tag)
58
64
 
59
- params[:tag] = :div if params[:tag].nil?
60
-
61
65
  if block_given? then
62
66
  @content = yield
63
67
  else
64
68
  @content = params[:content]
65
- @content = [ @content ] unless @content.kind_of? Array
66
69
  end
70
+ @content = [ @content ] unless (@content.kind_of? Array or @content.to_s.length == 0)
71
+ @content ||= []
67
72
  params.delete(:content)
68
- @tag = params[:tag]
69
73
  params.delete(:tag)
70
74
  # params[:id] = self.class.to_s.split('::')[-1].downcase + '_' << @@element_count.to_s if params[:id].nil?
71
- params[:onclick] << ';' unless params[:onclick].nil? or params[:onclick].include?(';')
75
+ # params[:onclick] << ';' unless params[:onclick].nil? or params[:onclick].include?(';')
72
76
 
73
77
  @attrib = params
74
78
  # @attrib[:id] = @attrib[:id].to_s
75
79
 
76
80
  end
77
81
 
82
+ def has_content?
83
+ (@content.length > 0)
84
+ end
85
+
78
86
  # Return DOM id of this element.
79
87
  def dom_id
80
88
  @attrib[:id]
@@ -105,6 +113,10 @@ module GUI
105
113
  @attrib[meth.to_s.gsub('=','').intern] = value
106
114
  end
107
115
 
116
+ def content=(obj)
117
+ @content = [ obj ]
118
+ end
119
+
108
120
  # Define explicitly so built-in method #type
109
121
  # is not invoked instead
110
122
  def type=(type)
@@ -156,18 +168,20 @@ module GUI
156
168
  end
157
169
  if !value.nil? then
158
170
  value = value.to_s
159
- attrib_string << name.to_s + '="' << value + '" '
171
+ attrib_string << ' ' << name.to_s + '="' << value + '"'
160
172
  end
161
173
  }
162
174
 
163
- if @force_close || content.to_s != '' then
164
- '<' << @tag.to_s << ' ' << attrib_string << '>' << "\n " <<
165
- content.to_s.gsub("\n","\n ") + "\n" <<
166
- '</' << @tag.to_s << '>'
175
+ out = ''
176
+ if @force_closing_tag || content.to_s != '' then
177
+ out << "<#{@tag}"
178
+ out << attrib_string if attrib_string.length > 0
179
+ out << ">#{content}"
180
+ out << "</#{@tag}>"
167
181
  else
168
- '<' << @tag.to_s << ' ' << attrib_string << '/>'
182
+ out = "<#{@tag.to_s}#{attrib_string} />"
169
183
  end
170
-
184
+ return out
171
185
  end
172
186
  alias to_s string
173
187
 
@@ -26,7 +26,7 @@ module GUI
26
26
  attr_accessor :field
27
27
 
28
28
  def initialize(field)
29
- label_params = { :for => field.dom_id }
29
+ label_params = { :for => field.dom_id, :force_closing_tag => true }
30
30
  label_params[:id] = field.dom_id.to_s + '_label'
31
31
  label = field.label
32
32
  @content = [ HTML.label(label_params) { label }, field ]
@@ -177,7 +177,7 @@ module GUI
177
177
  #
178
178
  class Form < Element
179
179
 
180
- attr_accessor :method, :target, :action, :fields, :elements, :element_map
180
+ attr_accessor :method, :target, :action, :fields, :elements, :element_map, :values
181
181
 
182
182
  def initialize(params, &block)
183
183
  @action = params[:action]
@@ -216,9 +216,17 @@ module GUI
216
216
  end
217
217
 
218
218
  # Assign / overwrite field element with index form_index.
219
+ # TODO: FIX ME
219
220
  def []=(index, form_field)
220
- @elements[index] = form_field
221
221
  @content = false # Invalidate
222
+ if !index.kind_of? Numeric
223
+ @element_map[index,to_s] = form_field
224
+ @elements.collect { |e|
225
+ e = form_field if e.name.to_s == index.to_s
226
+ }
227
+ else
228
+ @elements[index] = form_field
229
+ end
222
230
  end
223
231
 
224
232
  # Delete form field with name field_name from
@@ -240,6 +248,8 @@ module GUI
240
248
  end
241
249
 
242
250
  # Add form field element to this form.
251
+ # TODO: Should overwrite previous field element
252
+ # with same field name.
243
253
  def add(form_field_element)
244
254
  if !form_field_element.dom_id then
245
255
  form_field_element.dom_id = form_field_element.name.to_s.gsub('.','_')
@@ -18,6 +18,7 @@ module GUI
18
18
  @attrib[:force_closing_tag] = true
19
19
  HTML.textarea(@attrib) { @value.to_s }
20
20
  end
21
+
21
22
  end
22
23
 
23
24
  end
@@ -220,9 +220,9 @@ module GUI
220
220
  def self.render(meth_name, attrib_hash={}, &block)
221
221
  raise ::Exception.new('Missing attributes for HTML.' << meth_name.inspect) unless attrib_hash
222
222
  attrib_hash[:tag] = meth_name
223
- cont = yield if block_given?
224
- attrib_hash[:content] = cont
225
- Element.new(attrib_hash)
223
+ # cont = yield if block_given?
224
+ # attrib_hash[:content] = cont
225
+ Element.new(attrib_hash, &block)
226
226
  end
227
227
 
228
228
  def self.build(&block)
data/spec/; ADDED
@@ -0,0 +1,14 @@
1
+
2
+ class Context_Loader
3
+
4
+ def self.render(element)
5
+ puts 'loader: ' << element.to_s + ' end loader'
6
+
7
+ def run(&block)
8
+ class_eval(&block)
9
+ end
10
+
11
+ end
12
+
13
+ l = Context_Loader.new()
14
+ l.run { 'foo' }
data/spec/element.rb ADDED
@@ -0,0 +1,157 @@
1
+
2
+ require('rubygems')
3
+ require('aurita-gui/element')
4
+
5
+ include Aurita::GUI
6
+
7
+ describe Aurita::GUI::Element, "basic rendering" do
8
+ before do
9
+ @e1 = Element.new(:tag => :tagname)
10
+ @inner = Element.new(:tag => :h2) { 'i am an inner header' }
11
+ @outer = Element.new(:tag => :p, :class => :outer) { @inner }
12
+ end
13
+
14
+ it "should have a tag attribute" do
15
+ e = Element.new(:tag => :tagname)
16
+ e.tag.should == :tagname
17
+ end
18
+
19
+ it "should have :div as default attribute" do
20
+ e = Element.new()
21
+ e.tag.should == :div
22
+ end
23
+
24
+ it "should redirect id to a tag attribute (DOM id)" do
25
+ e = Element.new(:id => :the_dom_id)
26
+ e.id.should == :the_dom_id
27
+ e.id = :changed_id
28
+ e.id.should == :changed_id
29
+ end
30
+
31
+ it "should render to an array automatically if necessary" do
32
+ e1 = Element.new(:id => :first)
33
+ e2 = Element.new(:id => :second)
34
+
35
+ (e1 << e2).should == [ e1, e2 ]
36
+ (e1 + e2).should == [ e1, e2 ]
37
+ end
38
+
39
+ it "should be able to render to string" do
40
+ @e1.to_s.should == '<tagname />'
41
+ @e1.string.should == '<tagname />'
42
+ end
43
+
44
+ it "should have enclosed content that is an array" do
45
+ @e1.content.length.should == 0
46
+ @e1.has_content?().should == false
47
+ @e1.content.should == []
48
+ end
49
+
50
+ it "should close the tag depending on content" do
51
+ @e1.content = 'the content'
52
+ @e1.content.should == [ 'the content' ]
53
+ @e1.to_s.should == '<tagname>the content</tagname>'
54
+ end
55
+
56
+ it "should not allow self-closing tags in some cases (textarea, label, button ...)" do
57
+ textarea = Element.new(:tag => :textarea)
58
+ textarea.to_s.should == '<textarea></textarea>'
59
+ button = Element.new(:tag => :button, :param => :value)
60
+ button.to_s.should == '<button param="value"></button>'
61
+ label = Element.new(:tag => :label, :param => :value)
62
+ label.to_s.should == '<label param="value"></label>'
63
+ end
64
+
65
+ it "should be possible to force a closing tag" do
66
+ e = Element.new(:tag => :something, :force_closing_tag => true)
67
+ e.to_s.should == '<something></something>'
68
+ e.force_closing_tag = false
69
+ e.to_s.should == '<something />'
70
+ end
71
+
72
+ it "should offer tag attributes as hash, maintaining keys as provided" do
73
+ e = Element.new(:foo => 1, :bar => 'barvalue', 'wombat' => 3)
74
+ e.attrib[:foo].should == 1
75
+ e.attrib[:bar].should == 'barvalue'
76
+ e.attrib['wombat'].should == 3
77
+ end
78
+
79
+ it "should render parameters as tag attributes" do
80
+ e = Element.new(:tag => :foo, :some_param => :the_value)
81
+ e.to_s.should == '<foo some_param="the_value" />'
82
+ end
83
+
84
+ it "should be possible to retreive original parameter values" do
85
+ e = Element.new(:tag => :foo, :some_param => :the_value)
86
+ e.some_param.should == :the_value
87
+ end
88
+
89
+ it "should accept parameter :content" do
90
+ e = Element.new(:tag => :bar, :content => 'content here', :name => :wombat)
91
+ e.content.should == ['content here']
92
+ e.name.should == :wombat
93
+ e.to_s.should == '<bar name="wombat">content here</bar>'
94
+ end
95
+
96
+ it "should accept content as block, maintaining object identity" do
97
+ test_obj_identity = 'lorem_ipsum'
98
+ e = Element.new(:tag => :jada) { test_obj_identity }
99
+ e.content.should == [ test_obj_identity ]
100
+ e.tag.should == :jada
101
+ end
102
+
103
+ it "should be possible to add new attributes" do
104
+ e = Element.new(:tag => :jada) { 'string' }
105
+ added_value = 'added_value'
106
+ e.added = added_value
107
+ e.added.should == added_value
108
+ end
109
+
110
+ it "should accept other element instances as content" do
111
+ @outer.content.should == [ @inner ]
112
+ @outer.to_s.should == '<p class="outer"><h2>i am an inner header</h2></p>'
113
+ end
114
+
115
+ it "should maintain an object hierarchy" do
116
+ @outer.content.first.new_attrib = 23
117
+ @outer.content.first.new_attrib.should == 23
118
+ end
119
+
120
+ it "should be possible to alter an enclosed element after adding it as content" do
121
+ @inner.decorated_attrib = 'decorated'
122
+ @inner.content << ' with decoration'
123
+
124
+ @outer.to_s.should == '<p class="outer"><h2 decorated_attrib="decorated">i am an inner header with decoration</h2></p>'
125
+
126
+ @inner.content = 'maintaining hierarchies rocks!'
127
+
128
+ @outer.to_s.should == '<p class="outer"><h2 decorated_attrib="decorated">maintaining hierarchies rocks!</h2></p>'
129
+ end
130
+
131
+ it "should delegate array operators to @content array" do
132
+ inner = Element.new(:tag => :h2) { 'i am an inner header' }
133
+ outer = Element.new(:tag => :p) { inner }
134
+ outer[0].new_attrib = 23
135
+ outer[0].new_attrib.should == 23
136
+ end
137
+
138
+ it "should be able to overwrite tag attributes" do
139
+ e = Element.new(:tag => :div, :the_param => 42)
140
+ e.the_param.should == 42
141
+ e.the_param = 23
142
+ e.the_param.should == 23
143
+ end
144
+
145
+ it "should be possible to add content elements" do
146
+ e = Element.new(:foo => :bar)
147
+ ras = 'one'
148
+ dwa = 'two'
149
+ tri = 'three'
150
+ e.content = ras
151
+ e.content.should == [ ras ]
152
+ e.content << dwa
153
+ e.content << tri
154
+ e.content.should == [ ras, dwa, tri ]
155
+ end
156
+
157
+ end
data/spec/html.rb ADDED
@@ -0,0 +1,39 @@
1
+
2
+ require('rubygems')
3
+ require('aurita-gui/html')
4
+
5
+ include Aurita::GUI
6
+
7
+ describe Aurita::GUI::HTML, "basic rendering" do
8
+ before do
9
+ @e = HTML.build {
10
+ div(:class => :outer) {
11
+ h2(:id => :header) { 'Header' } +
12
+ p(:id => :content) { 'Content' }
13
+ }
14
+ }
15
+ end
16
+
17
+ it "should act as factory by setting tag attribute for Element on class method calls" do
18
+ HTML.p.to_s.should == Element.new(:tag => :p).to_s
19
+ HTML.p(:class => :auto).to_s.should == Element.new(:tag => :p, :class => :auto).to_s
20
+ end
21
+
22
+ it "should accept a block and pass it to Element as content" do
23
+ HTML.p { 'string' }.to_s.should == '<p>string</p>'
24
+ end
25
+
26
+ it "should provide a build mechanism that delegates class methods" do
27
+ @e.to_s = '<div class="outer"><h2 id="header">Header</h2><p id="content">Content</p></div>'
28
+ end
29
+
30
+ it "should still provide an object tree" do
31
+ @e[0].id.should == :header
32
+ @e[1].id.should == :content
33
+ @e[1].content.should == [ 'Content' ]
34
+ @e[1].content = 'Altered'
35
+ @e[1].content.should == [ 'Altered' ]
36
+ @e.to_s.should == '<div class="outer"><h2 id="header">Header</h2><p id="content">Altered</p></div>'
37
+ end
38
+
39
+ end
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.3.0
4
+ version: 0.3.1
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-13 00:00:00 +01:00
12
+ date: 2009-01-15 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,6 +24,7 @@ extra_rdoc_files: []
24
24
  files:
25
25
  - fieldtest.rb
26
26
  - cheatsheet.rb
27
+ - spec
27
28
  - History.txt
28
29
  - lib
29
30
  - aurita-gui.gemspec
@@ -56,9 +57,9 @@ files:
56
57
  - lib/aurita-gui/form/text_field.rb
57
58
  - lib/aurita-gui/form/input_field.rb
58
59
  - lib/aurita-gui/form/date_field.rb
59
- - test/tc_options.rb
60
- - test/tc_table.rb
61
- - test/tc_form.rb
60
+ - spec/;
61
+ - spec/html.rb
62
+ - spec/element.rb
62
63
  has_rdoc: true
63
64
  homepage: http://aurita.wortundform.de/
64
65
  post_install_message:
data/test/tc_form.rb DELETED
@@ -1,30 +0,0 @@
1
-
2
- require('aurita-gui/form')
3
-
4
- include Aurita::GUI
5
-
6
- form = Form.new(:method => :post, :action => '/where/to/send', :onsubmit => "alert('submitting');")
7
-
8
- input1 = Input_Field.new(:name => :description, :label => 'Description',
9
- :class => :custom_input_field_class,
10
- :value => 'first input text'),
11
- input2 = Input_Field.new(:name => :custom, :label => 'Custom ID',
12
- :id => :custom_id_value,
13
- :value => 'second input text'),
14
- select = Select_Field.new(:name => :category, :label => 'Select category',
15
- :disabled => true,
16
- :value => :value_b, # Value to be selected
17
- :options => { :value_a => :key_a, :value_b => :key_b })
18
- form[1].readonly!
19
- form.each { |e| e.attrib[:class] = [ e.attrib[:class], :aurita ] }
20
- form.each { |e| e.attrib[:onfocus] = "alert('focussed #{e.id}');" }
21
-
22
- puts HTML.h1 { 'Single element' }
23
- puts form.element_map[:description]
24
-
25
- puts HTML.h1 { 'Editable form, one element disabled' }
26
- puts form
27
-
28
- puts HTML.h1 { 'Readonly form' }
29
- form.readonly!
30
- puts form
data/test/tc_options.rb DELETED
@@ -1,59 +0,0 @@
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
-
data/test/tc_table.rb DELETED
@@ -1,23 +0,0 @@
1
-
2
- require 'aurita-gui/table'
3
-
4
-
5
- include Aurita::GUI
6
-
7
- t = Table.new(:headers => ['user', 'phone', 'email'],
8
- :options => { :class => 'css_class', :id => 'test_table', :border => 1, :cellpadding => 3 })
9
-
10
- t.add_row([ 'a','b','c' ])
11
- t.add_row([ 'd','e','f','g' ])
12
-
13
- t[0][0].value = 'foo'
14
- t[0][0].colspan = 2
15
- t[1][0].onclick = 'test();'
16
- t[0][1] = HTML.a(:href => 'http://google.com') { 'google' }
17
-
18
- t[0][1].value.href = 'other'
19
- t[0][1].value.content = 'clickme'
20
-
21
- t[0].class = 'highlighted'
22
-
23
- puts t