Sha256: 3c4947b362cdb8546e2c269f911df3844ded6aaac808e5691035e6337795e00a
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 KB
Contents
module Grape class Entity module Exposure class NestingExposure class OutputBuilder < SimpleDelegator def initialize @output_hash = {} @output_collection = [] end def add(exposure, result) # Save a result array in collections' array if it should be merged if result.is_a?(Array) && exposure.for_merge @output_collection << result else # If we have an array which should not be merged - save it with a key as a hash # If we have hash which should be merged - save it without a key (merge) if exposure.for_merge @output_hash.merge! result, &merge_strategy(exposure.for_merge) else @output_hash[exposure.key] = result end end end def kind_of?(klass) klass == output.class || super end alias_method :is_a?, :kind_of? def __getobj__ output end private # If output_collection contains at least one element we have to represent the output as a collection def output if @output_collection.empty? output = @output_hash else output = @output_collection output << @output_hash unless @output_hash.empty? output.flatten! end output end # In case if we want to solve collisions providing lambda to :merge option def merge_strategy(for_merge) if for_merge.respond_to? :call for_merge else -> {} end end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
grape-entity-0.5.2 | lib/grape_entity/exposure/nesting_exposure/output_builder.rb |