Class: AppEngine::MirahDatastorePlugin
- Inherits:
-
Object
- Object
- AppEngine::MirahDatastorePlugin
- Defined in:
- lib/mirah_model/datastore.rb
Defined Under Namespace
Classes: ModelState
Constant Summary collapse
- TypeMap =
{ 'Category' => 'String', 'Email' => 'String', 'Link' => 'String', 'PhoneNumber' => 'String', 'PostalAddress' => 'String', 'Text' => 'String', 'Blob' => 'byte[]', 'ShortBlob' => 'byte[]', 'Rating' => 'int', 'Long' => 'long', 'Double' => 'double', 'Boolean' => 'boolean' }
- Primitives =
['Long', 'Double', 'Boolean', 'Rating']
- Boxes =
{ 'int' => 'Integer', 'boolean' => 'Boolean', 'long' => 'Long', 'double' => 'Double', }
- Defaults =
{ 'Rating' => '0', 'Long' => 'long(0)', 'Double' => '0.0', 'Boolean' => 'false', 'Blob' => 'byte[].cast(nil)', 'ShortBlob' => 'byte[].cast(nil)', }
- Conversions =
{ 'Category' => 'getCategory', 'Email' => 'getEmail', 'Link' => 'getValue', 'PhoneNumber' => 'getNumber', 'PostalAddress' => 'getAddress', 'Text' => 'getValue', 'Blob' => 'getBytes', 'ShortBlob' => 'getBytes', 'Long' => 'longValue', 'Double' => 'doubleValue', 'Boolean' => 'booleanValue', 'Rating' => 'getRating', }
Class Method Summary collapse
- .add_property(name, type, transformer, fcall) ⇒ Object
- .find_class(node) ⇒ Object
- .from_datastore(type, value) ⇒ Object
- .maybe_box(mirah_type, value) ⇒ Object
- .reset ⇒ Object
- .to_datastore(type, value) ⇒ Object
Class Method Details
.add_property(name, type, transformer, fcall) ⇒ Object
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/mirah_model/datastore.rb', line 289 def self.add_property(name, type, transformer, fcall) if transformer.state != @state reset @state = transformer.state end parent = fcall.parent name = name.literal type = type.name type = 'Long' if type == 'Integer' result = Mirah::AST::ScopedBody.new(parent, fcall.position) {[]} result.static_scope = fcall.scope.static_scope klass = find_class(parent) unless @models[klass] @models[klass] = ModelState.new( transformer, klass, parent, fcall.position, result) end model = @models[klass] mirah_type = TypeMap.fetch(type, type) coercion = "coerce_" + mirah_type.downcase.sub("[]", "s") if mirah_type == 'List' model.extend_query(<<-EOF) def #{name}(value:Object) returns :void _query.addFilter("#{name}", _eq_op, value) end EOF else model.extend_query(<<-EOF) def #{name}(value:#{mirah_type}) returns :void _query.addFilter("#{name}", _eq_op, #{to_datastore(type, 'value')}) end EOF end temp = transformer.tmp model.extend_read(<<-EOF) #{temp} = e.getProperty("#{name}") @#{name} = #{from_datastore(type, temp)} EOF model.extend_save(<<-EOF) e.setProperty("#{name}", #{to_datastore(type, '@' + name)}) EOF model.extend_update(<<-EOF) self.#{name} = properties.get("#{name}") if properties.containsKey("#{name}") EOF model.extend_get_properties(<<-EOF) result.put("#{name}", #{maybe_box(mirah_type, 'self.' + name)}) EOF result << model.eval(parent, <<-EOF) def #{name} @#{name} end def #{name}=(value:#{mirah_type}) @#{name} = value end def #{name}=(value:Object) self.#{name} = #{coercion}(value) end EOF result end |
.find_class(node) ⇒ Object
262 263 264 265 |
# File 'lib/mirah_model/datastore.rb', line 262 def self.find_class(node) node = node.parent until Mirah::AST::ClassDefinition === node node end |
.from_datastore(type, value) ⇒ Object
277 278 279 280 281 282 283 284 285 286 |
# File 'lib/mirah_model/datastore.rb', line 277 def self.from_datastore(type, value) mirah_type = TypeMap[type] if mirah_type default = Defaults.fetch(type, "#{mirah_type}(nil)") conversion = Conversions[type] "(#{value} ? #{type}(#{value}).#{conversion} : #{default})" else "#{type}(#{value})" end end |
.maybe_box(mirah_type, value) ⇒ Object
362 363 364 365 366 367 368 |
# File 'lib/mirah_model/datastore.rb', line 362 def self.maybe_box(mirah_type, value) if Boxes.include?(mirah_type) "#{Boxes[mirah_type]}.valueOf(#{value})" else value end end |
.reset ⇒ Object
370 371 372 |
# File 'lib/mirah_model/datastore.rb', line 370 def self.reset @models = {} end |
.to_datastore(type, value) ⇒ Object
267 268 269 270 271 272 273 274 275 |
# File 'lib/mirah_model/datastore.rb', line 267 def self.to_datastore(type, value) if Primitives.include?(type) "#{type}.new(#{value})" elsif TypeMap.include?(type) "(#{value} ? #{type}.new(#{value}) : nil)" else value end end |