Skip to content

Commit

Permalink
Refactoring: extract exposures.
Browse files Browse the repository at this point in the history
This one reaches many goals at once:

 - Fixes ruby-grape#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 ruby-grape#149: runtime `options` are now wrapped in an `Options` object.
 - Fixes ruby-grape#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%.
  • Loading branch information
marshall-lee committed Jul 21, 2015
1 parent a4aa641 commit edb8467
Show file tree
Hide file tree
Showing 18 changed files with 737 additions and 282 deletions.
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
31 changes: 31 additions & 0 deletions lib/grape_entity/condition/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Grape
class Entity
module Condition
class Base
def self.new(inverse, *args, &block)
super(inverse).tap { |e| e.setup_options(*args, &block) }
end

def initialize(inverse = false)
@inverse = inverse
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
17 changes: 17 additions & 0 deletions lib/grape_entity/condition/block_condition.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Grape
class Entity
module Condition
class BlockCondition < Base
attr_reader :block

def setup_options(&block)
@block = block
end

def if_value(entity, options)
entity.exec_with_object(options, &@block)
end
end
end
end
end
21 changes: 21 additions & 0 deletions lib/grape_entity/condition/hash_condition.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Grape
class Entity
module Condition
class HashCondition < Base
attr_reader :cond_hash

def setup_options(cond_hash)
@cond_hash = 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
17 changes: 17 additions & 0 deletions lib/grape_entity/condition/symbol_condition.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Grape
class Entity
module Condition
class SymbolCondition < Base
attr_reader :symbol

def setup_options(symbol)
@symbol = symbol
end

def if_value(_entity, options)
options[symbol]
end
end
end
end
end
Loading

0 comments on commit edb8467

Please sign in to comment.