Skip to content

Commit 079993f

Browse files
committed
Merge pull request #151 from marshall-lee/exposures
Refactoring: extract exposures.
2 parents 7365266 + c550978 commit 079993f

22 files changed

+1285
-422
lines changed

.rubocop_todo.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
# This configuration was generated by `rubocop --auto-gen-config`
2-
# on 2015-05-21 22:47:03 +0700 using RuboCop version 0.31.0.
2+
# on 2015-08-02 19:30:25 +0300 using RuboCop version 0.31.0.
33
# The point is for the user to remove these configuration records
44
# one by one as the offenses are removed from the code base.
55
# Note that changes in the inspected code, or installation of new
66
# versions of RuboCop, may require this file to be generated again.
77

8-
# Offense count: 8
8+
# Offense count: 6
99
Metrics/AbcSize:
10-
Max: 53
10+
Max: 33
1111

12-
# Offense count: 1
12+
# Offense count: 2
1313
# Configuration parameters: CountComments.
1414
Metrics/ClassLength:
15-
Max: 346
15+
Max: 198
1616

17-
# Offense count: 5
17+
# Offense count: 3
1818
Metrics/CyclomaticComplexity:
19-
Max: 17
19+
Max: 11
2020

21-
# Offense count: 176
21+
# Offense count: 208
2222
# Configuration parameters: AllowURI, URISchemes.
2323
Metrics/LineLength:
2424
Max: 146
2525

26-
# Offense count: 7
26+
# Offense count: 8
2727
# Configuration parameters: CountComments.
2828
Metrics/MethodLength:
29-
Max: 37
29+
Max: 28
3030

3131
# Offense count: 5
3232
Metrics/PerceivedComplexity:
33-
Max: 15
33+
Max: 13
3434

35-
# Offense count: 31
35+
# Offense count: 58
3636
Style/Documentation:
3737
Enabled: false
3838

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
0.5.0 (Next)
22
============
33
* [#139](https://github.com/intridea/grape-entity/pull/139): Keep a track of attribute nesting path during condition check or runtime exposure - [@calfzhou](https://github.com/calfzhou).
4+
* [#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).
5+
* [#151](https://github.com/intridea/grape-entity/pull/151): `.nested_exposures` is removed too - [@marshall-lee](https://github.com/marshall-lee).
6+
* [#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).
7+
* [#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).
8+
* [#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).
9+
* [#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).
10+
* [#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).
11+
* [#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).
12+
* [#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).
13+
* [#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).
14+
* [#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).
415
* Your contribution here.
516

617
0.4.6 (2015-07-27)

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)