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.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/image_optim/bin_resolver/comparable_condition.rb', line 30

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.



28
29
30
# File 'lib/image_optim/bin_resolver/comparable_condition.rb', line 28

def args
  @args
end

#methodObject (readonly)

Returns the value of attribute method.



28
29
30
# File 'lib/image_optim/bin_resolver/comparable_condition.rb', line 28

def method
  @method
end

Class Method Details

.isObject



24
25
26
# File 'lib/image_optim/bin_resolver/comparable_condition.rb', line 24

def self.is
  Builder.new
end

Instance Method Details

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



43
44
45
# File 'lib/image_optim/bin_resolver/comparable_condition.rb', line 43

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

#to_sObject



48
49
50
51
52
53
54
# File 'lib/image_optim/bin_resolver/comparable_condition.rb', line 48

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