Module: SelectiveAttributeProxy

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/aliyun/ess/extensions.rb', line 113

def self.included(klass)
  klass.extend(ClassMethods)
  klass.class_eval("    cattr_accessor :attribute_proxy\n    cattr_accessor :attribute_proxy_options\n    \n    # Default name for attribute storage\n    self.attribute_proxy         = :attributes\n    self.attribute_proxy_options = {:exclusively => true}\n    \n    private\n      # By default proxy all attributes\n      def proxiable_attribute?(name)\n        return true unless self.class.attribute_proxy_options[:exclusively]\n        send(self.class.attribute_proxy).has_key?(name)\n      end\n      \n      def method_missing(method, *args, &block)\n        # Autovivify attribute storage\n        if method == self.class.attribute_proxy\n          ivar = \"@\\\#{method}\"\n          instance_variable_set(ivar, {}) unless instance_variable_get(ivar).is_a?(Hash)\n          instance_variable_get(ivar)\n        # Delegate to attribute storage\n        elsif method.to_s =~ /^(\\\\w+)(=?)$/ && proxiable_attribute?($1)\n          attributes_hash_name = self.class.attribute_proxy\n          $2.empty? ? send(attributes_hash_name)[$1] : send(attributes_hash_name)[$1] = args.first\n        else\n          super\n        end\n      end\n  EVAL\nend\n", __FILE__, __LINE__)