Class: ImageOptim::BinResolver::ComparableCondition
- Inherits:
-
Object
- Object
- ImageOptim::BinResolver::ComparableCondition
- 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
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
Class Method Summary collapse
Instance Method Summary collapse
- #===(other) ⇒ Object (also: #match)
-
#initialize(method, *args) ⇒ ComparableCondition
constructor
A new instance of ComparableCondition.
- #to_s ⇒ Object
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
#args ⇒ Object (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 |
#method ⇒ Object (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
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_s ⇒ Object
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 |