Sha256: b5021b53c2d9ecf899504f426a790b9a8d50897bd769b8d9880e1ea4d4df53ff

Contents?: true

Size: 1.26 KB

Versions: 30

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

class ReeMapper::MapperStrategy
  attr_reader :method, :always_optional

  contract(Symbol, ReeMapper::StrategyOutput, Bool => Any)
  def initialize(method:, output:, always_optional:)
    @method          = method
    @output          = output
    @always_optional = always_optional
  end

  contract None => ReeMapper::StrategyOutput
  def output
    @output
  end

  contract ReeMapper::StrategyOutput => ReeMapper::StrategyOutput
  def output=(output)
    @output = output
  end

  contract(Any)
  def build_object
    output.build_object
  end

  contract(Any, ReeMapper::Field, Any => nil)
  def assign_value(object, field, value)
    output.assign_value(object, field, value)
  end

  contract(Any, ReeMapper::Field => Bool)
  def has_value?(obj, field)
    if obj.is_a?(Hash)
      obj.key?(field.from) || obj.key?(field.from_as_str)
    else
      obj.respond_to?(field.from)
    end
  end

  contract(Any, ReeMapper::Field => Any)
  def get_value(obj, field)
    if obj.is_a?(Hash)
      obj.key?(field.from) ? obj[field.from] : obj[field.from_as_str]
    else
      obj.public_send(field.from)
    end
  end

  contract(Class => Class)
  def dto=(dto)
    output.dto = dto
  end

  contract(None => Nilor[Class])
  def dto
    output.dto
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
ree_lib-1.0.9 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_strategy.rb
ree_lib-1.0.8 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_strategy.rb
ree_lib-1.0.7 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_strategy.rb
ree_lib-1.0.6 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_strategy.rb
ree_lib-1.0.5 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_strategy.rb
ree_lib-1.0.4 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_strategy.rb
ree_lib-1.0.3 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_strategy.rb
ree_lib-1.0.2 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_strategy.rb
ree_lib-1.0.1 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_strategy.rb
ree_lib-1.0.0 lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_strategy.rb