class OEHClient::Data::Node attr_accessor :nodes def initialize(data) # Raise an exception if the data passed is not already a hash # based on the type of object that is passed, create the proper attribute accessor # to allow easy access to data. For example, a simple value in the structure called # firstName will be represented in the object as my_structure.first_name. Simple # object are represented as related node classes. For example a Preference node with # an attribute of nonSmokingRoom will be represented as my_structure.preference.non_smoking_room data.each do | key, value | (value.kind_of?(Array) ? add_collection(key, value) : add_attribute(key, value)) end end # return the names of each of the attributes of the current nodes of the structure def attribute_names() name_collection = Array.new instance_variable_names.each do | structure_attribute | structure_attribute.slice!(0) name_collection << structure_attribute end name_collection end # def attribute_names private # add_collection dynamically create an attr_accessor as an array of Oeh::Structure::Node # classes related to the current instance of the object. def add_collection(name, value_collection) # initialize the array object that will act as the value of the attribute node_collection = [] # for each hash in the array, add a new structure node value_collection.each do | structure_node | node_collection << OEHClient::Data::Structure.new(structure_node) end # add the arry of node objects to the accessor add_attribute(name, node_collection) end # add_attribute dynamically creates an attr_accessor for the passed key_value pair either # as a simple value attribute or as an instance of another Node class def add_attribute(name, value) # generate a ruby-friendly attribute accessor based on the name of the # structure attribute accessor_name = name.underscore accessor_name.gsub!(/( )/, '_') if (accessor_name.match(/\s/)) # create the accessor in the current class and set the value based on the type # of object that represents the value self.class.send(:attr_accessor, accessor_name) instance_variable_set("@#{accessor_name}", (value.kind_of?(Hash) ? OEHClient::Data::Node.new(value) : value)) end end