Skip to content

Commit aaad470

Browse files
committed
Refactoring: extract exposures.
This one reaches many goals at once: - Fixes #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 #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. - Fixes #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 20-30%. Version is bumbed to 0.5.0.
1 parent a4aa641 commit aaad470

20 files changed

+1114
-376
lines changed

CHANGELOG.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@
77
* [#134](https://github.com/intridea/grape-entity/pull/134): Subclasses no longer affected in all cases by `unexpose` in parent - [@etehtsea](https://github.com/etehtsea).
88
* [#135](https://github.com/intridea/grape-entity/pull/135): Added `except` option - [@dan-corneanu](https://github.com/dan-corneanu).
99
* [#136](https://github.com/intridea/grape-entity/pull/136): Allow for strings in `only` and `except` options - [@bswinnerton](https://github.com/bswinnerton).
10-
* [#147](https://github.com/intridea/grape-entity/pull/147): Expose `safe` attributes as `nil` if they cannot be evaluated: [#140](https://github.com/intridea/grape-entity/issues/140).
11-
* [#147](https://github.com/intridea/grape-entity/pull/147): Fix: private method values were not exposed with `safe` option: [#142](https://github.com/intridea/grape-entity/pull/142).
12-
* [#147](https://github.com/intridea/grape-entity/pull/147): Remove catching of `NoMethodError` because it can occur deep inside in a method call so this exception does not mean that attribute not exist.
13-
* [#147](https://github.com/intridea/grape-entity/pull/147): `valid_exposures` is removed.
10+
* [#147](https://github.com/intridea/grape-entity/pull/147): Expose `safe` attributes as `nil` if they cannot be evaluated: [#140](https://github.com/intridea/grape-entity/issues/140) - [@marshall-lee](https://github.com/marshall-lee).
11+
* [#147](https://github.com/intridea/grape-entity/pull/147): Fix: private method values were not exposed with `safe` option: [#142](https://github.com/intridea/grape-entity/pull/142) - [@marshall-lee](https://github.com/marshall-lee).
12+
* [#147](https://github.com/intridea/grape-entity/pull/147): Remove catching of `NoMethodError` because it can occur deep inside in a method call so this exception does not mean that attribute not exist - [@marshall-lee](https://github.com/marshall-lee).
13+
* [#147](https://github.com/intridea/grape-entity/pull/147): `valid_exposures` is removed - [@marshall-lee](https://github.com/marshall-lee).
14+
* [#151](https://github.com/intridea/grape-entity/pull/151): `.exposures` is removed and substituted with `.root_exposures` array - [@marshall-lee](https://github.com/marshall-lee).
15+
* [#151](https://github.com/intridea/grape-entity/pull/151): `.nested_exposures` is removed too - [@marshall-lee](https://github.com/marshall-lee).
16+
* [#151](https://github.com/intridea/grape-entity/pull/151): `#should_return_attribute?`, `#only_fields` and `#except_fields` are moved to other classes - [@marshall-lee](https://github.com/marshall-lee).
17+
* [#151](https://github.com/intridea/grape-entity/pull/151): Fix: double exposures with conditions does not rewrite previously defined now: [#56](https://github.com/intridea/grape-entity/issues/56) - [@marshall-lee](https://github.com/marshall-lee).
18+
* [#151](https://github.com/intridea/grape-entity/pull/151): Fix: nested exposures were flattened in `.documentation`: [#112](https://github.com/intridea/grape-entity/issues/112) - [@marshall-lee](https://github.com/marshall-lee).
19+
* [#151](https://github.com/intridea/grape-entity/pull/151): Fix: `@only_fields` and `@except_fields` memoization: [#149](https://github.com/intridea/grape-entity/issues/149) - [@marshall-lee](https://github.com/marshall-lee).
20+
* [#151](https://github.com/intridea/grape-entity/pull/151): Fix: `:unless` condition with `Hash` argument logic: [#150](https://github.com/intridea/grape-entity/issues/150) - [@marshall-lee](https://github.com/marshall-lee).
21+
* [#151](https://github.com/intridea/grape-entity/pull/151): Nested `unexpose` now raises an exception: [#152](https://github.com/intridea/grape-entity/issues/152) - [@marshall-lee](https://github.com/marshall-lee).
22+
* [#151](https://github.com/intridea/grape-entity/pull/151): Fix: `@documentation` memoization: [#153](https://github.com/intridea/grape-entity/issues/153) - [@marshall-lee](https://github.com/marshall-lee).
23+
* [#151](https://github.com/intridea/grape-entity/pull/151): Fix: serializing of deeply nested presenter exposures: [#155](https://github.com/intridea/grape-entity/issues/155) - [@marshall-lee](https://github.com/marshall-lee).
24+
* [#151](https://github.com/intridea/grape-entity/pull/151): Fix: deep projections (`:only`, `:except`) were unaware of nesting: [#156](https://github.com/intridea/grape-entity/issues/156) - [@marshall-lee](https://github.com/marshall-lee).
1425
* Your contribution here.
1526

1627
0.4.5 (2015-03-10)

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)