Class: Lore::Model
- Inherits:
-
Table_Accessor
- Object
- Table_Accessor
- Lore::Model
- Extended by:
- Aspect, Cache::Cacheable, Query_Shortcuts
- 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::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
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 Method Summary collapse
-
#by_id(pkey_id) ⇒ Object
extend Lore::Migration.
Methods included from Cache::Cacheable
create_entity_cache, entity_cache, flush_entity_cache, read_entity_cache, use_entity_cache
Methods included from Query_Shortcuts
all, all_with, delete, delete_all, each, find, set, value_of
Methods included from Aspect
after_commit, after_instance_delete, before_commit, before_instance_delete
Methods inherited from Table_Accessor
[], __associations__, __attributes__, __delete_strategy__, __filters__, __insert_strategy__, __select_strategy__, __update_strategy__, all_table_names, #attribute_values_by_table, create, delete, disable_output_filters, distribute_attrib_values, enable_output_filters, explicit_insert, get, get_fields, get_fields_flat, get_own_primary_keys, get_primary_keys, get_sequences, #initialize, insert, inspect, is_a?, key_array, load, #log, log, method_missing, new_polymorphic, org_is_a?, output_filters_disabled?, polymorphic_select, polymorphic_select_query, polymorphic_select_value, polymorphic_select_values, select, select_query, select_value, select_values, set_sequences, table_name, update
Methods included from Prepare
Methods included from Model_Shortcuts
Methods included from Polymorphic_Class_Methods
#is_polymorphic, #is_polymorphic?, #polymorphic_attribute, #polymorphic_attribute_index, #select_polymorphic
Methods included from Prepare_Class_Methods
Methods included from Polymorphic_Instance_Methods
Methods included from Model_Instance
#<=>, #==, #===, #[], #abs_attr, #attr, #attribute_values, #commit, #delete, #get_attribute_value_map, #get_attribute_values, #get_label_string, #get_primary_key_value_map, #get_primary_key_values, #id, #inspect, #is_cached_entity?, #key, #marshal_dump, #marshal_load, #method_missing, #obj_id, #pkey, #pkeys, #set_attribute_value, #set_attribute_values, #table_accessor, #touch, #touched?, #untouch, #update_pkey_values, #update_values
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::Model_Instance
Instance Method Details
#by_id(pkey_id) ⇒ Object
extend Lore::Migration
119 120 121 |
# File 'lib/lore/model.rb', line 119 def by_id(pkey_id) _by_id(pkey_id).first # Auto-defined in Lore::Table_Accessor.primary_key end |