Module: AttributedClass
- Defined in:
- lib/attributed_class.rb
Overview
Extension to have inherited, class attributes, Where each class have his own attributes set.
Defined Under Namespace
Classes: Attribute, AttributeError, MethodAttribute
Class Method Summary collapse
Class Method Details
.included(aClass) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# 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 <<-"end;", __FILE__, __LINE__ def #{name}(arg=nil, &block) if arg.nil? if block.nil? @#{name} else self.#{name} = block[] end else self.#{name} = arg end end end; 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 |