forked from ruby-grape/grape-entity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option :merge to expose. Allow to merge fields into nested hashes…
… or into the root. This also closes ruby-grape#138
- Loading branch information
Showing
7 changed files
with
128 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
lib/grape_entity/exposure/nesting_exposure/output_builder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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 | ||
else | ||
@output_hash[exposure.key] = result | ||
end | ||
|
||
end | ||
end | ||
|
||
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 | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters