class ImageOptim::BinResolver::ComparableCondition

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

Attributes

args[R]
method[R]

Public Class Methods

is() click to toggle source
# File lib/image_optim/bin_resolver/comparable_condition.rb, line 22
def self.is
  Builder.new
end
new(method, *args) click to toggle source
# File lib/image_optim/bin_resolver/comparable_condition.rb, line 27
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

Public Instance Methods

===(other) click to toggle source
# File lib/image_optim/bin_resolver/comparable_condition.rb, line 40
def ===(other)
  other.send(@method, *@args)
end
Also aliased as: match
match(other)
Alias for: ===
to_s() click to toggle source
# File lib/image_optim/bin_resolver/comparable_condition.rb, line 45
def to_s
  if @method == :between?
    @args.join('..')
  else
    "#{@method} #{@args.first}"
  end
end

Private Instance Methods

argument_error!(message) click to toggle source
# File lib/image_optim/bin_resolver/comparable_condition.rb, line 55
def argument_error!(message)
  fail ArgumentError, message
end