Module: SelectiveAttributeProxy

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/aws/s3/extensions.rb', line 127

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