s

Ruby code posted
created at 09 May 08:55, updated at 09 May 17:24

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module CheckedAttributes 
  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def attr_checked(attribute, &validation)
      define_method "#{attribute}=" do |value|
        raise 'Invalid attribute' unless validation.call(value) 
        instance_variable_set("@#{attribute}", value)
      end
      define_method attribute do 
        instance_variable_get "@#{attribute}"
      end 
    end
  end 
end
456 Bytes in 2 ms with coderay