Skip to content

Commit edb8467

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. - For ones who want previous rewriting behavior a `rewrite` option for `exposure` is introduced. - Fixes #149: runtime `options` are now wrapped in an `Options` object. - Fixes #150: see new `spec/grape_entity/exposure_spec.rb` - 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 edb8467

18 files changed

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

0 commit comments

Comments
 (0)