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.
This one reaches many goals at once: - Fixes ruby-grape#56. - `exposures` hash table is removed and substituted with `root_exposures` array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - Ones who want an old rewriting behavior should manually `unexpose`. - Fixes ruby-grape#112. - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb`. - Fixes ruby-grape#152. - Fixes ruby-grape#153. - Fixes ruby-grape#155. - Fixes ruby-grape#156. - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 15-30%.
- Loading branch information
1 parent
7365266
commit c550978
Showing
22 changed files
with
1,285 additions
and
422 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'grape_entity/condition/base' | ||
require 'grape_entity/condition/block_condition' | ||
require 'grape_entity/condition/hash_condition' | ||
require 'grape_entity/condition/symbol_condition' | ||
|
||
module Grape | ||
class Entity | ||
module Condition | ||
def self.new_if(arg) | ||
case arg | ||
when Hash then HashCondition.new false, arg | ||
when Proc then BlockCondition.new false, &arg | ||
when Symbol then SymbolCondition.new false, arg | ||
end | ||
end | ||
|
||
def self.new_unless(arg) | ||
case arg | ||
when Hash then HashCondition.new true, arg | ||
when Proc then BlockCondition.new true, &arg | ||
when Symbol then SymbolCondition.new true, arg | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Grape | ||
class Entity | ||
module Condition | ||
class Base | ||
def self.new(inverse, *args, &block) | ||
super(inverse).tap { |e| e.setup(*args, &block) } | ||
end | ||
|
||
def initialize(inverse = false) | ||
@inverse = inverse | ||
end | ||
|
||
def ==(other) | ||
(self.class == other.class) && (self.inversed? == other.inversed?) | ||
end | ||
|
||
def inversed? | ||
@inverse | ||
end | ||
|
||
def met?(entity, options) | ||
!@inverse ? if_value(entity, options) : unless_value(entity, options) | ||
end | ||
|
||
def if_value(_entity, _options) | ||
fail NotImplementedError | ||
end | ||
|
||
def unless_value(entity, options) | ||
!if_value(entity, options) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Grape | ||
class Entity | ||
module Condition | ||
class BlockCondition < Base | ||
attr_reader :block | ||
|
||
def setup(&block) | ||
@block = block | ||
end | ||
|
||
def ==(other) | ||
super && @block == other.block | ||
end | ||
|
||
def if_value(entity, options) | ||
entity.exec_with_object(options, &@block) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Grape | ||
class Entity | ||
module Condition | ||
class HashCondition < Base | ||
attr_reader :cond_hash | ||
|
||
def setup(cond_hash) | ||
@cond_hash = cond_hash | ||
end | ||
|
||
def ==(other) | ||
super && @cond_hash == other.cond_hash | ||
end | ||
|
||
def if_value(_entity, options) | ||
@cond_hash.all? { |k, v| options[k.to_sym] == v } | ||
end | ||
|
||
def unless_value(_entity, options) | ||
@cond_hash.any? { |k, v| options[k.to_sym] != v } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Grape | ||
class Entity | ||
module Condition | ||
class SymbolCondition < Base | ||
attr_reader :symbol | ||
|
||
def setup(symbol) | ||
@symbol = symbol | ||
end | ||
|
||
def ==(other) | ||
super && @symbol == other.symbol | ||
end | ||
|
||
def if_value(_entity, options) | ||
options[symbol] | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.