Sha256: f8372b90d944a48b89d3872944e8931d191c3b7f8d03a8e7ecdb9d92f0118373

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require 'uri'

module Judge
  module Controller

    def self.included(base)
      base.clear_respond_to
      base.respond_to(:json)
    end

    def validation(params)
      params = normalized_params(params)
      if params_present?(params) && params_exposed?(params)
        Validation.new(params)
      else
        NullValidation.new(params)
      end
    end

    private

      REQUIRED_PARAMS = %w{klass attribute value kind}

      def params_exposed?(params)
        Judge.config.exposed?(params[:klass], params[:attribute])
      end

      def params_present?(params)
        params.keys == REQUIRED_PARAMS && params.values.all?
      end

      def normalized_params(params)
        params = params.dup.keep_if { |k| REQUIRED_PARAMS.include?(k) }
        params[:klass]     = find_klass(params[:klass]) if params[:klass]
        params[:attribute] = params[:attribute].to_sym  if params[:attribute]
        params[:value]     = URI.decode(params[:value]) if params[:value]
        params[:kind]      = params[:kind].to_sym       if params[:kind]
        params
      end

      def find_klass(name)
        Module.const_get(name.classify)
      rescue NameError
        nil
      end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
judge-2.0.5 lib/judge/controller.rb
judge-2.0.4 lib/judge/controller.rb