Sha256: c0fd369ce64d686bcf27c3e9152e53e37b2ac73b0ae1d2c400f69016ac9d55c4

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module Doorkeeper
  class InvalidSyntax < StandardError; end
  class DoorkeeperFor
    attr_reader :scopes

    def initialize(options)
      options ||= {}
      fail InvalidSyntax unless options.is_a? Hash
      @filter_options = {}

      options.each do |k, v|
        send("#{k}=", v)
      end
    end

    def filter_options
      @filter_options
    end

    private

    def scopes=(scopes)
      @scopes = scopes.map(&:to_s)
    end

    def if=(if_block)
      @filter_options[:if] = if_block
    end

    def unless=(unless_block)
      @filter_options[:unless] = unless_block
    end
  end

  class AllDoorkeeperFor < DoorkeeperFor
    private

    def except=(actions)
      @filter_options[:except] = actions
    end
  end

  class SelectedDoorkeeperFor < DoorkeeperFor
    def initialize(*args)
      options = args.pop if args.last.is_a? Hash
      super(options)
      only(args)
    end

    private

    def only(actions)
      @filter_options[:only] = actions
    end
  end

  class DoorkeeperForBuilder
    def self.create_doorkeeper_for(*args)
      case args.first
      when :all
        AllDoorkeeperFor.new(args[1] || {})
      when Hash, nil
        fail InvalidSyntax
      else
        SelectedDoorkeeperFor.new(*args)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
doorkeeper-1.4.2 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-1.4.1 lib/doorkeeper/doorkeeper_for.rb
doorkeeper-1.4.0 lib/doorkeeper/doorkeeper_for.rb