Sha256: 73ee73e2c12790835a54da28893dca5107765073476cdbaac5e475c4ae392d65

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module RakeFactory
  module Values
    def self.resolve(value)
      case value
      when RakeFactory::DynamicValue, RakeFactory::StaticValue
        value
      else
        RakeFactory::StaticValue.new(value)
      end
    end

    module FactoryMethods
      def dynamic(&)
        DynamicValue.new(&)
      end

      def static(value)
        StaticValue.new(value)
      end
    end
  end

  class DynamicValue
    def initialize(pre_arguments = [], post_arguments = [], &block)
      @block = block
      @pre_arguments = pre_arguments
      @post_arguments = post_arguments
    end

    def prepend_argument(argument)
      self.class.new(
        [argument, *@pre_arguments], @post_arguments, &@block
      )
    end

    def append_argument(argument)
      self.class.new(
        @pre_arguments, [*@post_arguments, argument], &@block
      )
    end

    def evaluate(arguments)
      resolved_arguments = [*@pre_arguments, *arguments, *@post_arguments]
      @block.call(*resolved_arguments.slice(0, @block.arity))
    end
  end

  class StaticValue
    def initialize(value)
      @value = value
    end

    def prepend_argument(_)
      self
    end

    def append_argument(_)
      self
    end

    def evaluate(*_)
      @value
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rake_factory-0.34.0.pre.2 lib/rake_factory/values.rb
rake_factory-0.34.0.pre.1 lib/rake_factory/values.rb
rake_factory-0.33.0 lib/rake_factory/values.rb
rake_factory-0.32.0.pre.6 lib/rake_factory/values.rb