Skip to content

Refactoring: extract exposures. #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2015-05-21 22:47:03 +0700 using RuboCop version 0.31.0.
# on 2015-08-02 19:30:25 +0300 using RuboCop version 0.31.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 8
# Offense count: 6
Metrics/AbcSize:
Max: 53
Max: 33

# Offense count: 1
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 346
Max: 198

# Offense count: 5
# Offense count: 3
Metrics/CyclomaticComplexity:
Max: 17
Max: 11

# Offense count: 176
# Offense count: 208
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 146

# Offense count: 7
# Offense count: 8
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 37
Max: 28

# Offense count: 5
Metrics/PerceivedComplexity:
Max: 15
Max: 13

# Offense count: 31
# Offense count: 58
Style/Documentation:
Enabled: false

Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
0.5.0 (Next)
============
* [#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).
* [#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).
* [#151](https://github.com/intridea/grape-entity/pull/151): `.nested_exposures` is removed too - [@marshall-lee](https://github.com/marshall-lee).
* [#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).
* [#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).
* [#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).
* [#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).
* [#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).
* [#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).
* [#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).
* [#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).
* [#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).
* Your contribution here.

0.4.6 (2015-07-27)
Expand Down
2 changes: 2 additions & 0 deletions lib/grape_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
require 'grape_entity/version'
require 'grape_entity/entity'
require 'grape_entity/delegator'
require 'grape_entity/exposure'
require 'grape_entity/options'
26 changes: 26 additions & 0 deletions lib/grape_entity/condition.rb
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
35 changes: 35 additions & 0 deletions lib/grape_entity/condition/base.rb
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
21 changes: 21 additions & 0 deletions lib/grape_entity/condition/block_condition.rb
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
25 changes: 25 additions & 0 deletions lib/grape_entity/condition/hash_condition.rb
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
21 changes: 21 additions & 0 deletions lib/grape_entity/condition/symbol_condition.rb
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
Loading