Skip to content

Commit

Permalink
Fixed rubocop offences
Browse files Browse the repository at this point in the history
  • Loading branch information
etki committed Jul 30, 2017
1 parent a88c7c2 commit 6dbb585
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
47 changes: 20 additions & 27 deletions lib/mapper/engine.rb
Expand Up @@ -110,13 +110,11 @@ def recursive_map(source, types, context)

# @param [Object] source
# @param [AMA::Entity::Mapper::Type] type
# @param [AMA::Entity::Mapper::Engine::Context] context
def try_map(source, type, context)
unless type.valid?(source, context)
source = reassemble(source, type, context)
end
result = map_attributes(source, type, context)
type.valid!(result, context)
# @param [AMA::Entity::Mapper::Engine::Context] ctx
def try_map(source, type, ctx)
source = reassemble(source, type, ctx) unless type.valid?(source, ctx)
result = map_attributes(source, type, ctx)
type.valid!(result, ctx)
result
end

Expand All @@ -138,17 +136,7 @@ def reassemble(source, type, context)
# @param [AMA::Entity::Mapper::Type] type
# @param [AMA::Entity::Mapper::Engine::Context] context
def map_attributes(entity, type, context)
enumerator = type.enumerator.enumerate(entity, type, context)
return entity if enumerator.none? { true }
changes = enumerator.map do |attribute, value, segment = nil|
if value.nil? && attribute.nullable
next [false, attribute, value, segment]
end
next_context = segment ? context.advance(segment) : context
mutated = recursive_map(value, attribute.types, next_context)
[!value.equal?(mutated), attribute, mutated, segment]
end
changes = changes.reject(&:nil?)
changes = mutate_attributes(entity, type, context)
return entity if changes.select(&:first).empty?
instance = type.factory.create(type, entity, context)
changes.map do |_, attribute, value, segment = nil|
Expand All @@ -158,17 +146,22 @@ def map_attributes(entity, type, context)
instance
end

def normalize_types(types, context)
if types.empty?
compliance_error('Requested map operation with no target types')
end
types = types.map do |type|
@resolver.resolve(type)
end
types.each do |type|
type.resolved!(context)
# Returns attributes in [mutated?, attr, value, segment] format
def mutate_attributes(entity, type, context)
enumerator = type.enumerator.enumerate(entity, type, context)
enumerator.map do |attr, value, segment = nil|
next [false, attr, value, segment] if value.nil? && attr.nullable
next_context = segment ? context.advance(segment) : context
mutated = recursive_map(value, attr.types, next_context)
[!value.equal?(mutated), attr, mutated, segment]
end
end

def normalize_types(types, context)
compliance_error('Called #map() with no types') if types.empty?
types = types.map { |type| @resolver.resolve(type) }
types.each { |type| type.resolved!(context) }
end
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions test/suite/unit/mapper/type/type.spec.rb
Expand Up @@ -5,7 +5,6 @@
require_relative '../../../../../lib/mapper/exception/validation_error'

inspected_class = ::AMA::Entity::Mapper::Type
mapping_error_class = ::AMA::Entity::Mapper::Exception::MappingError
validation_error_class = ::AMA::Entity::Mapper::Exception::ValidationError

describe inspected_class do
Expand Down Expand Up @@ -134,7 +133,7 @@ def to_s
expect(&proc).not_to raise_error
end

it 'should raise mapping error if #instance? returns false' do
it 'should raise validation error if #instance? returns false' do
allow(dummy).to receive(:instance?).and_return(false)
proc = lambda do
dummy.instance!(nil)
Expand Down

0 comments on commit 6dbb585

Please sign in to comment.