Sha256: 0ff5696c11ff776aa03ab6ec621407a1e5c0f5e9f36a6d33fb35c5cf5a63b3bc

Contents?: true

Size: 644 Bytes

Versions: 4

Compression:

Stored size: 644 Bytes

Contents

# frozen_string_literal: true

module Voucher
  class SegmentsBuilder
    def self.call(data)
      new(data).call
    end

    def initialize(data)
      @data = data
    end

    def call
      build
    end

    private

    def build
      { election: election, segments: segments }
    end

    def election
      attributes = @data.dig(:election, :attributes) || {}

      Election.new(attributes)
    end

    def segments
      raise ArgumentError, 'Segments data must be an array' unless @data[:segments].is_a?(Array)

      @data[:segments].map do |segment|
        Segment.new(segment[:attributes] || {})
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
voucher-0.3.0 lib/voucher/segments_builder.rb
voucher-0.2.1 lib/voucher/segments_builder.rb
voucher-0.1.1 lib/voucher/segments_builder.rb
voucher-0.1.0 lib/voucher/segments_builder.rb