dynamically add classes to a running program

T

Tom

Hi i want to dynamically add classes to a running program given a description
provided in a textfile. I'm new at Ruby so i dont know how to implement this
and would apreciate som help.

This is what i want to do in detail:

A module Model should be implemented for this purpose(dynamically add classes)
with the method generate that accepts a file path to a specification file
and returns a class object for the generated class.

Syntax of Specification File
Below is a simple description of the syntax of a specification file:


title :Title
attribute :name_1, Type
...
attribute :name_n, Type
constraint :name_1, "boolean expression_1"
constraint :name_1, "boolean expression_2"
...
constraint :name_n, "boolean expression_m"

Where :Title should be the class name of the defined class, the attributes
should be attributes of the generated class. The accessors and mutators should
make sure that the values held by an attribute has the correct type, (expressed
here as a class), or nil. If the object has the wrong class, an error should
be raised.

Last, the constraints specify a set of additional constraints on the objects
of the attributes that must be upheld at all times, just as the type. Note
that an attribute can have several constraints.

Example:


title :person
attribute :name, String
attribute :age, Fixnum
constraint :name, 'name != nil'
constraint :name, 'name.size > 0'
constraint :name, 'name =~ /^[A-Z]/'
constraint :age, 'age >= 0'


This code should generate a class Person with the attributes name, a string,
and age, a fixed numeral. Whenever name is read or updated, the class should
make sure that name is not nil, that the name is longer than 0 characters,
and that it starts with an uppercase letter. Similarly, age must never be
negative.

When called, the generate method should parse a file such as the above, and
create a person class with an implementation that satisfies all the specified
constraints. Importantly, the person class should know how to parse a YAML
file containing person entries and return an array with such objects parsed
from a file (see the load_from_file call in the first example). This should
be handled by a class method that accepts as argument the path to a YAML
file with the data to be parsed. Entries that do not satisfy the constraints
or don't contain all the specified attributes should be ignored. Attributes
that are not mentioned in the specification should be ignored.

regards
Tom
 
P

Phlip

Tom said:
Hi i want to dynamically add classes to a running program given a
description
provided in a textfile. I'm new at Ruby so i dont know how to implement
this
and would apreciate som help.

Write Ruby source to a file and run this:

myObject = eval(File.read('myFile.rb'))

If your last line is like Klass.new(), you got your object.

Or...
Below is a simple description of the syntax of a specification file:

title :Title
attribute :name_1, Type

That YAML format could be a tiny bit better...
...
attribute :name_n, Type
constraint :name_1, "boolean expression_1"
constraint :name_1, "boolean expression_2"
...
constraint :name_n, "boolean expression_m"

After you read the Yaml object, it will appear in memory as a map. Index the
map, generally like map['constraint:name_2'] or similar, to get the
constraint. Then eval() the constraint.
Last, the constraints specify a set of additional constraints on the
objects
of the attributes that must be upheld at all times, just as the type. Note
that an attribute can have several constraints.

May we ask what the actual problem you want to solve is?
Example:

title :person
attribute :name, String
attribute :age, Fixnum
constraint :name, 'name != nil'

At this point, my eval() suggestion won't directly work, because name is not
yet a living variable. But you could consider making it into a variable by
building an assignment statement with its name and value, and prefixing this
to the eval() statement.
 
T

Timothy Goddard

You can create classes at runtime using Class.new:

def make_a_class_to_hold(*fields)
Class.new do
attr_accessor *fields
define_method :list_my_fields do
fields
end
end
end

MyClass = make_a_class_to_hold:)foo, :bar, :baz)
my_object = MyClass.new
puts my_object.list_my_fields
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top