Module: Stupidedi::Versions::FunctionalGroups::ThirtyFifty::ElementTypes::Operators::Relational

Included in:
DateVal::Proper, FixnumVal::NonEmpty, FloatVal::NonEmpty, IdentifierVal::Empty, IdentifierVal::NonEmpty, StringVal::Empty, StringVal::NonEmpty
Defined in:
lib/stupidedi/versions/functional_groups/003050/element_types/operators.rb

Instance Method Summary collapse

Instance Method Details

#relational_operators(*ops) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/stupidedi/versions/functional_groups/003050/element_types/operators.rb', line 61

def relational_operators(*ops)
  file, line, = Stupidedi.caller

  if ops.last.is_a?(Hash)
    options = ops.pop

    case options[:coerce]
    when String, Symbol
      coerce = options[:coerce]
    else
      raise ArgumentError,
        "must pass :coerce => :method"
    end
  else
    raise ArgumentError,
      "must pass :coerce => :method"
  end

  ops.each do |op|
    class_eval(<<-RUBY, file, line.to_i - 1)
      def #{op}(other, &block)
        if other.respond_to?(:#{coerce})
          value.__send__(:#{op}, other.#{coerce}, &block)
        elsif other.respond_to?(:coerce)
          me, he = other.coerce(self)
          me.__send__(:#{op}, he, &block)
        else
          raise TypeError,
            "cannot coerce \#{other.class} to \#{self.class}"
        end
      end
    RUBY
  end
end