module UniverseCompiler module Entity module FieldManagement private def define_known_fields_accessors self.class.fields_constraints.each do |field_name, constraints| define_field_accessor field_name if fields[field_name].nil? fields[field_name] = [] if constraints[:has_many] || constraints[:is_array] fields[field_name] = {} if constraints[:is_hash] end end end def define_field_accessor(field_name) metaclass = class << self; self ; end UniverseCompiler.logger.debug 'Defining field accessor %s on class %s (%s)' % [field_name, metaclass, self.type] define_field_reader(field_name, metaclass) define_field_writer(field_name, metaclass) end def define_field_writer(field_name, metaclass) if self.respond_to? "#{field_name}=" UniverseCompiler.logger.info "'#{field_name}=' already exists on class '#{metaclass}'. Skipped !" else metaclass.instance_eval do define_method "#{field_name}=" do |value| fields[field_name] = value end end end end def define_field_reader(field_name, metaclass) if self.respond_to? field_name UniverseCompiler.logger.info "'#{field_name}' already exists on class '#{metaclass}'. Skipped !" else metaclass.instance_eval do define_method field_name do fields[field_name] end end end end end end end