Module: FlexCoerce

Defined in:
lib/flex_coerce.rb,
lib/flex_coerce/version.rb,
lib/flex_coerce/flex_coerce.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepend_features(base) ⇒ Object



2
3
4
5
6
7
# File 'lib/flex_coerce/flex_coerce.rb', line 2

def self.prepend_features(base)
  base.instance_eval{
    @__original_coerce_arity ||= base.instance_method(:coerce).arity
  }
  super
end

Instance Method Details

#coerce(other, meth = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/flex_coerce/flex_coerce.rb', line 8

def coerce(other, meth = nil)
  if meth
    case self.class.class_eval{ @__original_coerce_arity }
    when 1
      super(other.obj)
    when 2, -2
      # coerce(other, meth) or coerce(other, meth = nil)
      super(other.obj, meth)
    else
      raise 'Too many arguments for coerce'
    end
  else
    [CoerceableWrapper.new(other), self]
  end
end