# frozen_string_literal: true require 'prolog/dry_types' require_relative './constants' # Generate blog post/article fixture data, with embedded marker tag pairs. module ArticleFixtureGen # Maintain configuration information for app. class Config # Builds hash with correct keys based on input. What's the value of this, # you ask? Two things: # 1. It defines `*_given` entries which are `true` if their corresponding # entries are specified, whether those other entries are truthy or not; # 2. It confirms filtering of the input hash, offering a guarantee that any # entry accepted is one supported elsehwere in our code. # # And yes, this class is *awfully* procedural. Got ideas for a fix? We'd # appreciate your PR! class Builder def self.call(options_hash) new.call options_hash end def call(options) @options = options hashes end private attr_reader :options def config_hash value = options[:config] given = Internals.string_given?(value) { config_given: given }.tap do |ret| ret[:config] = value || '' end end def generate_config_hash value = options[:generate_config] given = Internals.string_given?(value) { generate_config_given: given }.tap do |ret| ret[:generate_config] = value || '' end end def hashes items = hash_items {}.tap do |ret| items.each { |item| ret.merge! item } end end def hash_items items = [:config_hash, :generate_config_hash, :other_hash] items.map { |sym| send(sym) } end def other_hash ret = Internals.other_specified_values(options) ret.merge! Internals.defaults_not_in(ret) end # Stateless methods. module Internals def self.defaults_not_in(hash) keys = _other_keys_not_in(hash) _defaults_for(keys) end def self.other_specified_values(options) keys = _other_hash_keys options.select { |key, _| keys.include? key } end def self.string_given?(value) !value.to_s.empty? end def self._defaults_for(keys) Constants::DEFAULTS.select { |key| keys.include? key } end def self._other_hash_keys [:article_count, :para_count_max, :para_count_min, :sent_count_max, :sent_count_min, :pmtp_count, :pmtp_text, :smtp_count, :smtp_text] end def self._other_keys_not_in(hash) _other_hash_keys.reject { |key| hash.key? key } end end private_constant :Internals end # class ArticleFixtureGen::Config::Builder end # class ArticleFixtureGen::Config end