Class: Lore::Model
- Inherits:
-
Table_Accessor
- Object
- Table_Accessor
- Lore::Model
- Extended by:
- Aspect, Cache::Cacheable, Migration, Query_Shortcuts, Validation
- Defined in:
- lib/lore/model.rb
Overview
For API details see Lore::Table_Accessor
How to define models
Example:
require 'lore/model'
class Vehicle < Lore::Model
end
class Car < Vehicle
end
Sets following defaults:
class Vehicle
table :vehicle, :vehicle_id
primary_key :id # or :vehicle_id if :id is not present
end
class Car < Vehicle
table :car, :public
primary_key :id # or :car_id if :id is not present
is_a Vehicle, :vehicle_id
end
How to use models
Creating entities (INSERTs)
manuf_1 = Manufacturer.create(:name => 'Audi')
manuf_2 = Manufacturer.create(:name => 'BMW')
car = Car.create(:manufacturer => manuf, :name => 'TT')
Changing entities (UPDATEs)
car['name'] = 'BMW318i'
car.manufacturer = manuf_2 # same as car[:manufacturer_id] = manuf_2.manufacturer_id
car.commit
Deleting entities (DELETEs)
car.delete!
is the same as
Car.delete.where(Car.car_id == car.car_id).perform
is the same as
Lore.perform Car.delete.where(Car.car_id == car.car_id)
is the same as
Car.delete { |c|
c.where(c.car_id = car.car_id)
}
How to disable/enable features
Lore::Model extends
-
Lore::Cache::Cacheable
-
Lore::Query_Shortcuts
-
Lore::Validation
-
Lore::Aspect
Each of them is optional. If you want, for example, a minimalistic version of Lore::Model with comfortable query interfaces you can define your own Model class that extends the Lore::Query_Shortcuts module only:
class Slim_Model < Lore::Table_Accessor
extend Lore::Query_Shortcuts
end
You can use it as base class for other Model classes, too:
class Cache_Model < Slim_Model
extend Lore::Query_Shortcuts
extend Lore::Cacheable
end
You also can define this differently for every model by deriving them from Lore::Table_Accessor and extending every single one of them:
class User < Lore::Table_Accessor
extend Lore::Query_Shortcuts
extend Lore::Aspect
extend Lore::Validation
table :user, :public
primary_key :user_id
end
How to use Lore::Model in a Rails app
By default, Lore::Model doesn’t extend Rails interface bridges. You either can define your own Model base class (see: How to disable/enable features) and extend it by Lore::Rails::Model
class My_Model < Lore::Model
extend Lore::Rails::Model
end
… or extend Lore::Model directly:
Lore::Model.extend Lore::Rails_Bridge
In this case, every Model in your app will include Rails interfaces.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Table_Accessor
#attribute_types, #attributes, #constraints, #foreign_keys, #primary_keys
Instance Method Summary collapse
Methods included from Cache::Cacheable
cache_params, create_cache, create_cache!, invalidate, invalidate_all, is_cacheable, is_cached, read_cache
Methods included from Query_Shortcuts
all, all_with, delete, delete_all, each, find, set, value_of
Methods included from Validation
Methods included from Aspect
after_commit, after_instance_delete, before_commit, before_instance_delete
Methods included from Migration
bootstrap, has_attributes, update_schema
Methods inherited from Table_Accessor
[], context, create, create_entity_cache, create_shallow, delete, entity_cache, execute_prepared, explicit_insert, flush_entity_cache, get_all_table_names, get_constraints, get_context, get_explicit_attributes, get_foreign_keys, get_implicit_attributes, get_primary_keys, get_sequences, get_table_name, get_table_names, #initialize, inspect, key_array, load, #log, log, method_missing, prepare, read_entity_cache, select, select_by_key, select_query, select_value, select_values, set_explicit_attributes, set_foreign_keys, set_implicit_attributes, set_sequences, set_table_name, table, table_name, update, use_entity_cache, validate_params?
Methods included from Model_Shortcuts
Methods included from Table_Instance
#<=>, #==, #===, #[], #[]=, #abs_attr, #attr, #commit, #delete, #get_attribute_values, #get_label_string, #get_primary_key_values, #id, #inspect, #is_cached_entity?, #key, #marshal_dump, #marshal_load, #method_missing, #move, #set_attribute_value, #setup_instance, #table_accessor, #touched?
Constructor Details
This class inherits a constructor from Lore::Table_Accessor
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Lore::Table_Instance
Instance Method Details
#by_id(pkey_id) ⇒ Object
134 135 136 |
# File 'lib/lore/model.rb', line 134 def by_id(pkey_id) _by_id(pkey_id).first # Auto-defined in Lore::Table_Accessor.primary_key end |