module DataBuilder

Constants

VERSION

Attributes

data_contents[RW]
parent[R]

Public Class Methods

data_files_for(scenario) click to toggle source
# File lib/data_builder.rb, line 37
def data_files_for(scenario)
  tags = scenario.send(scenario.respond_to?(:tags) ? :tags : :source_tags)
  tags.map(&:name).select { |t| t =~ /@databuilder_/ }.map do |t|
    t.gsub('@databuilder_', '').to_sym
  end
end
Also aliased as: data_for_scenario
data_for_scenario(scenario)
Alias for: data_files_for
default_data_path() click to toggle source
# File lib/data_builder.rb, line 33
def default_data_path
  'data'
end
generators() click to toggle source
# File lib/data_builder.rb, line 50
def generators
  @generators || []
end
included(caller) click to toggle source
# File lib/data_builder.rb, line 23
def self.included(caller)
  @parent = caller
  generators.each do |generator|
    Generation.send :include, generator
  end
end
locale=(value) click to toggle source
# File lib/data_builder.rb, line 46
def locale=(value)
  Faker::Config.locale = value
end
use_in_scenario(scenario, data_location = DataBuilder.data_path) click to toggle source
# File lib/data_builder.rb, line 77
def self.use_in_scenario(scenario, data_location = DataBuilder.data_path)
  original_data_path = DataBuilder.data_path
  DataBuilder.data_path = data_location
  data_files = data_files_for(scenario)
  DataBuilder.load("#{data_files.last}.yml") if data_files.count.positive?
  DataBuilder.data_path = original_data_path
end

Public Instance Methods

data_about(key, specified = {}) click to toggle source
# File lib/data_builder.rb, line 55
def data_about(key, specified = {})
  if key.is_a?(String) && key.match(%r{/})
    file, record = key.split('/')
    DataBuilder.load("#{file}.yml")
  else
    record = key.to_s
    DataBuilder.load(builder_source) unless DataBuilder.data_contents
  end

  data = DataBuilder.data_contents[record]
  raise ArgumentError, "Undefined key for data: #{key}" unless data

  # rubocop:disable Metrics/LineLength
  process_data(data.merge(specified.key?(record) ? specified[record] : specified).deep_copy)
  # rubocop:enable Metrics/LineLength
end
data_for(key, specified = {})
Alias for: data_about
data_from(key, specified = {})
Alias for: data_about
using_data_for(key, specified = {})
Alias for: data_about
using_data_from(key, specified = {})
Alias for: data_about

Private Instance Methods

builder_source() click to toggle source
# File lib/data_builder.rb, line 109
def builder_source
  ENV['DATA_BUILDER_SOURCE'] || 'default.yml'
end
generate(value) click to toggle source
# File lib/data_builder.rb, line 99
def generate(value)
  generation.send :process, value
rescue StandardError => e
  fail "Failed to generate: #{value}\n Reason: #{e.message}\n"
end
generation() click to toggle source
# File lib/data_builder.rb, line 105
def generation
  @generation ||= Generation.new parent
end
process_data(data) click to toggle source
# File lib/data_builder.rb, line 87
def process_data(data)
  case data
    when Hash
      data.each { |key, value| data[key] = process_data(value) }
    when Array
      data.each_with_index { |value, i| data[i] = process_data(value) }
    when String
      return generate(data[1..-1]) if data[0, 1] == "~"
  end
  data
end