Sha256: 00ceb05930ca47a1aa0ebd112ee936deaeea8e356d305b4e8ea78858c04a5702

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module MakeRestful::ClassMethods

  def resource_name
    resource_class.name.gsub(/(.*::)?(.*)/,"\\2").underscore.pluralize
  end

  def instance_name
    resource_name.singularize
  end

  # Is a format allowed?
  def allow_format?(*formats)
    true if formats.flatten.map(&:to_sym).select { |i| allowed_formats.map(&:to_sym).include? i }.present?
  end

  # Is a method allowed?
  def allow_method?(*methods)
    true if methods.flatten.map(&:to_sym).select { |i| allowed_methods.map(&:to_sym).include? i }.present?
  end

  alias :allow_action? :allow_method?

  private

  def resource(klass)

    before_filter :verify_method!
    before_filter :load_resource
    before_filter :load_collection, only: [:index]
    before_filter :load_instance, only: [:show, :destroy, :update]

    begin
      klass = klass.to_s.camelize.constantize
    rescue NameError
      klass = false
    end

    if klass
      self.resource_class = klass
    end

  end

  def find_by(key)
    self.finder = key.to_sym
  end

  def allow_method(*methods)
    self.allowed_methods += methods.flatten
  end
  alias :allow_methods :allow_method

  def allow_format(*formats)
    self.allowed_formats += formats.flatten
  end
  alias :allow_formats :allow_format

  def deny_method(*methods)
    self.allowed_methods -= methods
  end
  alias :deny_methods :deny_method

  def deny_format(*methods)
    self.allowed_formats -= methods
  end
  alias :deny_formats :deny_format

  def paginates!(opts={})
    opts.reverse_merge!({ per_page: 25 })
    self.pagination = opts
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
make_restful-0.1.3 lib/make_restful/class_methods.rb
make_restful-0.1.2 lib/make_restful/class_methods.rb