Sha256: 39f946c5791ac4770d0aeadc9ce4e15b137fad70b53227849420160d8024016c

Contents?: true

Size: 1.21 KB

Versions: 15

Compression:

Stored size: 1.21 KB

Contents

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

15 entries across 15 versions & 1 rubygems

Version Path
rake_factory-0.30.0.pre.5 lib/rake_factory/values.rb
rake_factory-0.30.0.pre.4 lib/rake_factory/values.rb
rake_factory-0.30.0.pre.3 lib/rake_factory/values.rb
rake_factory-0.30.0.pre.2 lib/rake_factory/values.rb
rake_factory-0.30.0.pre.1 lib/rake_factory/values.rb
rake_factory-0.29.0 lib/rake_factory/values.rb
rake_factory-0.28.0.pre.2 lib/rake_factory/values.rb
rake_factory-0.28.0.pre.1 lib/rake_factory/values.rb
rake_factory-0.27.0 lib/rake_factory/values.rb
rake_factory-0.26.0.pre.1 lib/rake_factory/values.rb
rake_factory-0.24.0.pre.2 lib/rake_factory/values.rb
rake_factory-0.25.0 lib/rake_factory/values.rb
rake_factory-0.24.0.pre.1 lib/rake_factory/values.rb
rake_factory-0.23.0 lib/rake_factory/values.rb
rake_factory-0.22.0.pre.1 lib/rake_factory/values.rb