Sha256: cacef7ad846e624ec01eafd80b118c6c5e8c22b0562c5df56f736b7bb81906e6
Contents?: true
Size: 1.94 KB
Versions: 9
Compression:
Stored size: 1.94 KB
Contents
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 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
Version data entries
9 entries across 9 versions & 1 rubygems