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 "\n      def \#{attribute}_with_model_x_boolean=(value)\n        self.\#{attribute}_without_model_x_boolean = ModelX::Boolean.convert(value)\n      end\n      alias_method_chain :\#{attribute}=, :model_x_boolean\n      alias_method :\#{attribute}?, :\#{attribute}\n\n    RUBY\n\n  end\nend\n", __FILE__, __LINE__+1