Sha256: 5261bef1b94cbbd263c8c8681260f9a024dc155063fbdf6826ace2254cadd8fd

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require_relative './options_with_defaults'

module ArticleFixtureGen
  module Exe
    module Trollop
      # Adds modified attributes to lists produced by `OptionsWithDefaults`.
      class OptionsAndMods
        def self.call(all_specs)
          new(all_specs).call
        end

        def call
          container = OptionsWithDefaults.call(all_specs).to_hash
          container[:modified] = Internals.modified_from(container)
          Result.new container
        end

        protected

        def initialize(all_specs)
          @all_specs = all_specs
          self
        end

        private

        attr_reader :all_specs

        # Stateless methods
        module Internals
          def self.modified_from(container)
            _modified_attributes(container).reject do |attrib, _|
              [:help, :version].include? attrib
            end
          end

          def self._modified_attributes(container)
            container[:options].reject do |attrib, value|
              container[:defaults][attrib] == value
            end
          end
        end
        private_constant :Internals

        # Encapsulates configured options, default values, and modified options.
        class Result < Dry::Struct
          attribute :defaults, Types::Strict::Hash
          attribute :modified, Types::Strict::Hash
          attribute :options, Types::Strict::Hash
        end
        private_constant :Result
      end # class ArticleFixtureGen::Exe::Trollop::OptionsAndMods
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
article_fixture_gen-0.1.2 lib/article_fixture_gen/exe/option_parser/trollop/options_and_mods.rb
article_fixture_gen-0.1.1 lib/article_fixture_gen/exe/option_parser/trollop/options_and_mods.rb