Class: ImageOptim::BinResolver::ComparableCondition

Inherits:
Object
  • Object
show all
Defined in:
lib/image_optim/bin_resolver/comparable_condition.rb

Overview

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

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, *args) ⇒ ComparableCondition

Returns a new instance of ComparableCondition.



27
28
29
30
31
32
33
34
35
36
37
38
# 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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



26
27
28
# File 'lib/image_optim/bin_resolver/comparable_condition.rb', line 26

def args
  @args
end

#methodObject (readonly)

Returns the value of attribute method.



26
27
28
# File 'lib/image_optim/bin_resolver/comparable_condition.rb', line 26

def method
  @method
end

Class Method Details

.isObject



22
23
24
# File 'lib/image_optim/bin_resolver/comparable_condition.rb', line 22

def self.is
  Builder.new
end

Instance Method Details

#===(other) ⇒ Object Also known as: match



40
41
42
# File 'lib/image_optim/bin_resolver/comparable_condition.rb', line 40

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

#to_sObject



45
46
47
48
49
50
51
# 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