This class is the internal implementation of eager_graph. It is responsible for taking an array of plain hashes and returning an array of model objects with all eager_graphed associations already set in the association cache.
Methods
Public Class
Public Instance
Attributes
after_load_map | [R] |
Hash with table alias symbol keys and after_load hook values |
alias_map | [R] |
Hash with table alias symbol keys and association name values |
column_maps | [R] |
Hash with table alias symbol keys and subhash values mapping column_alias symbols to the symbol of the real name of the column |
dependency_map | [R] |
Recursive hash with table alias symbol keys mapping to hashes with dependent table alias symbol keys. |
limit_map | [R] |
Hash with table alias symbol keys and [limit, offset] values |
master | [R] |
The table alias symbol for the primary model |
primary_keys | [R] |
Hash with table alias symbol keys and primary key symbol values (or arrays of primary key symbols for composite key tables) |
reciprocal_map | [R] |
Hash with table alias symbol keys and reciprocal association symbol values, used for setting reciprocals for one_to_many associations. |
records_map | [R] |
Hash with table alias symbol keys and subhash values mapping primary key symbols (or array of symbols) to model instances. Used so that only a single model instance is created for each object. |
reflection_map | [R] |
Hash with table alias symbol keys and |
row_procs | [R] |
Hash with table alias symbol keys and callable values used to create model instances |
type_map | [R] |
Hash with table alias symbol keys and true/false values, where true means the association represented by the table alias uses an array of values instead of a single value (i.e. true => *_many, false => *_to_one). |
Public Class methods
Initialize all of the data structures used during loading.
# File lib/sequel/model/associations.rb 3793 def initialize(dataset) 3794 opts = dataset.opts 3795 eager_graph = opts[:eager_graph] 3796 @master = eager_graph[:master] 3797 requirements = eager_graph[:requirements] 3798 reflection_map = @reflection_map = eager_graph[:reflections] 3799 reciprocal_map = @reciprocal_map = eager_graph[:reciprocals] 3800 limit_map = @limit_map = eager_graph[:limits] 3801 @unique = eager_graph[:cartesian_product_number] > 1 3802 3803 alias_map = @alias_map = {} 3804 type_map = @type_map = {} 3805 after_load_map = @after_load_map = {} 3806 reflection_map.each do |k, v| 3807 alias_map[k] = v[:name] 3808 after_load_map[k] = v[:after_load] if v[:after_load] 3809 type_map[k] = if v.returns_array? 3810 true 3811 elsif (limit_and_offset = limit_map[k]) && !limit_and_offset.last.nil? 3812 :offset 3813 end 3814 end 3815 after_load_map.freeze 3816 alias_map.freeze 3817 type_map.freeze 3818 3819 # Make dependency map hash out of requirements array for each association. 3820 # This builds a tree of dependencies that will be used for recursion 3821 # to ensure that all parts of the object graph are loaded into the 3822 # appropriate subordinate association. 3823 dependency_map = @dependency_map = {} 3824 # Sort the associations by requirements length, so that 3825 # requirements are added to the dependency hash before their 3826 # dependencies. 3827 requirements.sort_by{|a| a[1].length}.each do |ta, deps| 3828 if deps.empty? 3829 dependency_map[ta] = {} 3830 else 3831 deps = deps.dup 3832 hash = dependency_map[deps.shift] 3833 deps.each do |dep| 3834 hash = hash[dep] 3835 end 3836 hash[ta] = {} 3837 end 3838 end 3839 freezer = lambda do |h| 3840 h.freeze 3841 h.each_value(&freezer) 3842 end 3843 freezer.call(dependency_map) 3844 3845 datasets = opts[:graph][:table_aliases].to_a.reject{|ta,ds| ds.nil?} 3846 column_aliases = opts[:graph][:column_aliases] 3847 primary_keys = {} 3848 column_maps = {} 3849 models = {} 3850 row_procs = {} 3851 datasets.each do |ta, ds| 3852 models[ta] = ds.model 3853 primary_keys[ta] = [] 3854 column_maps[ta] = {} 3855 row_procs[ta] = ds.row_proc 3856 end 3857 column_aliases.each do |col_alias, tc| 3858 ta, column = tc 3859 column_maps[ta][col_alias] = column 3860 end 3861 column_maps.each do |ta, h| 3862 pk = models[ta].primary_key 3863 if pk.is_a?(Array) 3864 primary_keys[ta] = [] 3865 h.select{|ca, c| primary_keys[ta] << ca if pk.include?(c)} 3866 else 3867 h.select{|ca, c| primary_keys[ta] = ca if pk == c} 3868 end 3869 end 3870 @column_maps = column_maps.freeze 3871 @primary_keys = primary_keys.freeze 3872 @row_procs = row_procs.freeze 3873 3874 # For performance, create two special maps for the master table, 3875 # so you can skip a hash lookup. 3876 @master_column_map = column_maps[master] 3877 @master_primary_keys = primary_keys[master] 3878 3879 # Add a special hash mapping table alias symbols to 5 element arrays that just 3880 # contain the data in other data structures for that table alias. This is 3881 # used for performance, to get all values in one hash lookup instead of 3882 # separate hash lookups for each data structure. 3883 ta_map = {} 3884 alias_map.each_key do |ta| 3885 ta_map[ta] = [row_procs[ta], alias_map[ta], type_map[ta], reciprocal_map[ta]].freeze 3886 end 3887 @ta_map = ta_map.freeze 3888 freeze 3889 end
Public Instance methods
Return an array of primary model instances with the associations cache prepopulated for all model objects (both primary and associated).
# File lib/sequel/model/associations.rb 3893 def load(hashes) 3894 # This mapping is used to make sure that duplicate entries in the 3895 # result set are mapped to a single record. For example, using a 3896 # single one_to_many association with 10 associated records, 3897 # the main object column values appear in the object graph 10 times. 3898 # We map by primary key, if available, or by the object's entire values, 3899 # if not. The mapping must be per table, so create sub maps for each table 3900 # alias. 3901 @records_map = records_map = {} 3902 alias_map.keys.each{|ta| records_map[ta] = {}} 3903 3904 master = master() 3905 3906 # Assign to local variables for speed increase 3907 rp = row_procs[master] 3908 rm = records_map[master] = {} 3909 dm = dependency_map 3910 3911 records_map.freeze 3912 3913 # This will hold the final record set that we will be replacing the object graph with. 3914 records = [] 3915 3916 hashes.each do |h| 3917 unless key = master_pk(h) 3918 key = hkey(master_hfor(h)) 3919 end 3920 unless primary_record = rm[key] 3921 primary_record = rm[key] = rp.call(master_hfor(h)) 3922 # Only add it to the list of records to return if it is a new record 3923 records.push(primary_record) 3924 end 3925 # Build all associations for the current object and it's dependencies 3926 _load(dm, primary_record, h) 3927 end 3928 3929 # Remove duplicate records from all associations if this graph could possibly be a cartesian product 3930 # Run after_load procs if there are any 3931 post_process(records, dm) if @unique || !after_load_map.empty? || !limit_map.empty? 3932 3933 records_map.each_value(&:freeze) 3934 freeze 3935 3936 records 3937 end