DSL question

J

Joe Van Dyk

Hi,

require 'traits'
$applications =3D {}

class Application
has %w{ application_id title executable options}
end

def application(application_id, &block)
a =3D Application.new
###### What goes here? #############
$applications[application_id] =3D a
end

application :joe_app do
title "Joe's Application"
executable "My executable"
options "my options"
end

application :another_app do
title "Another App"
executable "another exec"
options "the options"
end


It should be pretty apparent to what I'm trying to do. Any ideas?=20
Can I restructure this better somehow?

Thanks,
Joe
 
J

Joel VanderWerf

Joe said:
Hi,

require 'traits'
$applications = {}

class Application
has %w{ application_id title executable options}
end

def application(application_id, &block)
a = Application.new
###### What goes here? #############
$applications[application_id] = a
end

application :joe_app do
title "Joe's Application"
executable "My executable"
options "my options"
end

application :another_app do
title "Another App"
executable "another exec"
options "the options"
end


It should be pretty apparent to what I'm trying to do. Any ideas?
Can I restructure this better somehow?

instance_eval can help:

class Application
def title(*args)
case args.size
when 1
@title = args[0]
when 0
@title
end
end

def initialize(&block)
instance_eval(&block)
end
end

a = Application.new do
title "Foo"
end

puts a.title # ==> Foo
 
J

Joe Van Dyk

Joe said:
Hi,

require 'traits'
$applications =3D {}

class Application
has %w{ application_id title executable options}
end

def application(application_id, &block)
a =3D Application.new
###### What goes here? #############
$applications[application_id] =3D a
end

application :joe_app do
title "Joe's Application"
executable "My executable"
options "my options"
end

application :another_app do
title "Another App"
executable "another exec"
options "the options"
end


It should be pretty apparent to what I'm trying to do. Any ideas?
Can I restructure this better somehow?
=20
instance_eval can help:
=20



Ooh, thanks. I forgot about that one. Revised code:

joe@ubuntu:~ $ cat test.rb
require 'traits'
$applications =3D {}

class Application
has %w{ application_id title executable options }
end

def application(application_id, &block)
a =3D Application.new
a.instance_eval(&block)
$applications[application_id] =3D a
end

application :joe_app do
title "Joe's Application"
executable "My executable"
options "my options"
end

application :another_app do
title "Another App"
executable "another exec"
options "the options"
end

puts $applications[:joe_app].title


joe@ubuntu:~ $ ruby test.rb
Joe's Application
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top