Skip to content

Commit 1b955d4

Browse files
committed
Some array handling micro optimizations
1 parent 20298ea commit 1b955d4

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

lib/grape_entity/entity.rb

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,8 @@ def exposures
399399
end
400400

401401
def valid_exposures
402-
exposures.reject { |_, options| options[:nested] }.select do |attribute, exposure_options|
403-
valid_exposure?(attribute, exposure_options)
402+
exposures.select do |attribute, exposure_options|
403+
!exposure_options[:nested] && valid_exposure?(attribute, exposure_options)
404404
end
405405
end
406406

@@ -432,7 +432,7 @@ def serializable_hash(runtime_options = {})
432432
output[self.class.key_for(attribute)] =
433433
if partial_output.respond_to?(:serializable_hash)
434434
partial_output.serializable_hash(runtime_options)
435-
elsif partial_output.is_a?(Array) && !partial_output.map { |o| o.respond_to?(:serializable_hash) }.include?(false)
435+
elsif partial_output.is_a?(Array) && partial_output.all? { |o| o.respond_to?(:serializable_hash) }
436436
partial_output.map(&:serializable_hash)
437437
elsif partial_output.is_a?(Hash)
438438
partial_output.each do |key, value|
@@ -518,13 +518,24 @@ def self.key_for(attribute)
518518
exposures[attribute.to_sym][:as] || name_for(attribute)
519519
end
520520

521-
def self.nested_exposures_for(attribute)
522-
nested_exposures[attribute] || {}
521+
def self.nested_exposures_for?(attribute)
522+
nested_exposures.key?(attribute)
523+
end
524+
525+
def nested_value_for(attribute, options)
526+
nested_exposures = self.class.nested_exposures[attribute]
527+
nested_attributes =
528+
nested_exposures.map do |nested_attribute, nested_exposure_options|
529+
if conditions_met?(nested_exposure_options, options)
530+
[self.class.key_for(nested_attribute), value_for(nested_attribute, options)]
531+
end
532+
end
533+
534+
Hash[nested_attributes.compact]
523535
end
524536

525537
def value_for(attribute, options = {})
526538
exposure_options = exposures[attribute.to_sym]
527-
nested_exposures = self.class.nested_exposures_for(attribute)
528539

529540
if exposure_options[:using]
530541
exposure_options[:using] = exposure_options[:using].constantize if exposure_options[:using].respond_to? :constantize
@@ -551,15 +562,8 @@ def value_for(attribute, options = {})
551562
instance_exec(delegate_attribute(attribute), &format_with)
552563
end
553564

554-
elsif nested_exposures.any?
555-
nested_attributes =
556-
nested_exposures.map do |nested_attribute, nested_exposure_options|
557-
if conditions_met?(nested_exposure_options, options)
558-
[self.class.key_for(nested_attribute), value_for(nested_attribute, options)]
559-
end
560-
end
561-
562-
Hash[nested_attributes.compact]
565+
elsif self.class.nested_exposures_for?(attribute)
566+
nested_value_for(attribute, options)
563567
else
564568
delegate_attribute(attribute)
565569
end
@@ -585,8 +589,7 @@ def delegate_attribute(attribute)
585589
end
586590

587591
def valid_exposure?(attribute, exposure_options)
588-
nested_exposures = self.class.nested_exposures_for(attribute)
589-
(nested_exposures.any? && nested_exposures.all? { |a, o| valid_exposure?(a, o) }) || \
592+
(self.class.nested_exposures_for?(attribute) && self.class.nested_exposures[attribute].all? { |a, o| valid_exposure?(a, o) }) || \
590593
exposure_options.key?(:proc) || \
591594
!exposure_options[:safe] || \
592595
object.respond_to?(self.class.name_for(attribute)) || \

0 commit comments

Comments
 (0)