Sha256: 21e53adf33bd2f32db5a0a1593b1a10a134418c715a975e919a13c541c693331

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Fixturizer
  module Engines
    class Dataset
      attr_reader :name, :dataset

      def initialize(dataset:)
        @name = dataset
        @configuration = Fixturizer::Services.get.configuration
        @dataset = dataset.is_a?(Symbol) ? @configuration.datasets[@name] : dataset
        @effectives_rules = @dataset[:rules]
      end

      def generate
        data = @dataset[:definition]
        substitute_values(data)
      end

      private

      def apply_rule(key, value, rule)
        if @effectives_rules.key? key
          value = Fixturizer::Services.get.engine(name: :record, parameters: { value:, rule: }).apply
        end
        value
      end

      def substitute_values(obj)
        return obj.map { |v| substitute_values(v) } if obj.is_a? Array
        return obj unless obj.is_a? Hash

        obj.each_with_object({}) do |(key, value), result|
          result[key] = case value
                        when Hash
                          substitute_values(value)
                        when Array
                          value.map { |v| substitute_values(v) }
                        else
                          @effectives_rules.nil? ? value : apply_rule(key, value, @effectives_rules[key])
                        end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fixturizer-0.4.4 lib/fixturizer/engines/datasets.rb
fixturizer-0.4.3 lib/fixturizer/engines/datasets.rb