-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from marshall-lee/exposures
Refactoring: extract exposures.
- Loading branch information
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.