Skip to content

Commit 1dba59e

Browse files
committed
Refactoring: extract exposures.
This one reaches many goals at once: - Fixes #56: now `exposures` is not a hash table but an 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 #112 - Fixes #149: runtime `options` are now wrapped in an `Options` object. - Fixes #150: see new `spec/grape_entity/exposure_spec.rb` - Fixes #152 - Fixes #153 - Fixes #155 - 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 20-30%.
1 parent a4aa641 commit 1dba59e

18 files changed

+984
-371
lines changed

lib/grape_entity.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
require 'grape_entity/version'
44
require 'grape_entity/entity'
55
require 'grape_entity/delegator'
6+
require 'grape_entity/exposure'
7+
require 'grape_entity/options'

lib/grape_entity/condition.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'grape_entity/condition/base'
2+
require 'grape_entity/condition/block_condition'
3+
require 'grape_entity/condition/hash_condition'
4+
require 'grape_entity/condition/symbol_condition'
5+
6+
module Grape
7+
class Entity
8+
module Condition
9+
def self.new_if(arg)
10+
case arg
11+
when Hash then HashCondition.new false, arg
12+
when Proc then BlockCondition.new false, &arg
13+
when Symbol then SymbolCondition.new false, arg
14+
end
15+
end
16+
17+
def self.new_unless(arg)
18+
case arg
19+
when Hash then HashCondition.new true, arg
20+
when Proc then BlockCondition.new true, &arg
21+
when Symbol then SymbolCondition.new true, arg
22+
end
23+
end
24+
end
25+
end
26+
end

lib/grape_entity/condition/base.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Grape
2+
class Entity
3+
module Condition
4+
class Base
5+
def self.new(inverse, *args, &block)
6+
super(inverse).tap { |e| e.setup(*args, &block) }
7+
end
8+
9+
def initialize(inverse = false)
10+
@inverse = inverse
11+
end
12+
13+
def ==(other)
14+
(self.class == other.class) && (self.inversed? == other.inversed?)
15+
end
16+
17+
def inversed?
18+
@inverse
19+
end
20+
21+
def met?(entity, options)
22+
!@inverse ? if_value(entity, options) : unless_value(entity, options)
23+
end
24+
25+
def if_value(_entity, _options)
26+
fail NotImplementedError
27+
end
28+
29+
def unless_value(entity, options)
30+
!if_value(entity, options)
31+
end
32+
end
33+
end
34+
end
35+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Grape
2+
class Entity
3+
module Condition
4+
class BlockCondition < Base
5+
attr_reader :block
6+
7+
def setup(&block)
8+
@block = block
9+
end
10+
11+
def ==(other)
12+
super && @block == other.block
13+
end
14+
15+
def if_value(entity, options)
16+
entity.exec_with_object(options, &@block)
17+
end
18+
end
19+
end
20+
end
21+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Grape
2+
class Entity
3+
module Condition
4+
class HashCondition < Base
5+
attr_reader :cond_hash
6+
7+
def setup(cond_hash)
8+
@cond_hash = cond_hash
9+
end
10+
11+
def ==(other)
12+
super && @cond_hash == other.cond_hash
13+
end
14+
15+
def if_value(_entity, options)
16+
@cond_hash.all? { |k, v| options[k.to_sym] == v }
17+
end
18+
19+
def unless_value(_entity, options)
20+
@cond_hash.any? { |k, v| options[k.to_sym] != v }
21+
end
22+
end
23+
end
24+
end
25+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Grape
2+
class Entity
3+
module Condition
4+
class SymbolCondition < Base
5+
attr_reader :symbol
6+
7+
def setup(symbol)
8+
@symbol = symbol
9+
end
10+
11+
def ==(other)
12+
super && @symbol == other.symbol
13+
end
14+
15+
def if_value(_entity, options)
16+
options[symbol]
17+
end
18+
end
19+
end
20+
end
21+
end

0 commit comments

Comments
 (0)