Sha256: 7188bb4aa6c1294624394043e2c34ca3aabf861b95dae9498d0ae49fd0de470d

Contents?: true

Size: 962 Bytes

Versions: 1

Compression:

Stored size: 962 Bytes

Contents

#!/usr/bin/env ruby

require 'irb'
require 'irb/completion'
require 'awesome_print'
require 'bundler/setup'
Bundler.require

class Truck < ContractValueObject
  class Wheel < ContractValueObject
    attributes(
      size: Optional[Enum[:xs, :s, :m, :l, :xl]],
    )

    defaults(
      size: :m,
    )
  end

  attributes(
    color: Enum[:red, :white, :blue],
    model: String,
    wheels: ArrayOf[Wheel],
  )

  private

  def wheels=(wheels)
    # transform all the inputs into wheels
    if wheels.all? { |wheel| wheel.is_a?(Symbol) }
      wheels = wheels.map do |size|
        Wheel.new(size: size)
      end
    end

    # make sure that wheels behave rationally
    raise ArgumentError, 'All of the wheels should be the same size' if wheels.uniq(&:size).count != 1

    @wheels = wheels
  end
end

TRUCK = Truck.new(
  wheels: [Truck::Wheel.new, Truck::Wheel.new, Truck::Wheel.new, Truck::Wheel.new],
  color: :red,
  model: 'Ford F150',
)

IRB.start

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
contract_value_object-0.1.0 bin/console