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
77 78 79 80 |
# File 'lib/glue/attribute.rb', line 77 def mattr_accessor(*syms) mattr_reader(*syms) mattr_writer(*syms) end |
#mattr_reader(*params) ⇒ Object Also known as: cattr_reader
:nodoc:
17 18 19 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 |
# File 'lib/glue/attribute.rb', line 17 def mattr_reader(*params) default = if params.last.is_a?(Symbol) then nil else params.pop end for sym in params module_eval <<-"end_eval", __FILE__, __LINE__ if not defined?(@@#{sym.id2name}) @@#{sym.id2name} = #{default.inspect} end def self.#{sym.id2name} @@#{sym} end def #{sym.id2name} @@#{sym} end def call_#{sym.id2name} case @@#{sym.id2name} when Symbol then send(@@#{sym}) when Proc then @@#{sym}.call(self) when String then @@#{sym} else nil end end end_eval end end |
#mattr_writer(*params) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/glue/attribute.rb', line 50 def mattr_writer(*params) default = if params.last.is_a?(Symbol) then nil else params.pop end for sym in params module_eval <<-"end_eval", __FILE__, __LINE__ if not defined?(@@#{sym.id2name}) @@#{sym.id2name} = #{default.inspect.inspect} end def self.#{sym.id2name}=(obj) @@#{sym.id2name} = obj end def self.set_#{sym.id2name}(obj) @@#{sym.id2name} = obj end def #{sym.id2name}=(obj) @@#{sym} = obj end end_eval end end |
#meta(key, val) ⇒ Object
Attach metadata. Guard against duplicates, no need to keep order. This method uses closures :) – gmosx: crappy implementation, recode. ++
378 379 380 381 382 383 384 |
# File 'lib/glue/property.rb', line 378 def (key, val) self.module_eval %{ @@__meta[key] ||= [] @@__meta[key].delete_if { |v| val == v } @@__meta[key] << val } end |
#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.
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 314 315 316 317 |
# File 'lib/glue/property.rb', line 260 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 %{ def self.inherited(sub) N::PropertyUtils.enchant(sub) N::PropertyUtils.copy_props(self, sub) # gmosx: We have to define @@__props first to avoid reusing # the hash from the module. super must stay at the end. super end } else # Add some extra code for modules to append # their features to classes that include it. self.module_eval %{ def self.append_features(base) N::PropertyUtils.enchant(base) N::PropertyUtils.copy_props(self, base) # gmosx: We have to define @@__props first to avoid reusing # the hash from the module. super must stay at the end. N::PropertyUtils.include_meta_mixins(base) super end } end property = N::Property.new(symbol, klass, ) reader = [:reader] || true writer = writer || [:writer] || false [:reader] = true if [:reader].nil? if defined?(writer) [:writer] = writer else [:writer] = true if [:writer].nil? end N::PropertyUtils.add_prop(self, property) # gmosx: should be placed AFTER enchant! N::PropertyUtils.(self) end |
#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)”
359 360 361 362 363 364 365 366 367 368 |
# File 'lib/glue/property.rb', line 359 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)”
325 326 327 328 329 330 331 332 333 334 |
# File 'lib/glue/property.rb', line 325 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)”
342 343 344 345 346 347 348 349 350 351 |
# File 'lib/glue/property.rb', line 342 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 |