|
12 | 12 | module Grape |
13 | 13 | class Entity |
14 | 14 | module Exposure |
15 | | - def self.new(attribute, options) |
16 | | - conditions = compile_conditions(options) |
17 | | - base_args = [attribute, options, conditions] |
| 15 | + class << self |
| 16 | + def new(attribute, options) |
| 17 | + conditions = compile_conditions(options) |
| 18 | + base_args = [attribute, options, conditions] |
18 | 19 |
|
19 | | - if options[:proc] |
20 | | - block_exposure = BlockExposure.new(*base_args, &options[:proc]) |
21 | | - else |
22 | | - delegator_exposure = DelegatorExposure.new(*base_args) |
23 | | - end |
24 | | - |
25 | | - if options[:using] |
| 20 | + passed_proc = options[:proc] |
26 | 21 | using_class = options[:using] |
| 22 | + format_with = options[:format_with] |
27 | 23 |
|
28 | | - if options[:proc] |
29 | | - RepresentExposure.new(*base_args, using_class, block_exposure) |
| 24 | + if using_class |
| 25 | + build_class_exposure(base_args, using_class, passed_proc) |
| 26 | + elsif passed_proc |
| 27 | + build_block_exposure(base_args, passed_proc) |
| 28 | + elsif format_with |
| 29 | + build_formatter_exposure(base_args, format_with) |
| 30 | + elsif options[:nesting] |
| 31 | + build_nesting_exposure(base_args) |
30 | 32 | else |
31 | | - RepresentExposure.new(*base_args, using_class, delegator_exposure) |
| 33 | + build_delegator_exposure(base_args) |
32 | 34 | end |
| 35 | + end |
33 | 36 |
|
34 | | - elsif options[:proc] |
35 | | - block_exposure |
| 37 | + private |
36 | 38 |
|
37 | | - elsif options[:format_with] |
38 | | - format_with = options[:format_with] |
| 39 | + def compile_conditions(options) |
| 40 | + if_conditions = [ |
| 41 | + options[:if_extras], |
| 42 | + options[:if] |
| 43 | + ].compact.flatten.map { |cond| Condition.new_if(cond) } |
| 44 | + |
| 45 | + unless_conditions = [ |
| 46 | + options[:unless_extras], |
| 47 | + options[:unless] |
| 48 | + ].compact.flatten.map { |cond| Condition.new_unless(cond) } |
| 49 | + |
| 50 | + if_conditions + unless_conditions |
| 51 | + end |
| 52 | + |
| 53 | + def build_class_exposure(base_args, using_class, passed_proc) |
| 54 | + exposure = |
| 55 | + if passed_proc |
| 56 | + build_block_exposure(base_args, passed_proc) |
| 57 | + else |
| 58 | + build_delegator_exposure(base_args) |
| 59 | + end |
39 | 60 |
|
| 61 | + RepresentExposure.new(*base_args, using_class, exposure) |
| 62 | + end |
| 63 | + |
| 64 | + def build_formatter_exposure(base_args, format_with) |
40 | 65 | if format_with.is_a? Symbol |
41 | 66 | FormatterExposure.new(*base_args, format_with) |
42 | | - elsif format_with.respond_to? :call |
| 67 | + elsif format_with.respond_to?(:call) |
43 | 68 | FormatterBlockExposure.new(*base_args, &format_with) |
44 | 69 | end |
| 70 | + end |
45 | 71 |
|
46 | | - elsif options[:nesting] |
| 72 | + def build_nesting_exposure(base_args) |
47 | 73 | NestingExposure.new(*base_args) |
48 | | - |
49 | | - else |
50 | | - delegator_exposure |
51 | 74 | end |
52 | | - end |
53 | | - |
54 | | - def self.compile_conditions(options) |
55 | | - if_conditions = [] |
56 | | - if_conditions.concat(options[:if_extras]) unless options[:if_extras].nil? |
57 | | - if_conditions << options[:if] unless options[:if].nil? |
58 | 75 |
|
59 | | - if_conditions.map! do |cond| |
60 | | - Condition.new_if cond |
| 76 | + def build_block_exposure(base_args, passed_proc) |
| 77 | + BlockExposure.new(*base_args, &passed_proc) |
61 | 78 | end |
62 | 79 |
|
63 | | - unless_conditions = [] |
64 | | - unless_conditions.concat(options[:unless_extras]) unless options[:unless_extras].nil? |
65 | | - unless_conditions << options[:unless] unless options[:unless].nil? |
66 | | - |
67 | | - unless_conditions.map! do |cond| |
68 | | - Condition.new_unless cond |
| 80 | + def build_delegator_exposure(base_args) |
| 81 | + DelegatorExposure.new(*base_args) |
69 | 82 | end |
70 | | - |
71 | | - if_conditions + unless_conditions |
72 | 83 | end |
73 | 84 | end |
74 | 85 | end |
|
0 commit comments