Skip to content

Refactor and simplify various modules and classes #291

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
Jan 12, 2018
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#### Fixes

* [#288](https://github.com/ruby-grape/grape-entity/pull/288) Fix wrong argument exception when &:block passed to the expose method - [@DmitryTsepelev](https://github.com/DmitryTsepelev).

* [#291](https://github.com/ruby-grape/grape-entity/pull/291) Refactor and simplify various classes and modules
* Your contribution here.

### 0.6.1 (2017-01-09)
Expand Down
29 changes: 18 additions & 11 deletions lib/grape_entity/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@
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
class << self
def new_if(arg)
condition(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
def new_unless(arg)
condition(true, arg)
end

private

def condition(inverse, arg)
condition_klass =
case arg
when Hash then HashCondition
when Proc then BlockCondition
when Symbol then SymbolCondition
end

condition_klass.new(inverse, arg)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape_entity/condition/block_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Condition
class BlockCondition < Base
attr_reader :block

def setup(&block)
def setup(block)
@block = block
end

Expand Down
17 changes: 8 additions & 9 deletions lib/grape_entity/delegator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ module Grape
class Entity
module Delegator
def self.new(object)
if object.is_a?(Hash)
HashObject.new object
elsif defined?(OpenStruct) && object.is_a?(OpenStruct)
OpenStructObject.new object
elsif object.respond_to? :fetch, true
FetchableObject.new object
else
PlainObject.new object
end
delegator_klass =
if object.is_a?(Hash) then HashObject
elsif defined?(OpenStruct) && object.is_a?(OpenStruct) then OpenStructObject
elsif object.respond_to?(:fetch, true) then FetchableObject
else PlainObject
end

delegator_klass.new(object)
end
end
end
Expand Down
8 changes: 2 additions & 6 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,8 @@ def inspect

def initialize(object, options = {})
@object = object
@delegator = Delegator.new object
@options = if options.is_a? Options
options
else
Options.new options
end
@delegator = Delegator.new(object)
@options = options.is_a?(Options) ? options : Options.new(options)
end

def root_exposures
Expand Down
87 changes: 49 additions & 38 deletions lib/grape_entity/exposure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,74 @@
module Grape
class Entity
module Exposure
def self.new(attribute, options)
conditions = compile_conditions(options)
base_args = [attribute, options, conditions]
class << self
def new(attribute, options)
conditions = compile_conditions(options)
base_args = [attribute, options, conditions]

if options[:proc]
block_exposure = BlockExposure.new(*base_args, &options[:proc])
else
delegator_exposure = DelegatorExposure.new(*base_args)
end

if options[:using]
passed_proc = options[:proc]
using_class = options[:using]
format_with = options[:format_with]

if options[:proc]
RepresentExposure.new(*base_args, using_class, block_exposure)
if using_class
build_class_exposure(base_args, using_class, passed_proc)
elsif passed_proc
build_block_exposure(base_args, passed_proc)
elsif format_with
build_formatter_exposure(base_args, format_with)
elsif options[:nesting]
build_nesting_exposure(base_args)
else
RepresentExposure.new(*base_args, using_class, delegator_exposure)
build_delegator_exposure(base_args)
end
end

elsif options[:proc]
block_exposure
private

elsif options[:format_with]
format_with = options[:format_with]
def compile_conditions(options)
if_conditions = [
options[:if_extras],
options[:if]
].compact.flatten.map { |cond| Condition.new_if(cond) }

unless_conditions = [
options[:unless_extras],
options[:unless]
].compact.flatten.map { |cond| Condition.new_unless(cond) }

if_conditions + unless_conditions
end

def build_class_exposure(base_args, using_class, passed_proc)
exposure =
if passed_proc
build_block_exposure(base_args, passed_proc)
else
build_delegator_exposure(base_args)
end

RepresentExposure.new(*base_args, using_class, exposure)
end

def build_formatter_exposure(base_args, format_with)
if format_with.is_a? Symbol
FormatterExposure.new(*base_args, format_with)
elsif format_with.respond_to? :call
elsif format_with.respond_to?(:call)
FormatterBlockExposure.new(*base_args, &format_with)
end
end

elsif options[:nesting]
def build_nesting_exposure(base_args)
NestingExposure.new(*base_args)

else
delegator_exposure
end
end

def self.compile_conditions(options)
if_conditions = []
if_conditions.concat(options[:if_extras]) unless options[:if_extras].nil?
if_conditions << options[:if] unless options[:if].nil?

if_conditions.map! do |cond|
Condition.new_if cond
def build_block_exposure(base_args, passed_proc)
BlockExposure.new(*base_args, &passed_proc)
end

unless_conditions = []
unless_conditions.concat(options[:unless_extras]) unless options[:unless_extras].nil?
unless_conditions << options[:unless] unless options[:unless].nil?

unless_conditions.map! do |cond|
Condition.new_unless cond
def build_delegator_exposure(base_args)
DelegatorExposure.new(*base_args)
end

if_conditions + unless_conditions
end
end
end
Expand Down
46 changes: 23 additions & 23 deletions lib/grape_entity/exposure/nesting_exposure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,27 @@ def valid?(entity)
end

def value(entity, options)
new_options = nesting_options_for(options)
output = OutputBuilder.new(entity)

normalized_exposures(entity, new_options).each_with_object(output) do |exposure, out|
exposure.with_attr_path(entity, new_options) do
result = exposure.value(entity, new_options)
out.add(exposure, result)
end
map_entity_exposures(entity, options) do |exposure, nested_options|
exposure.value(entity, nested_options)
end
end

def valid_value_for(key, entity, options)
new_options = nesting_options_for(options)

result = nil
normalized_exposures(entity, new_options).select { |e| e.key(entity) == key }.each do |exposure|
exposure.with_attr_path(entity, new_options) do
result = exposure.valid_value(entity, new_options)
end
def serializable_value(entity, options)
map_entity_exposures(entity, options) do |exposure, nested_options|
exposure.serializable_value(entity, nested_options)
end
result
end

def serializable_value(entity, options)
def valid_value_for(key, entity, options)
new_options = nesting_options_for(options)
output = OutputBuilder.new(entity)

normalized_exposures(entity, new_options).each_with_object(output) do |exposure, out|
key_exposures = normalized_exposures(entity, new_options).select { |e| e.key(entity) == key }

key_exposures.map do |exposure|
exposure.with_attr_path(entity, new_options) do
result = exposure.serializable_value(entity, new_options)
out.add(exposure, result)
exposure.valid_value(entity, new_options)
end
end
end.last
end

# if we have any nesting exposures with the same name.
Expand Down Expand Up @@ -122,6 +110,18 @@ def normalized_exposures(entity, options)
end
end
end

def map_entity_exposures(entity, options)
new_options = nesting_options_for(options)
output = OutputBuilder.new(entity)

normalized_exposures(entity, new_options).each_with_object(output) do |exposure, out|
exposure.with_attr_path(entity, new_options) do
result = yield(exposure, new_options)
out.add(exposure, result)
end
end
end
end
end
end
Expand Down
Loading