S
swille
I'm admittedly quite a novice with programming. I'm sort of playing
around with some ideas, and I'm wondering if what I'm doing is
completely stupid, and if not, is there a better way to do it? Also,
is there any way that I could do some type of type-ish validation on
the data?
Basically, I want to provide what I suppose would be abstract class
(?), which someone else could extend and add attributes if they'd
like. I'd like to be able to look into the class and pick out the
attributes to populate (which is the reason for all of the sp_set &
sp_get stuff). Basically, my thought is to be able to provide a web
interface at some point to populate the needed data of a class, and
then maybe Marshal.dump the object to a database and call it later
using that data as config data essentially. I'm sure I could learn a
ton about this from reading Rails code, but there's so much of it that
I don't know where to look. Output and code follows. Please feel
free to tell me if this is dumb :O)
Output:
Enter Username: name
Enter Url: url
Enter Extra: more
Enter Password: pass
Enter Owner: own
Enter Certificate: cert
name
url
more
pass
own
cert
Code:
<code>
class ServiceProfile
def initialize()
end
def sp_set_url(url)
@url =3D url
end
def sp_get_url()
@url
end
def sp_set_username(username)
@username =3D username
end
def sp_get_username()
@username
end
def sp_set_password(password)
@password =3D password
end
def sp_get_password()
@password
end
def sp_set_certificate(certificate)
@certificate =3D certificate
end
def sp_get_certificate()
@certificate
end
def sp_set_owner(owner)
@owner =3D owner
end
def sp_get_owner()
@owner
end
def load_certificate()
@certificate.read
end
def call_service(&block)
end
end
class MyService < ServiceProfile
def sp_set_extra(extra)
@extra =3D extra
end
def sp_get_extra()
@extra
end
end
svc_profile =3D MyService.new
set_re =3D Regexp.new('^sp_set_(\w+)')
get_re =3D Regexp.new('^sp_get_(\w+)')
svc_profile.methods.sort.reverse.each { |meth|
if meth.match(set_re)
print "Enter #{Regexp.last_match(1).capitalize}: "
value =3D $stdin.gets.chomp
svc_profile.send(meth, value)
end
}
svc_profile.methods.sort.reverse.each { |meth|
if meth.match(get_re)
puts svc_profile.send(meth)
end
}
</code>
around with some ideas, and I'm wondering if what I'm doing is
completely stupid, and if not, is there a better way to do it? Also,
is there any way that I could do some type of type-ish validation on
the data?
Basically, I want to provide what I suppose would be abstract class
(?), which someone else could extend and add attributes if they'd
like. I'd like to be able to look into the class and pick out the
attributes to populate (which is the reason for all of the sp_set &
sp_get stuff). Basically, my thought is to be able to provide a web
interface at some point to populate the needed data of a class, and
then maybe Marshal.dump the object to a database and call it later
using that data as config data essentially. I'm sure I could learn a
ton about this from reading Rails code, but there's so much of it that
I don't know where to look. Output and code follows. Please feel
free to tell me if this is dumb :O)
Output:
Enter Username: name
Enter Url: url
Enter Extra: more
Enter Password: pass
Enter Owner: own
Enter Certificate: cert
name
url
more
pass
own
cert
Code:
<code>
class ServiceProfile
def initialize()
end
def sp_set_url(url)
@url =3D url
end
def sp_get_url()
@url
end
def sp_set_username(username)
@username =3D username
end
def sp_get_username()
@username
end
def sp_set_password(password)
@password =3D password
end
def sp_get_password()
@password
end
def sp_set_certificate(certificate)
@certificate =3D certificate
end
def sp_get_certificate()
@certificate
end
def sp_set_owner(owner)
@owner =3D owner
end
def sp_get_owner()
@owner
end
def load_certificate()
@certificate.read
end
def call_service(&block)
end
end
class MyService < ServiceProfile
def sp_set_extra(extra)
@extra =3D extra
end
def sp_get_extra()
@extra
end
end
svc_profile =3D MyService.new
set_re =3D Regexp.new('^sp_set_(\w+)')
get_re =3D Regexp.new('^sp_get_(\w+)')
svc_profile.methods.sort.reverse.each { |meth|
if meth.match(set_re)
print "Enter #{Regexp.last_match(1).capitalize}: "
value =3D $stdin.gets.chomp
svc_profile.send(meth, value)
end
}
svc_profile.methods.sort.reverse.each { |meth|
if meth.match(get_re)
puts svc_profile.send(meth)
end
}
</code>