From d1142c6f0a0adc33714e637f6459da5ad7385a81 Mon Sep 17 00:00:00 2001 From: Anton Dyachuk Date: Mon, 7 Jul 2008 18:30:31 +0400 Subject: [PATCH] Separate properties to attributes and relationships and improve performance by caching methods and properties --- lib/callable.rb | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/callable.rb b/lib/callable.rb index 6bd7ec1..65388a1 100644 --- a/lib/callable.rb +++ b/lib/callable.rb @@ -8,12 +8,42 @@ def intersys_reflector # returns list of methods for this class def intersys_methods - @methods ||= intersys_reflector._methods + @methods ||= intersys_reflector._methods.to_a + end + + def intersys_method_names + @method_names ||= intersys_methods.map { |method| method.intersys_get('Name') } end # returns list of properties for current class def intersys_properties - @properties ||= intersys_reflector.properties + properties = Intersys::Object.common_get_or_set('@properties', {}) + properties[class_name] ||= intersys_reflector.properties.to_a + end + + def intersys_property_names + property_names = Intersys::Object.common_get_or_set('@property_names', {}) + property_names[class_name] ||= intersys_properties.map { |prop| prop.intersys_get('Name') } + end + + def intersys_relations + relations = Intersys::Object.common_get_or_set('@relations', {}) + relations[class_name] ||= intersys_properties.find_all { |prop| prop.relationship } + end + + def intersys_relation_names + relation_names = Intersys::Object.common_get_or_set('@relation_names', {}) + relation_names[class_name] ||= intersys_relations.map { |prop| prop.intersys_get('Name') } + end + + def intersys_attributes + attributes = Intersys::Object.common_get_or_set('@attributes', {}) + attributes[class_name] ||= intersys_properties.find_all { |prop| !prop.relationship } + end + + def intersys_attribute_names + attribute_names = Intersys::Object.common_get_or_set('@attribute_names', {}) + attribute_names[class_name] ||= intersys_attributes.map { |prop| prop.intersys_get('Name') } end #protected @@ -37,11 +67,11 @@ def intersys_call(method_name, *args) alias :call :intersys_call def intersys_has_property?(property) - self.intersys_reflector.properties.to_a.include?(property) + intersys_property_names.include?(property) end def intersys_has_method?(method) - self.intersys_reflector._methods.to_a.include?(method) + intersys_method_names.include?(method) end # Get the specified property @@ -56,10 +86,7 @@ def intersys_set(property, value) def method_missing(method, *args) method_name = method.to_s.camelize - if match_data = method_name.match(/intersys_(.*)/) - # Protection from errors in this method - return super(method, *args) - end + if match_data = method_name.match(/(\w+)=/) return intersys_set(match_data.captures.first, args.first) end @@ -83,4 +110,4 @@ class Object end Intersys::Object.extend(Callable) -end \ No newline at end of file +end