Sha256: c44d0d72908e1b98ab360d8885b1039a0361dbecd34a3c55d2430097ebd2c6b1

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module MotionResource
  class Base
    class_attribute :collection_url, :member_url
    class_attribute :root_url, :default_url_options
    class_attribute :extension
    class_attribute :url_encoder

    self.extension = '.json'
    self.url_encoder = UrlEncoder.new

    class << self
      def custom_urls(params = {})
        params.each do |name, url_format|
          define_method name do |method_params = {}|
            self.url_encoder.fill_url_params(url_format, method_params, self)
          end
          define_singleton_method name do
            url_format
          end
        end
      end
      
      def collection_url_or_default
        collection_url || name.underscore.pluralize
      end
      
      def member_url_or_default
        member_url || "#{name.underscore.pluralize}/:#{primary_key}"
      end

    end

    def collection_url(params = {})
      self.class.url_encoder.fill_url_params(self.class.collection_url_or_default, params, self)
    end

    def member_url(params = {})
      self.class.url_encoder.fill_url_params(self.class.member_url_or_default, params, self)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
motion-resource-0.1.4 lib/motion-resource/urls.rb