Module: Grape::Entity::Exposure

Defined in:
lib/grape_entity/exposure.rb,
lib/grape_entity/exposure/base.rb,
lib/grape_entity/exposure/block_exposure.rb,
lib/grape_entity/exposure/nesting_exposure.rb,
lib/grape_entity/exposure/delegator_exposure.rb,
lib/grape_entity/exposure/formatter_exposure.rb,
lib/grape_entity/exposure/represent_exposure.rb,
lib/grape_entity/exposure/formatter_block_exposure.rb,
lib/grape_entity/exposure/nesting_exposure/output_builder.rb,
lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb

Defined Under Namespace

Classes: Base, BlockExposure, DelegatorExposure, FormatterBlockExposure, FormatterExposure, NestingExposure, RepresentExposure

Class Method Summary collapse

Class Method Details

.compile_conditions(options) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/grape_entity/exposure.rb', line 54

def self.compile_conditions(options)
  if_conditions = []
  unless options[:if_extras].nil?
    if_conditions.concat(options[:if_extras])
  end
  if_conditions << options[:if] unless options[:if].nil?

  if_conditions.map! do |cond|
    Condition.new_if cond
  end

  unless_conditions = []
  unless options[:unless_extras].nil?
    unless_conditions.concat(options[:unless_extras])
  end
  unless_conditions << options[:unless] unless options[:unless].nil?

  unless_conditions.map! do |cond|
    Condition.new_unless cond
  end

  if_conditions + unless_conditions
end

.new(attribute, options) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/grape_entity/exposure.rb', line 15

def self.new(attribute, options)
  conditions = compile_conditions(options)
  base_args = [attribute, options, conditions]

  if options[:proc]
    block_exposure = BlockExposure.new(*base_args, &options[:proc])
  else
    delegator_exposure = DelegatorExposure.new(*base_args)
  end

  if options[:using]
    using_class = options[:using]

    if options[:proc]
      RepresentExposure.new(*base_args, using_class, block_exposure)
    else
      RepresentExposure.new(*base_args, using_class, delegator_exposure)
    end

  elsif options[:proc]
    block_exposure

  elsif options[:format_with]
    format_with = options[:format_with]

    if format_with.is_a? Symbol
      FormatterExposure.new(*base_args, format_with)
    elsif format_with.respond_to? :call
      FormatterBlockExposure.new(*base_args, &format_with)
    end

  elsif options[:nesting]
    NestingExposure.new(*base_args)

  else
    delegator_exposure
  end
end