Class: Class

Inherits:
Object show all
Defined in:
lib/ruby-ext.rb

Instance Method Summary collapse

Instance Method Details

#attr_checked(attribute, &validation) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/ruby-ext.rb', line 13

def attr_checked(attribute, &validation)
  define_method "#{attribute}=" do |val|
    raise "Invalid attribute #{val.inspect}" unless validation.call(val)
    instance_variable_set("@#{attribute}", val)
  end
  define_method attribute do
    instance_variable_get "@#{attribute}"
  end
end

#attr_writer_delegate(*args) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/ruby-ext.rb', line 22

def attr_writer_delegate(*args)
  args.each do |name|
    define_method "#{name}=" do |value|
      instance_variable_set("@#{name}", self.class.const_get(name.to_s.to_camel.to_sym).new(value))
    end
  end
end