File: condition.rb

package info (click to toggle)
ruby-grape-entity 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 456 kB
  • sloc: ruby: 3,335; makefile: 6
file content (35 lines) | stat: -rw-r--r-- 758 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true

require 'grape_entity/condition/base'
require 'grape_entity/condition/block_condition'
require 'grape_entity/condition/hash_condition'
require 'grape_entity/condition/symbol_condition'

module Grape
  class Entity
    module Condition
      class << self
        def new_if(arg)
          condition(false, arg)
        end

        def new_unless(arg)
          condition(true, arg)
        end

        private

        def condition(inverse, arg)
          condition_klass =
            case arg
            when Hash then HashCondition
            when Proc then BlockCondition
            when Symbol then SymbolCondition
            end

          condition_klass.new(inverse, arg)
        end
      end
    end
  end
end