Sha256: ab607a8a101d434e857767d0f8661011d06c2b8473610438211fe458efd5a969

Contents?: true

Size: 1.46 KB

Versions: 29

Compression:

Stored size: 1.46 KB

Contents

class ImageOptim
  class BinResolver
    # Allows to externalize conditions for an instance of Comparable to use in
    # case statemens
    #
    #     is = ComparableCondition.is
    #     case rand(100)
    #     when is < 10 then # ...
    #     when is.between?(13, 23) then # ...
    #     when is >= 90 then # ...
    #     end
    class ComparableCondition
      # Helper class for creating conditions using ComparableCondition.is
      class Builder
        Comparable.instance_methods.each do |method|
          define_method method do |*args|
            ComparableCondition.new(method, *args)
          end
        end
      end

      def self.is
        Builder.new
      end

      attr_reader :method, :args
      def initialize(method, *args)
        @method, @args = method.to_sym, args

        case @method
        when :between?
          @args.length == 2 || argument_error!("`between?' expects 2 arguments")
        when :<, :<=, :==, :>, :>=
          @args.length == 1 || argument_error!("`#{method}' expects 1 argument")
        else
          argument_error! "Unknown method `#{method}'"
        end
      end

      def ===(other)
        other.send(@method, *@args)
      end
      alias_method :match, :===

      def to_s
        if @method == :between?
          @args.join('..')
        else
          "#{@method} #{@args.first}"
        end
      end

    private

      def argument_error!(message)
        fail ArgumentError, message
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 3 rubygems

Version Path
image_optim-0.26.3 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.26.2 lib/image_optim/bin_resolver/comparable_condition.rb
discourse_image_optim-0.26.2 lib/image_optim/bin_resolver/comparable_condition.rb
discourse_image_optim-0.26.1 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.26.1 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.26.0 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.25.0 lib/image_optim/bin_resolver/comparable_condition.rb
discourse_image_optim-0.24.5 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.24.3 lib/image_optim/bin_resolver/comparable_condition.rb
discourse_image_optim-0.24.4 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.24.2 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.24.1 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.24.0 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.23.0 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.22.1 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.22.0 lib/image_optim/bin_resolver/comparable_condition.rb
openstreetmap-image_optim-0.21.0.1 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.21.0 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.20.2 lib/image_optim/bin_resolver/comparable_condition.rb
image_optim-0.20.1 lib/image_optim/bin_resolver/comparable_condition.rb