Module: ModelX::Boolean::ClassMethods

Defined in:
lib/model_x/boolean.rb

Instance Method Summary collapse

Instance Method Details

#boolean(*attributes) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/model_x/boolean.rb', line 36

def boolean(*attributes)
  attributes.each do |attribute|

    # An attribute must already exist.
    unless instance_methods.include?(:"#{attribute}=")
      raise ArgumentError, "cannot add boolean attribute #{attribute} - no existing attribute exists"
    end

    # Override the writer and add a ? version.
    class_eval <<-RUBY, __FILE__, __LINE__+1

      def #{attribute}_with_model_x_boolean=(value)
        self.#{attribute}_without_model_x_boolean = ModelX::Boolean.convert(value)
      end
      alias_method_chain :#{attribute}=, :model_x_boolean
      alias_method :#{attribute}?, :#{attribute}

    RUBY

  end
end