Sha256: 61851eeae4365ae0854947b115cc29c8734907a3ce3d3a8e0ec83536d002b4f6

Contents?: true

Size: 827 Bytes

Versions: 1

Compression:

Stored size: 827 Bytes

Contents

module InsalesApi
  class ActiveResourceProxy
    class << self
      def need_proxy?(value)
        return true if value.is_a?(Hash) || value.is_a?(Array)
        klass = value.is_a?(Class) ? value : value.class
        return true if klass < Base || klass <= ActiveResource::Collection
        false
      end
    end

    def initialize(configurer, subject)
      @configurer = configurer
      @subject    = subject
    end

    def respond_to_missing?(method_name)
      @subject.respond_to?(method_name) || super
    end

    def method_missing(method_id, *args, &block)
      @configurer.init_api { proxy_for @subject.send(method_id, *args, &block) }
    end

    private
      def proxy_for(value)
        return value unless self.class.need_proxy?(value)
        self.class.new(@configurer, value)
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
insales_api-0.1.0 lib/insales_api/active_resource_proxy.rb