Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Handler
- attr_accessor :entity
- def initialize(entity)
- @entity = entity
- end
- def self.my_attr(name_symbol,&write_block)
- name_string = name_symbol.to_s
- write_symbol = (name_string + "=").to_sym
- if block_given? then
- write_proc = Proc.new{|value|
- new_value = write_block.call(value)
- @entity.write(new_value)
- #the instance_variable_set method creates a class instance variable
- #I want to create a proper instance variable here
- instance_variable_set(("@" + name_string).to_sym,new_value)}
- else
- write_proc = Proc.new{@entity.write(name_string,value)
- #ditto here
- instance_variable_set(("@" + name_string).to_sym,new_value)}
- end
- read_proc = Proc.new{@entity.read(name_string)}
- define_method(name_symbol,read_proc)
- define_method(write_symbol,write_proc)
- end
- end
- class MyHandler < Handler
- my_attr(:off_center){|new_off_center|
- if new_off_center != @off_center then
- #I want off_center to be an instance variable, not a class instance variable
- #but the behaviour here isn't working out that way
- @off_center = new_off_center + 180
- else
- @off_center = new_off_center - 90
- end
- }
- end
Add Comment
Please, Sign In to add comment