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