Sha256: a30a57e11a9ba3b4a711e355ec647532df562c483e40c5593e3c0aced78705cf

Contents?: true

Size: 1.28 KB

Versions: 9

Compression:

Stored size: 1.28 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(&block)
        DynamicValue.new(&block)
      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

9 entries across 9 versions & 1 rubygems

Version Path
rake_factory-0.32.0.pre.5 lib/rake_factory/values.rb
rake_factory-0.32.0.pre.4 lib/rake_factory/values.rb
rake_factory-0.32.0.pre.3 lib/rake_factory/values.rb
rake_factory-0.32.0.pre.2 lib/rake_factory/values.rb
rake_factory-0.32.0.pre.1 lib/rake_factory/values.rb
rake_factory-0.31.0 lib/rake_factory/values.rb
rake_factory-0.30.0.pre.8 lib/rake_factory/values.rb
rake_factory-0.30.0.pre.7 lib/rake_factory/values.rb
rake_factory-0.30.0.pre.6 lib/rake_factory/values.rb