[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Widget-like API for DOM objects?

Berger, Daniel

1/13/2006 3:40:00 PM

Hi all,

Is there something out there that would let me create DOM objects in a more
Rubyesque manner? For example, say I want to create a 'select' object in a web
page. I'd like an API something along these lines:

select = Select.new
select.options = ["joe", "bob", "moe"] # or {1=>"joe", 2=>"bob", 3=>"moe"}
select.selected_index = 2
select.title = "Three guys"
select.access_key = "A"

p select.html # Show html that would be generated using above options

You get the picture. Is there anything like this out on the RAA? Nothing
immediately jumped out at me.

Regards,

Dan


7 Answers

Hannes Wyss

1/13/2006 4:17:00 PM

0

Dan

I can give you this: http://scm.ywesee.com/?...

However, this was one of my very first ruby-projects and basically needs
a complete rewrite.. Some of the reasons:
- Geared towards Table-Layout, hence the 'grid'
- tight coupling to http://scm.ywesee.c... which is a
state-based session-manager
- meta-programming by configuration-constants
- naming conventions changed in mid-project
- nonexistant documentation

That said, if you want to try it out and help me get the priorities for
a next version straight, you're more than welcome to do so and I hereby
pledge my support - which you'll probably need ;)

Cheers
Hannes

On 1/13/06, Daniel Berger <Daniel.Berger@qwest.com> wrote:
> Hi all,
>
> Is there something out there that would let me create DOM objects in a more
> Rubyesque manner? For example, say I want to create a 'select' object in a web
> page. I'd like an API something along these lines:
>
> select = Select.new
> select.options = ["joe", "bob", "moe"] # or {1=>"joe", 2=>"bob", 3=>"moe"}
> select.selected_index = 2
> select.title = "Three guys"
> select.access_key = "A"
>
> p select.html # Show html that would be generated using above options
>
> You get the picture. Is there anything like this out on the RAA? Nothing
> immediately jumped out at me.
>
> Regards,
>
> Dan
>
>


Berger, Daniel

1/13/2006 4:39:00 PM

0

Hannes Wyss wrote:
> Mime-Version: 1.0
> Content-Type: text/plain; charset=ISO-8859-1
> Content-Transfer-Encoding: quoted-printable
> Precedence: bulk
> Lines: 47
> List-Id: ruby-talk.ruby-lang.org
> List-Software: fml [fml 4.0.3 release (20011202/4.0.3)]
> List-Post: <mailto:ruby-talk@ruby-lang.org>
> List-Owner: <mailto:ruby-talk-admin@ruby-lang.org>
> List-Help: <mailto:ruby-talk-ctl@ruby-lang.org?body=help>
> List-Unsubscribe: <mailto:ruby-talk-ctl@ruby-lang.org?body=unsubscribe>
>
> Dan
>
> I can give you this: http://scm.ywesee.com/?p=...
>
> However, this was one of my very first ruby-projects and basically needs
> a complete rewrite.. Some of the reasons:
> - Geared towards Table-Layout, hence the 'grid'
> - tight coupling to http://scm.ywesee.com... which is a
> state-based session-manager
> - meta-programming by configuration-constants
> - naming conventions changed in mid-project
> - nonexistant documentation
>
> That said, if you want to try it out and help me get the priorities for
> a next version straight, you're more than welcome to do so and I hereby
> pledge my support - which you'll probably need ;)

I tried to look at the link you provided but I get a 403 forbidden. :(

Dan



Hannes Wyss

1/13/2006 4:46:00 PM

0

On 1/13/06, Daniel Berger <Daniel.Berger@qwest.com> wrote:
> I tried to look at the link you provided but I get a 403 forbidden. :(

sorry 'bout that, must have been a copy-paste thingummy... Try again:
http://scm.ywesee.com/?...

Hannes


Hannes Wyss

1/13/2006 4:57:00 PM

0

On 1/13/06, Hannes Wyss <hannes.wyss@gmail.com> wrote:
> On 1/13/06, Daniel Berger <Daniel.Berger@qwest.com> wrote:
> > I tried to look at the link you provided but I get a 403 forbidden. :(

I must learn to think before posting... I've made a tarball, just in
case you don't want to install git to download the code:
http://download.ywesee.com/htmlgrid/htmlgrid-dai...
http://download.ywesee.com/sbsm/sbsm-dai...

Apologies for all the noise...
Hannes


Paul Duncan

1/13/2006 7:50:00 PM

0

* Daniel Berger (Daniel.Berger@qwest.com) wrote:
> Hi all,
>
> Is there something out there that would let me create DOM objects in a more
> Rubyesque manner? For example, say I want to create a 'select' object in a
> web page. I'd like an API something along these lines:
>
> select = Select.new
> select.options = ["joe", "bob", "moe"] # or {1=>"joe", 2=>"bob", 3=>"moe"}
> select.selected_index = 2
> select.title = "Three guys"
> select.access_key = "A"
>
> p select.html # Show html that would be generated using above options
>
> You get the picture. Is there anything like this out on the RAA? Nothing
> immediately jumped out at me.

REXML has a DOM-like API, and it comes pre-installed with Ruby:

# load REXML
require 'rexml/document'

#
# Create an HTML select element.
#
class HTMLSelect
attr_accessor :options, :attrs, :index

def initialize(options = [], select_attrs = {}, selected_index = nil)
@options, @attrs, @index = options, select_attrs, selected_index
end

def to_s
# create return select element and append requested attributes
ret = REXML::Element.new('select')
@attrs.each { |key, val| ret.attributes[key] = val }

# iterate over, create, and append each option
@options.each_with_index do |opt, i|
# create and append element
opt_elem = ret << REXML::Element.new('option')

# add option text, and option attributes
opt_elem << REXML::Text.new(opt)
opt_elem.attributes['value'] = i.to_s
opt_elem.attributes['selected'] = 'Y' if i == @index
end

# convert select element to string and return
ret.to_s
end
end

# define attributes, option items, and initial selected index
select_attrs = {
'name' => 'which_guy',
'title' => 'Three Guys',
'access_key' => 'A',
}
selected_index, options = 2, %w{joe bob moe}

# build select box and print out result
puts HTMLSelect.new(options, select_attrs, selected_index).to_s

Hope this helps...

--
Paul Duncan <pabs@pablotron.org> pabs in #ruby-lang (OPN IRC)
http://www.pabl... OpenPGP Key ID: 0x82C29562

Berger, Daniel

1/13/2006 7:55:00 PM

0

Paul Duncan wrote:
> * Daniel Berger (Daniel.Berger@qwest.com) wrote:
>
>>Hi all,
>>
>>Is there something out there that would let me create DOM objects in a more
>>Rubyesque manner? For example, say I want to create a 'select' object in a
>>web page. I'd like an API something along these lines:
>>
>>select = Select.new
>>select.options = ["joe", "bob", "moe"] # or {1=>"joe", 2=>"bob", 3=>"moe"}
>>select.selected_index = 2
>>select.title = "Three guys"
>>select.access_key = "A"
>>
>>p select.html # Show html that would be generated using above options
>>
>>You get the picture. Is there anything like this out on the RAA? Nothing
>>immediately jumped out at me.
>
>
> REXML has a DOM-like API, and it comes pre-installed with Ruby:
>
> # load REXML
> require 'rexml/document'
>
> #
> # Create an HTML select element.
> #
> class HTMLSelect
> attr_accessor :options, :attrs, :index
>
> def initialize(options = [], select_attrs = {}, selected_index = nil)
> @options, @attrs, @index = options, select_attrs, selected_index
> end
>
> def to_s
> # create return select element and append requested attributes
> ret = REXML::Element.new('select')
> @attrs.each { |key, val| ret.attributes[key] = val }
>
> # iterate over, create, and append each option
> @options.each_with_index do |opt, i|
> # create and append element
> opt_elem = ret << REXML::Element.new('option')
>
> # add option text, and option attributes
> opt_elem << REXML::Text.new(opt)
> opt_elem.attributes['value'] = i.to_s
> opt_elem.attributes['selected'] = 'Y' if i == @index
> end
>
> # convert select element to string and return
> ret.to_s
> end
> end
>
> # define attributes, option items, and initial selected index
> select_attrs = {
> 'name' => 'which_guy',
> 'title' => 'Three Guys',
> 'access_key' => 'A',
> }
> selected_index, options = 2, %w{joe bob moe}
>
> # build select box and print out result
> puts HTMLSelect.new(options, select_attrs, selected_index).to_s
>
> Hope this helps...
>

Ah, thanks Paul. I'd like something prebuilt, but it looks like REXML would
serve as a great way to build things behind the scenes.

Regards,

Dan


Kero van Gelder

1/17/2006 9:25:00 AM

0

> Is there something out there that would let me create DOM objects in a more
> Rubyesque manner? For example, say I want to create a 'select' object in a web
> page. I'd like an API something along these lines:
>
> select = Select.new
> select.options = ["joe", "bob", "moe"] # or {1=>"joe", 2=>"bob", 3=>"moe"}
> select.selected_index = 2
> select.title = "Three guys"
> select.access_key = "A"
>
> p select.html # Show html that would be generated using above options
>
> You get the picture. Is there anything like this out on the RAA? Nothing
> immediately jumped out at me.

How about the stdlib cgi?