From: Tom Sawyer Date: 2003-01-07T15:06:36+09:00 Subject: Re: attr_cast, one a small step for interface techniques THERE SEEMS TO BE A BUG. HAVEN'T FIGURED IT OUT YET THOUGH. ANYONE SEE IT? On Monday 06 January 2003 11:00 pm, Tom Sawyer wrote: > here you go, in full! (by the way the old attr_accessor does not depend on > attr_reader or attr_writer. go figure. this new one does.) let me know if > you see any problems with this. > > alias attr_reader_basic attr_reader > > def attr_reader(*args) > if args.last.kind_of?(Hash) or args.last.kind_of?(Array) > attr_reader_with_cast *(args.pop) > end > attr_reader_basic *args > end > > def attr_reader_with_cast(*args) > args.each { |name, cast| > class_eval <<-EOS > def #{name}() > @#{name}.#{cast} > end > EOS > } > end > > alias attr_writer_basic attr_writer > > def attr_writer(*args) > if args.last.kind_of?(Hash) or args.last.kind_of?(Array) > attr_writer_with_cast *(args.pop) > end > attr_writer_basic *args > end > > def attr_writer_with_cast(*args) > args.each { |name, cast| > class_eval <<-EOS > def #{name}=(arg) > @#{name} = arg.#{cast} > end > EOS > } > end > > alias attr_accessor_basic attr_accessor > > def attr_accessor(*args) > attr_reader *args > attr_writer *args > end > > def attr_accessor_with_cast(*args) > attr_writer_with_cast *args > attr_reader_with_cast *args > end > > > -transami -- tom sawyer, aka transami transami@transami.net .''. .''. . *''* :_\/_: . :_\/_: _\(/_ .:.*_\/_* : /\ : .'.:.'. .''.: /\ : ./)\ ':'* /\ * : '..'. -=:o:=- :_\/_:'.:::. | ' *''* * '.\'/.' _\(/_'.':'.' : /\ : ::::: = *_\/_* -= o =- /)\ ' * '..' ':::' === * /\ * .'/.\'. '._____ * | *..* : |. |' .---"| * | _ .--'| || | _| | * | .-'| __ | | | || | .-----. | |' | || | | | | | || | ___' ' /"\ | '-."". '-' '-.' '` |_. ------------------------------------------------------------