Sha256: e22a8d352198a8901f4533a4701942adaeed488d5fffbdaa091a73ce3a932934

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

class Collector
  include Arstotzka

  MALE = 'male'
  FEMALE = 'female'

  attr_reader :hash

  expose :full_name, :age, path: :person, json: :hash
  expose :gender, path: :person, type: :gender, cached: true, json: :hash
  expose :car_names, flatten: true, compact: false, json: :hash,
                     default: 'MissingName',
                     full_path: 'collections.cars.units.nick_name'
  expose :finished_games, json: :hash,
                          flatten: true, class: Collector::Game,
                          after: :filter_finished, compact: true,
                          full_path: 'collections.games.titles'

  def initialize(hash = {})
    @hash = hash
  end

  private

  def filter_finished(games)
    games.select(&:finished?)
  end
end

module Arstotzka
  module TypeCast
    def to_gender(value)
      case value
      when 'man'
        Collector::MALE
      when 'woman'
        Collector::FEMALE
      else
        raise 'invalid gender'
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arstotzka-1.0.4 spec/support/models/collector.rb
arstotzka-1.0.3 spec/support/models/collector.rb