Class: Module
- Includes:
- Og::MetaLanguage
- Defined in:
- lib/og/meta.rb,
lib/glue/property.rb,
lib/glue/attribute.rb
Overview
-- Extends the module object with module and instance accessors for class attributes, just like the native attr* accessors for instance attributes. Aliases for classes are also provided.
Example:
mattr_accessor :my_attr, 'Default value' ++
Instance Method Summary collapse
- #mattr_accessor(*syms) ⇒ Object (also: #cattr_accessor)
-
#mattr_reader(*params) ⇒ Object
(also: #cattr_reader)
:nodoc:.
- #mattr_writer(*params) ⇒ Object
-
#meta(key, val) ⇒ Object
Attach metadata.
-
#prop(*params) ⇒ Object
Define a property (== typed attribute) This works like Ruby's standard attr method, ie creates only one property.
-
#prop_accessor(*params) ⇒ Object
Helper method.
-
#prop_reader(*params) ⇒ Object
Helper method.
-
#prop_writer(*params) ⇒ Object
Helper method.
Methods included from Og::MetaLanguage
#belongs_to, #has_many, #has_one, #joins, #many_to_many, #refers_to, #sql_index
Instance Method Details
#mattr_accessor(*syms) ⇒ Object Also known as: cattr_accessor
80 81 82 83 |
# File 'lib/glue/attribute.rb', line 80 def mattr_accessor(*syms) mattr_reader(*syms) mattr_writer(*syms) end |
#mattr_reader(*params) ⇒ Object Also known as: cattr_reader
:nodoc:
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/glue/attribute.rb', line 20 def mattr_reader(*params) default = if params.last.is_a?(Symbol) then nil else params.pop end for sym in params module_eval " \n if not defined?(@@\#{sym.id2name})\n @@\#{sym.id2name} = \#{default.inspect}\n end\n \n def self.\#{sym.id2name}\n @@\#{sym}\n end\n\n def \#{sym.id2name}\n @@\#{sym}\n end\n\n def call_\#{sym.id2name}\n case @@\#{sym.id2name}\n when Symbol then send(@@\#{sym})\n when Proc then @@\#{sym}.call(self)\n when String then @@\#{sym}\n else nil\n end\n end\n \n end_eval\n end\nend\n", __FILE__, __LINE__ |
#mattr_writer(*params) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/glue/attribute.rb', line 53 def mattr_writer(*params) default = if params.last.is_a?(Symbol) then nil else params.pop end for sym in params module_eval "\n if not defined?(@@\#{sym.id2name})\n @@\#{sym.id2name} = \#{default.inspect.inspect}\n end\n \n def self.\#{sym.id2name}=(obj)\n @@\#{sym.id2name} = obj\n end\n\n def self.set_\#{sym.id2name}(obj)\n @@\#{sym.id2name} = obj\n end\n\n def \#{sym.id2name}=(obj)\n @@\#{sym} = obj\n end\n\n end_eval\n end\nend\n", __FILE__, __LINE__ |
#meta(key, val) ⇒ Object
Attach metadata. Guard against duplicates, no need to keep order. This method uses closures :)
gmosx: crappy implementation, recode. ++
368 369 370 371 372 373 374 |
# File 'lib/glue/property.rb', line 368 def (key, val) self.module_eval " @@__meta[key] ||= [] \n @@__meta[key].delete_if { |v| val == v }\n @@__meta[key] << val \n end_eval\nend\n", __FILE__, __LINE__ |
#prop(*params) ⇒ Object
Define a property (== typed attribute) This works like Ruby's standard attr method, ie creates only one property.
Use the prop_reader, prop_writer, prop_accessor methods for multiple properties.
Examples: prop String, :name, :sql => "char(32), :sql_index => "name(32)" --> creates only writer. prop Fixnum, :oid, writer = true, :sql => "integer PRIMARY KEY" --> creates reader and writer.
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 |
# File 'lib/glue/property.rb', line 243 def prop(*params) , klass, symbols = N::PropertyUtils.resolve_prop_params(params) symbol = symbols.first N::PropertyUtils.enchant(self) if self.is_a?(Class) # Add some extra code to append features to # subclasses. self.module_eval "\n def self.inherited(sub)\n N::PropertyUtils.enchant(sub)\n N::PropertyUtils.copy_props(self, sub)\n # gmosx: We have to define @@__props first to avoid reusing\n # the hash from the module. super must stay at the end.\n super\n end\n\n end_eval\n\n else\n \n # Add some extra code for modules to append\n # their features to classes that include it.\n \n self.module_eval <<-\"end_eval\", __FILE__, __LINE__\n\n def self.append_features(base)\n N::PropertyUtils.enchant(base)\n N::PropertyUtils.copy_props(self, base)\n\n # gmosx: We have to define @@__props first to avoid reusing\n # the hash from the module. super must stay at the end.\n \n N::PropertyUtils.include_meta_mixins(base)\n \n super\n end\n\n end_eval\n \n end\n \n property = N::Property.new(symbol, klass, meta)\n \n reader = meta[:reader] || true\n writer = writer || meta[:writer] || false\n\n meta[:reader] = true if meta[:reader].nil?\n if defined?(writer)\n meta[:writer] = writer \n else\n meta[:writer] = true if meta[:writer].nil?\n end\n\n N::PropertyUtils.add_prop(self, property)\n\n # gmosx: should be placed AFTER enchant!\n \n N::PropertyUtils.include_meta_mixins(self)\nend\n", __FILE__, __LINE__ |
#prop_accessor(*params) ⇒ Object
Helper method. Accepts a collection of symbols and generates properties. Generates reader and writer.
Example: prop_accessor String, :name, :title, :body, :sql => "char(32)"
349 350 351 352 353 354 355 356 357 358 |
# File 'lib/glue/property.rb', line 349 def prop_accessor(*params) , klass, symbols = N::PropertyUtils.resolve_prop_params(params) [:reader] = true [:writer] = true for symbol in symbols prop(klass, symbol, ) end end |
#prop_reader(*params) ⇒ Object
Helper method. Accepts a collection of symbols and generates properties. Only generates reader.
Example: prop_reader String, :name, :title, :body, :sql => "char(32)"
315 316 317 318 319 320 321 322 323 324 |
# File 'lib/glue/property.rb', line 315 def prop_reader(*params) , klass, symbols = N::PropertyUtils.resolve_prop_params(params) [:reader] = true [:writer] = false for symbol in symbols prop(klass, symbol, ) end end |
#prop_writer(*params) ⇒ Object
Helper method. Accepts a collection of symbols and generates properties. Only generates writer.
Example: prop_writer String, :name, :title, :body, :sql => "char(32)"
332 333 334 335 336 337 338 339 340 341 |
# File 'lib/glue/property.rb', line 332 def prop_writer(*params) , klass, symbols = N::PropertyUtils.resolve_prop_params(params) [:reader] = false [:writer] = true for symbol in symbols prop(klass, symbol, ) end end |