class Object #--{{{ def singleton_method_added(*a, &b) #--{{{ obj = self obj.__attribute_singleton_class.module_eval{ @__attribute_singleton_super = obj } #--}}} end def __attribute_singleton_class #--{{{ @__attribute_singleton_class ||= class << self;self;end #--}}} end def __attribute_singleton? #--{{{ @__is_attribute_singleton ||= ((Class === self) and (@__is_attribute_singleton ||= (not self.ancestors.include?(self)))) #--}}} end def __attribute_search_path #--{{{ #((Class === self and not __attribute_singleton?) ? ancestors : [self]) (__attribute_singleton? or not respond_to? 'ancestors') ? [self] : ancestors #--}}} end def __attribute_arg_filter(*args) #--{{{ args, hashes = [ args ].flatten.partition{|item| not Hash === item} opts = hashes.inject(accum={}){|accum,hash| accum.update hash} [ args, opts ] #--}}} end def __attribute_instance_method_list #--{{{ @__attribute_instance_method_list ||= {'writers' => [], 'readers' => []} #--}}} end def __attribute_singleton_method_list #--{{{ @__attribute_singleton_method_list ||= {'writers' => [], 'readers' => []} #--}}} end def __attribute_instance_method_defaults #--{{{ @__attribute_instance_method_defaults ||= {} #--}}} end def __attribute_singleton_method_defaults #--{{{ @__attribute_singleton_method_defaults ||= {} #--}}} end def __attribute_define_singleton_reader_attributes(*args) #--{{{ names, names_and_defaults = __attribute_arg_filter args names.each{|name| names_and_defaults[name] = nil} defaults = __attribute_singleton_method_defaults list = __attribute_singleton_method_list unless names_and_defaults.empty? names_and_defaults.each do |name, default| __attribute_singleton_class.__attribute_define_reader_attribute name, default, defaults, list end end __attribute_search_path.map{|ta| ta.__attribute_singleton_method_list['readers']}.flatten #--}}} end def __attribute_define_singleton_writer_attributes(*args) #--{{{ names, names_and_defaults = __attribute_arg_filter args names.each{|name| names_and_defaults[name] = nil} defaults = __attribute_singleton_method_defaults list = __attribute_singleton_method_list unless names_and_defaults.empty? names_and_defaults.each do |name, default| __attribute_singleton_class.__attribute_define_writer_attribute name, default, defaults, list end end __attribute_search_path.map{|ta| ta.__attribute_singleton_method_list['writers']}.flatten #--}}} end def __attribute_define_singleton_attributes(*args) #--{{{ writers = __attribute_define_singleton_writer_attributes(*args) readers = __attribute_define_singleton_reader_attributes(*args) [readers, writers] #--}}} end #--}}} end class Class #--{{{ def __attribute_singleton_super #--{{{ class_eval{ def __________attribute_singleton_super_init;end } unless defined? @__attribute_singleton_super @__attribute_singleton_super #--}}} end #--}}} end class Module #--{{{ def __attribute_define_instance_reader_attributes(*args) #--{{{ if __attribute_singleton? return(__attribute_singleton_super.__attribute_define_singleton_reader_attributes(*args)) end names, names_and_defaults = __attribute_arg_filter args names.each{|name| names_and_defaults[name] = nil} defaults = __attribute_instance_method_defaults list = __attribute_instance_method_list unless names_and_defaults.empty? names_and_defaults.each do |name, default| __attribute_define_reader_attribute name, default, defaults, list end end __attribute_search_path.map{|ta| ta.__attribute_instance_method_list['readers']}.flatten #--}}} end %w( instance_reader_attributes instance_reader_attribute reader_attributes reader_attribute rattributes rattribute has_readers has_reader has_r ).each{|meth| alias_method meth, '__attribute_define_instance_reader_attributes'} def __attribute_define_instance_writer_attributes(*args) #--{{{ if __attribute_singleton? return(__attribute_singleton_super.__attribute_define_singleton_writer_attributes(*args)) end names, names_and_defaults = __attribute_arg_filter args names.each{|name| names_and_defaults[name] = nil} defaults = __attribute_instance_method_defaults list = __attribute_instance_method_list unless names_and_defaults.empty? names_and_defaults.each do |name, default| __attribute_define_writer_attribute name, default, defaults, list end end __attribute_search_path.map{|ta| ta.__attribute_instance_method_list['writers']}.flatten #--}}} end %w( instance_writer_attributes instance_writer_attribute writer_attributes writer_attribute wattributes wattribute has_writers has_writer has_w ).each{|meth| alias_method meth, '__attribute_define_instance_writer_attributes'} def __attribute_define_instance_attributes(*args) #--{{{ writers = __attribute_define_instance_writer_attributes(*args) readers = __attribute_define_instance_reader_attributes(*args) [readers, writers] #--}}} end %w( instance_attributes instance_attribute attributes attribute has ).each{|meth| alias_method meth, '__attribute_define_instance_attributes'} def __attribute_define_class_reader_attributes(*args) #--{{{ __attribute_define_singleton_reader_attributes(*args) #--}}} end %w( class_reader_attributes class_reader_attribute class_rattributes class_rattribute class_has_readers class_has_reader class_has_r c_has_readers c_has_reader c_has_r ).each{|meth| alias_method meth, '__attribute_define_class_reader_attributes'} def __attribute_define_class_writer_attributes(*args) #--{{{ __attribute_define_singleton_writer_attributes(*args) #--}}} end %w( class_writer_attributes class_writer_attribute class_wattributes class_wattribute class_has_writers class_has_writer class_has_w c_has_writers c_has_writer c_has_w ).each{|meth| alias_method meth, '__attribute_define_class_writer_attributes'} def __attribute_define_class_attributes(*args) #--{{{ __attribute_define_singleton_attributes(*args) #--}}} end %w( class_attributes class_attribute class_has c_has ).each{|meth| alias_method meth, '__attribute_define_class_attributes'} def __attribute_define_reader_attribute name, default, defaults, list #--{{{ getter = "#{ name }" setter = "#{ name }=" query = "#{ name }?" defaults[getter] = default if default list['readers'] << getter unless instance_methods.include? getter code = __attribute_gen_reader_code name, 'public' module_eval code end unless instance_methods.include? setter code = __attribute_gen_writer_code name, 'private' module_eval code end unless instance_methods.include? query code = __attribute_gen_query_code name, 'public' module_eval code end #--}}} end def __attribute_define_writer_attribute name, default, defaults, list #--{{{ reader = "#{ name }" writer = "#{ name }=" query = "#{ name }?" defaults[reader] = default if default list['writers'] << writer unless instance_methods.include? reader code = __attribute_gen_reader_code name, 'private' module_eval code end unless instance_methods.include? writer code = __attribute_gen_writer_code name, 'public' module_eval code end unless instance_methods.include? query code = __attribute_gen_query_code name, 'private' module_eval code end #--}}} end def __attribute_gen_reader_code name, access_protection = 'public' #--{{{ s = __attribute_singleton? #&& __attribute_singleton_super.is_a?(Class) klass = s ? 'self' : 'self.class' defaults = s ? '__attribute_singleton_method_defaults' : '__attribute_instance_method_defaults' search_ancestors = s ? 'true' : 'false' getter_meth = "#{ name }" setter_meth = "#{ name }=" query_meth = "#{ name }?" ivar = "@#{ name }" getter = "'#{ name }'" setter = "'#{ name }='" reader_code = <<-code def #{ name }(*a, &b) unless a.empty? send('#{ name }=', *a, &b) else unless(defined?(@________#{ name }_set) and @________#{ name }_set) #{ klass }.__attribute_search_path.each do |obj| #if self != obj and obj.respond_to? '#{ name }' #return(obj.send('#{ name }', *a, &b)) #else defaults = obj.#{ defaults } return(send('#{ name }=', *defaults['#{ name }'])) if defaults.has_key? '#{ name }' #end end end case self when Class if defined? @#{ name } @#{ name } else __attribute_search_path.each do |obj| if self != obj and obj.respond_to? '#{ name }' return(obj.send('#{ name }', *a, &b)) end end return nil end else @#{ name } end end end #{ access_protection } '#{ name }'.intern code #p self #puts reader_code #reader_code #--}}} end def __attribute_gen_writer_code name, access_protection = 'public' #--{{{ writer_code = <<-code def #{ name }= value @________#{ name }_set = true @#{ name } = value end #{ access_protection } '#{ name }='.intern code #--}}} end def __attribute_gen_query_code name, access_protection = 'public' #--{{{ query_code = <<-code def #{ name }? send('#{ name }') ? true : false end # #{ access_protection } '#{ name }?'.intern code #p self #puts query_code #query_code #--}}} end def __attribute_gen_access_protection_code name, access_protection = nil #--{{{ access_protection ||= 'public' case access_protection when %r/private/ "private '#{ name }'.intern" when %r/protected/ "protected '#{ name }'.intern" else "public '#{ name }'.intern" end #--}}} end #--}}} end