| Module | AttributedClass |
| In: |
lib/attributed_class.rb
|
Extension to have inherited, class attributes, Where each class have his own attributes set.
# File lib/attributed_class.rb, line 246 def self.attribute ( name, descr, *args, &block ) attr = Attribute.new(name, descr, *args, &block) unless method_defined? name if att = get_attribute(name, MethodAttribute) attributes.delete(att) end class_eval "def \#{name}(arg=nil, &block)\nif arg.nil?\nif block.nil?\n@\#{name}\nelse\nself.\#{name} = block[]\nend\nelse\nself.\#{name} = arg\nend\nend\n", __FILE__, __LINE__ end meth = "#{name}=" unless method_defined? meth if att = get_attribute(meth, MethodAttribute) attributes.delete(att) end attr_writer name end attributes << attr end
# File lib/attributed_class.rb, line 235 def self.attributes unless defined? @attributes if superclass and superclass.respond_to? :attributes @attributes = superclass.attributes.dup else @attributes = [] end end @attributes end
# File lib/attributed_class.rb, line 285 def self.get_attribute ( name, type=nil ) if type.nil? attributes.find { |attr| attr.name == name } else attributes.find { |attr| attr.name == name && attr.is_a?(type) } end end
# File lib/attributed_class.rb, line 226 def self.help ( output=STDERR ) output.puts "#{self.to_s}:" attributes.each do |attr| output << ' ' attr.help(output) output.puts '.' end end
# File lib/attributed_class.rb, line 176 def self.included ( aClass ) aClass.module_eval do def initialize_attributes ( *default_proc_args ) self.class.attributes.each do |attr| next if attr.is_a? MethodAttribute default = attr.compute_default(default_proc_args) if default.nil? instance_variable_set("@#{attr.name}", nil) else send "#{attr.name}=", default end end end def self.set_default_proc ( &block ) Attribute.set_default_proc(&block) end def each_attribute ( &block ) self.class.attributes.each do |attr| block[attr, send(attr.name)] end end def each_variable_attribute ( &block ) self.class.attributes.each do |attr| next if attr.is_a? MethodAttribute block[attr, send(attr.name)] end end def each_method_attribute ( &block ) self.class.attributes.each do |attr| next unless attr.is_a? MethodAttribute block[attr, send(attr.name)] end end def check_attribute ( attr, anObject ) unless attr.valid? anObject raise AttributeError.new(self, attr, anObject) end end def check_attributes each_variable_attribute(&method(:check_attribute)) end def self.help ( output=STDERR ) output.puts "#{self.to_s}:" attributes.each do |attr| output << ' ' attr.help(output) output.puts '.' end end def self.attributes unless defined? @attributes if superclass and superclass.respond_to? :attributes @attributes = superclass.attributes.dup else @attributes = [] end end @attributes end def self.attribute ( name, descr, *args, &block ) attr = Attribute.new(name, descr, *args, &block) unless method_defined? name if att = get_attribute(name, MethodAttribute) attributes.delete(att) end class_eval "def \#{name}(arg=nil, &block)\nif arg.nil?\nif block.nil?\n@\#{name}\nelse\nself.\#{name} = block[]\nend\nelse\nself.\#{name} = arg\nend\nend\n", __FILE__, __LINE__ end meth = "#{name}=" unless method_defined? meth if att = get_attribute(meth, MethodAttribute) attributes.delete(att) end attr_writer name end attributes << attr end def self.method_attribute ( name, descr, *args, &block ) # unless method_defined? name # raise ArgumentError, "Undefined method #{name}" # end attr = MethodAttribute.new(name, descr, *args, &block) attributes << attr end def self.get_attribute ( name, type=nil ) if type.nil? attributes.find { |attr| attr.name == name } else attributes.find { |attr| attr.name == name && attr.is_a?(type) } end end end end
# File lib/attributed_class.rb, line 277 def self.method_attribute ( name, descr, *args, &block ) # unless method_defined? name # raise ArgumentError, "Undefined method #{name}" # end attr = MethodAttribute.new(name, descr, *args, &block) attributes << attr end
# File lib/attributed_class.rb, line 192 def self.set_default_proc ( &block ) Attribute.set_default_proc(&block) end
# File lib/attributed_class.rb, line 216 def check_attribute ( attr, anObject ) unless attr.valid? anObject raise AttributeError.new(self, attr, anObject) end end
# File lib/attributed_class.rb, line 222 def check_attributes each_variable_attribute(&method(:check_attribute)) end
# File lib/attributed_class.rb, line 196 def each_attribute ( &block ) self.class.attributes.each do |attr| block[attr, send(attr.name)] end end
# File lib/attributed_class.rb, line 209 def each_method_attribute ( &block ) self.class.attributes.each do |attr| next unless attr.is_a? MethodAttribute block[attr, send(attr.name)] end end
# File lib/attributed_class.rb, line 202 def each_variable_attribute ( &block ) self.class.attributes.each do |attr| next if attr.is_a? MethodAttribute block[attr, send(attr.name)] end end
# File lib/attributed_class.rb, line 180 def initialize_attributes ( *default_proc_args ) self.class.attributes.each do |attr| next if attr.is_a? MethodAttribute default = attr.compute_default(default_proc_args) if default.nil? instance_variable_set("@#{attr.name}", nil) else send "#{attr.name}=", default end end end