S
Sergey Sheypak
Hello, I'm working with yaml and objects.
Please, see my model:
#Base class for all models.
class DataBean
#list allowed fields for model
def allowed_fields
[]
end
def is_attr_allowed!(attr)
raise(ValidationException, "Attribute with name [#{attr}] is not
allowed for model #{self.class}.
Allowed fields are
#{allowed_fields}", caller) unless allowed_fields.include?(attr)
end
def initialize(options)
options.each_pair { |attr_name , value|
is_attr_allowed!(attr_name)
self.class.senddefine_method, attr_name, Proc.new{value})
}
end
end
class Dates < DataBean
def allowed_fields
[:start, #project beginning
:end, #project ending
:current] #last modification date "T", means current date
end
end
So I can use 'rails like intializers'
Dates.newstart=>Date.new, :end=>Dates.new+100)
The problem is that I'm reading Dates class fields from yaml file.
Dates:
class: Dates
fields_ordered: [start, current, end]
And I pass String (read from yaml) as a hash key to initializer:
Dates.new('start'=>Date.new)
As I read, :symbols are constants, they are immutable and have int
representation. These facts help to spend less memory and resources.
Do I have any opportunity to pass Strings into my initializers?
Please, see my model:
#Base class for all models.
class DataBean
#list allowed fields for model
def allowed_fields
[]
end
def is_attr_allowed!(attr)
raise(ValidationException, "Attribute with name [#{attr}] is not
allowed for model #{self.class}.
Allowed fields are
#{allowed_fields}", caller) unless allowed_fields.include?(attr)
end
def initialize(options)
options.each_pair { |attr_name , value|
is_attr_allowed!(attr_name)
self.class.senddefine_method, attr_name, Proc.new{value})
}
end
end
class Dates < DataBean
def allowed_fields
[:start, #project beginning
:end, #project ending
:current] #last modification date "T", means current date
end
end
So I can use 'rails like intializers'
Dates.newstart=>Date.new, :end=>Dates.new+100)
The problem is that I'm reading Dates class fields from yaml file.
Dates:
class: Dates
fields_ordered: [start, current, end]
And I pass String (read from yaml) as a hash key to initializer:
Dates.new('start'=>Date.new)
As I read, :symbols are constants, they are immutable and have int
representation. These facts help to spend less memory and resources.
Do I have any opportunity to pass Strings into my initializers?