Class: ClassX::AttributeFactory
- Inherits:
-
Object
- Object
- ClassX::AttributeFactory
- Defined in:
- lib/classx/attribute.rb
Overview
generating anonymous class for meta attribute class.
Class Method Summary collapse
-
.create(args) ⇒ Object
creating anonymous class for meta attribute class.
Class Method Details
.create(args) ⇒ Object
creating anonymous class for meta attribute class.
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/classx/attribute.rb', line 203 def self.create args # TODO: hmm, ClassX::Commandable do nothing when freezed. # # if you would like to change attribute's infomation, it's better to redefine attribute. # So, config should freezed. # args.each do |key, val| # key.freeze # val.freeze # end # args.freeze raise ClassX::RequiredAttrShouldNotHaveDefault if args[:optional] == false && !args[:default].nil? raise ClassX::OptionalAttrShouldBeWritable if args[:optional] && args[:writable] == false klass = Class.new klass.extend(ClassX::AttributeMethods::ClassMethods) # you specify type changing rule with :coerce option. if args[:coerce] case args[:coerce] when Hash klass.extend(ClassX::AttributeMethods::ClassMethods::CoerceWithHash) when Array klass.extend(ClassX::AttributeMethods::ClassMethods::CoerceWithArray) when Proc klass.extend(ClassX::AttributeMethods::ClassMethods::CoerceWithProc) end else klass.extend(ClassX::AttributeMethods::ClassMethods::CoerceNothing) end # default paramater for attribute. # if default is Proc, run Proc every time in instanciate. case args[:default] when Proc klass.extend(ClassX::AttributeMethods::ClassMethods::DefaultWithProc) else klass.extend(ClassX::AttributeMethods::ClassMethods::DefaultWithNoProc) end # you can specify :validate option for checking when value is setted. # you can use :respond_to as shotcut for specifying { :validate => proc {|val| respond_to?(val, true) } } # you can use :isa or :kind_of as shotcut for specifying { :validate => proc {|val| kind_of?(val) } } if args[:validate] case args[:validate] when Proc klass.extend(ClassX::AttributeMethods::ClassMethods::ValidateWithProc) else klass.extend(ClassX::AttributeMethods::ClassMethods::ValidateWithNotProc) end elsif args[:validate_each] klass.extend(ClassX::AttributeMethods::ClassMethods::ValidateEach) elsif mod = ( args[:isa] || args[:kind_of] ) klass.extend(ClassX::AttributeMethods::ClassMethods::ValidateKindOf) elsif args[:respond_to] klass.extend(ClassX::AttributeMethods::ClassMethods::ValidateRespondTo) else klass.extend(ClassX::AttributeMethods::ClassMethods::ValidateNothing) end # you can specify :trigger option for invoking event on setting value to attribute. # :trigger should take Proc or multi Proc Array. if args[:trigger] unless args[:trigger].kind_of?(Array) args[:trigger] = [ args[:trigger] ] end klass.extend(ClassX::AttributeMethods::ClassMethods::TriggerArrayProc) else args[:trigger] = [] klass.extend(ClassX::AttributeMethods::ClassMethods::TriggerArrayProc) end # for extending attribute point. if args[:include] case args[:include] when Array args[:include].each do |mod| klass.__send__ :include, mod end else klass.__send__ :include, args[:include] end end if args[:extend] case args[:extend] when Array args[:extend].each do |mod| klass.__send__ :extend, mod end else klass.__send__ :extend, args[:extend] end end # XXX: Hack for defining class method for klass tmp_mod = Module.new tmp_mod.module_eval do define_method :config do args end end klass.extend(tmp_mod) klass.class_eval do include ClassX::AttributeMethods::InstanceMethods end klass end |