Class: Module
- Inherits:
-
Object
- Object
- Module
- Includes:
- Og::MetaLanguage
- Defined in:
- lib/og/meta.rb,
lib/nitro/properties.rb
Overview
module
Instance Method Summary collapse
-
#__add_prop(prop, reader = true, writer = true) ⇒ Object
Add the property.
-
#inherit_meta(mod = superclass) ⇒ Object
This method is typically called before including other modules to preserve properties order.
-
#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
Instance Method Details
#__add_prop(prop, reader = true, writer = true) ⇒ Object
Add the property
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 |
# File 'lib/nitro/properties.rb', line 219 def __add_prop(prop, reader = true, writer = true) if idx = @__props.index(prop) # override in case of duplicates. Keep the order of the props. @__props[idx] = prop else @__props << prop end # Precompile the property read/write methods s, klass = prop.symbol, prop.klass if reader module_eval %{ def #{s} return @#{s} end } end if writer module_eval %{ def #{s}=(val) @#{s} = val end def __force_#{s}(val) @#{s} = } + case klass.name when Fixnum.name "val.to_i()" when String.name "val.to_s()" when Float.name "val.to_f()" when Time.name "Time.parse(val.to_s())" when TrueClass.name, FalseClass.name "val.to_i() > 0" else "val" end + %{ end } end end |
#inherit_meta(mod = superclass) ⇒ Object
This method is typically called before including other modules to preserve properties order.
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/nitro/properties.rb', line 280 def (mod = superclass) # concat props. if mod.__props @__props = N::SafeArray.new unless @__props mod.__props.each { |p| __add_prop(p) } end # concat metadata if mod. mod..each { |k, val| val.each { |v| (k, v) } if val } end end |
#meta(key, val) ⇒ Object
Attach metadata
267 268 269 270 271 272 273 274 275 |
# File 'lib/nitro/properties.rb', line 267 def (key, val) @__meta = N::SafeHash.new unless @__meta @__meta[key] = [] unless @__meta[key] # guard against duplicates, no need to keep order. @__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.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/nitro/properties.rb', line 70 def prop(*params) = {} klass = Object for param in params if param.is_a?(Class) klass = param elsif param.is_a?(Symbol) symbol = param elsif param.is_a?(TrueClass) or param.is_a?(TrueClass) writer = param elsif param.is_a?(Hash) # the meta hash. = param else raise "Error when defining property!" end end unless self.methods.include?("__props") eval %{ # Properties # An array is used to enforce order. def __props @__props end def __props=(props) @__props = props end def __meta @__meta end def __meta=(meta) @__meta = meta end } end @__props = N::SafeArray.new() unless @__props property = N::Property.new(symbol, klass, ) reader = [:reader] || true writer = writer || [:writer] || false __add_prop(property, reader, writer) 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)”
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 |
# File 'lib/nitro/properties.rb', line 191 def prop_accessor(*params) = {} klass = Object symbols = [] for param in params if param.is_a?(Class) klass = param elsif param.is_a?(Symbol) symbols << param elsif param.is_a?(Hash) # the meta hash. = param else raise "Error when defining property!" end end [: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)”
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/nitro/properties.rb', line 127 def prop_reader(*params) = {} klass = Object symbols = [] for param in params if param.is_a?(Class) klass = param elsif param.is_a?(Symbol) symbols << param elsif param.is_a?(Hash) # the meta hash. = param else raise "Error when defining property!" end end [: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)”
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/nitro/properties.rb', line 159 def prop_writer(*params) = {} klass = Object symbols = [] for param in params if param.is_a?(Class) klass = param elsif param.is_a?(Symbol) symbols << param elsif param.is_a?(Hash) # the meta hash. = param else raise "Error when defining property!" end end [:reader] = false [:writer] = true for symbol in symbols prop(klass, symbol, ) end end |