Skip to content

Commit 117a150

Browse files
Refactor and simplify various modules and classes
1 parent 03d3b92 commit 117a150

File tree

7 files changed

+138
-147
lines changed

7 files changed

+138
-147
lines changed

lib/grape_entity/condition.rb

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,26 @@
88
module Grape
99
class Entity
1010
module Condition
11-
def self.new_if(arg)
12-
case arg
13-
when Hash then HashCondition.new false, arg
14-
when Proc then BlockCondition.new false, &arg
15-
when Symbol then SymbolCondition.new false, arg
11+
class << self
12+
def new_if(arg)
13+
condition(false, arg)
1614
end
17-
end
1815

19-
def self.new_unless(arg)
20-
case arg
21-
when Hash then HashCondition.new true, arg
22-
when Proc then BlockCondition.new true, &arg
23-
when Symbol then SymbolCondition.new true, arg
16+
def new_unless(arg)
17+
condition(true, arg)
18+
end
19+
20+
private
21+
22+
def condition(inverse, arg)
23+
condition_klass =
24+
case arg
25+
when Hash then HashCondition
26+
when Proc then BlockCondition
27+
when Symbol then SymbolCondition
28+
end
29+
30+
condition_klass.new(inverse, arg)
2431
end
2532
end
2633
end

lib/grape_entity/condition/block_condition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Condition
66
class BlockCondition < Base
77
attr_reader :block
88

9-
def setup(&block)
9+
def setup(block)
1010
@block = block
1111
end
1212

lib/grape_entity/delegator.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ module Grape
1010
class Entity
1111
module Delegator
1212
def self.new(object)
13-
if object.is_a?(Hash)
14-
HashObject.new object
15-
elsif defined?(OpenStruct) && object.is_a?(OpenStruct)
16-
OpenStructObject.new object
17-
elsif object.respond_to? :fetch, true
18-
FetchableObject.new object
19-
else
20-
PlainObject.new object
21-
end
13+
delegator_klass =
14+
if object.is_a?(Hash) then HashObject
15+
elsif defined?(OpenStruct) && object.is_a?(OpenStruct) then OpenStructObject
16+
elsif object.respond_to?(:fetch, true) then FetchableObject
17+
else PlainObject
18+
end
19+
20+
delegator_klass.new(object)
2221
end
2322
end
2423
end

lib/grape_entity/entity.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,8 @@ def inspect
451451

452452
def initialize(object, options = {})
453453
@object = object
454-
@delegator = Delegator.new object
455-
@options = if options.is_a? Options
456-
options
457-
else
458-
Options.new options
459-
end
454+
@delegator = Delegator.new(object)
455+
@options = options.is_a?(Options) ? options: Options.new(options)
460456
end
461457

462458
def root_exposures

lib/grape_entity/exposure.rb

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,63 +12,74 @@
1212
module Grape
1313
class Entity
1414
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]
1819

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]
2621
using_class = options[:using]
22+
format_with = options[:format_with]
2723

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)
3032
else
31-
RepresentExposure.new(*base_args, using_class, delegator_exposure)
33+
build_delegator_exposure(base_args)
3234
end
35+
end
3336

34-
elsif options[:proc]
35-
block_exposure
37+
private
3638

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
3960

61+
RepresentExposure.new(*base_args, using_class, exposure)
62+
end
63+
64+
def build_formatter_exposure(base_args, format_with)
4065
if format_with.is_a? Symbol
4166
FormatterExposure.new(*base_args, format_with)
42-
elsif format_with.respond_to? :call
67+
elsif format_with.respond_to?(:call)
4368
FormatterBlockExposure.new(*base_args, &format_with)
4469
end
70+
end
4571

46-
elsif options[:nesting]
72+
def build_nesting_exposure(base_args)
4773
NestingExposure.new(*base_args)
48-
49-
else
50-
delegator_exposure
5174
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?
5875

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)
6178
end
6279

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)
6982
end
70-
71-
if_conditions + unless_conditions
7283
end
7384
end
7485
end

lib/grape_entity/exposure/nesting_exposure.rb

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,27 @@ def valid?(entity)
3131
end
3232

3333
def value(entity, options)
34-
new_options = nesting_options_for(options)
35-
output = OutputBuilder.new(entity)
36-
37-
normalized_exposures(entity, new_options).each_with_object(output) do |exposure, out|
38-
exposure.with_attr_path(entity, new_options) do
39-
result = exposure.value(entity, new_options)
40-
out.add(exposure, result)
41-
end
34+
map_entity_exposures(entity, options) do |exposure, nested_options|
35+
exposure.value(entity, nested_options)
4236
end
4337
end
4438

45-
def valid_value_for(key, entity, options)
46-
new_options = nesting_options_for(options)
47-
48-
result = nil
49-
normalized_exposures(entity, new_options).select { |e| e.key(entity) == key }.each do |exposure|
50-
exposure.with_attr_path(entity, new_options) do
51-
result = exposure.valid_value(entity, new_options)
52-
end
39+
def serializable_value(entity, options)
40+
map_entity_exposures(entity, options) do |exposure, nested_options|
41+
exposure.serializable_value(entity, nested_options)
5342
end
54-
result
5543
end
5644

57-
def serializable_value(entity, options)
45+
def valid_value_for(key, entity, options)
5846
new_options = nesting_options_for(options)
59-
output = OutputBuilder.new(entity)
6047

61-
normalized_exposures(entity, new_options).each_with_object(output) do |exposure, out|
48+
key_exposures = normalized_exposures(entity, new_options).select { |e| e.key(entity) == key }
49+
50+
key_exposures.map do |exposure|
6251
exposure.with_attr_path(entity, new_options) do
63-
result = exposure.serializable_value(entity, new_options)
64-
out.add(exposure, result)
52+
exposure.valid_value(entity, new_options)
6553
end
66-
end
54+
end.last
6755
end
6856

6957
# if we have any nesting exposures with the same name.
@@ -122,6 +110,18 @@ def normalized_exposures(entity, options)
122110
end
123111
end
124112
end
113+
114+
def map_entity_exposures(entity, options)
115+
new_options = nesting_options_for(options)
116+
output = OutputBuilder.new(entity)
117+
118+
normalized_exposures(entity, new_options).each_with_object(output) do |exposure, out|
119+
exposure.with_attr_path(entity, new_options) do
120+
result = yield(exposure, new_options)
121+
out.add(exposure, result)
122+
end
123+
end
124+
end
125125
end
126126
end
127127
end

0 commit comments

Comments
 (0)