Sha256: 72f7361c018107231fe772952a718f782137f24837e9245c32f05bc334051c19

Contents?: true

Size: 1.62 KB

Versions: 13

Compression:

Stored size: 1.62 KB

Contents

module ActiveRestClient
  module Mapping
    module ClassMethods
      def get(name, url, options = {})
        _map_call(name, url:url, method: :get, options:options)
      end

      def put(name, url, options = {})
        _map_call(name, url:url, method: :put, options:options)
      end

      def post(name, url, options = {})
        _map_call(name, url:url, method: :post, options:options)
      end

      def delete(name, url, options = {})
        _map_call(name, url:url, method: :delete, options:options)
      end

      def _map_call(name, details)
        _calls[name] = {name:name}.merge(details)
        _calls["lazy_#{name}".to_sym] = {name:name}.merge(details)
        self.class.send(:define_method, name) do |options={}|
          _call(name, options)
        end
        self.class.send(:define_method, "lazy_#{name}".to_sym) do |options={}|
          _call("lazy_#{name}", options)
        end
      end

      def _call(name, options)
        mapped = _calls[name]
        lazy_forced = false
        if mapped.nil? && name.to_s[/^lazy_/]
          mapped = _calls[name.to_s.gsub(/^lazy_/, '').to_sym]
          lazy_forced = true
        end
        request = Request.new(mapped, self, options)
        if lazy_load? || lazy_forced
          ActiveRestClient::LazyLoader.new(request)
        else
          request.call
        end
      end

      def _calls
        @_calls
      end

      def _mapped_method(name)
        _calls[name]
      end

      def inherited(subclass)
        subclass.instance_variable_set(:@_calls, {})
      end

    end

    def self.included(base)
      base.extend(ClassMethods)
    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
active_rest_client-0.9.75 lib/active_rest_client/mapping.rb
active_rest_client-0.9.73 lib/active_rest_client/mapping.rb
active_rest_client-0.9.72 lib/active_rest_client/mapping.rb
active_rest_client-0.9.71 lib/active_rest_client/mapping.rb
active_rest_client-0.9.70 lib/active_rest_client/mapping.rb
active_rest_client-0.9.69 lib/active_rest_client/mapping.rb
active_rest_client-0.9.68 lib/active_rest_client/mapping.rb
active_rest_client-0.9.67 lib/active_rest_client/mapping.rb
active_rest_client-0.9.66 lib/active_rest_client/mapping.rb
active_rest_client-0.9.65 lib/active_rest_client/mapping.rb
active_rest_client-0.9.60 lib/active_rest_client/mapping.rb
active_rest_client-0.9.59 lib/active_rest_client/mapping.rb
active_rest_client-0.9.58 lib/active_rest_client/mapping.rb