| Class | Mack::Data::Bridge |
| In: |
lib/mack-data_factory/orm_api_bridge/bridge.rb
|
| Parent: | Object |
Different ORMs have different API at getting object from the store. The ORM bridge is an attempt to have a common API that the data_factory can use to get at the objects it needed.
Currently there are 2 orm bridges implemented: DataMapper and ActiveRecord. But developers are free to develop another adapter.
Although this feature is called an "ORM" bridge, the API that it‘s trying to bridge doesn‘t necessarily have to be of an ORM. As long as the module you‘re bridging can respond to these API, you can use it as a valid adapter.
This feature is an advanced feature of the Data Factory, and it‘s very useful in a case where you have some legacy data sitting somewhere, or you may have a collection of data that is obtained through a very costly sql query (so doing it many times is obviously not acceptable). So you may want to build a class that can preload all the data, then register itself to the OrmRegistry.
The most important method that a "handler" must implement is the can_handle method. When the DataFactory is handling a certain object, it will attempt to find the appropriate API module that can handle that object, so it will ask all the registered handlers of the OrmRegistry, and the first one to answer yes to the question will be the handler of the object. When a handler say yes to the question, it‘s expected that the handler implement all the methods defined in the Bridge class.
# File lib/mack-data_factory/orm_api_bridge/bridge.rb, line 41
41: def initialize
42: OrmRegistry.add(Mack::Data::OrmBridge::ActiveRecord.new)
43: OrmRegistry.add(Mack::Data::OrmBridge::DataMapper.new)
44: end
Get the total number of records for the given obj model
Parameters:
obj: the object model class args: the list of arguments
# File lib/mack-data_factory/orm_api_bridge/bridge.rb, line 87
87: def count(obj, *args)
88: handler(obj).count(obj, *args)
89: end
Get a record from the given obj model. In active record implementation: this will get translated to obj.find(*args)
Parameters:
obj: the object model class args: the list of arguments
# File lib/mack-data_factory/orm_api_bridge/bridge.rb, line 55
55: def get(obj, *args)
56: handler(obj).get(obj, *args)
57: end
Get all records from the given obj model
Parameters:
obj: the object model class args: the list of arguments
# File lib/mack-data_factory/orm_api_bridge/bridge.rb, line 66
66: def get_all(obj, *args)
67: handler(obj).get_all(obj, *args)
68: end
Get the first record from the given obj model
Parameters:
obj: the object model class args: the list of arguments
# File lib/mack-data_factory/orm_api_bridge/bridge.rb, line 77
77: def get_first(obj, *args)
78: handler(obj).get_first(obj, *args)
79: end