class ObjectFramework::Persistence::Mappings
Owns the application’s mappings without modifying domain classes.
mappings = ObjectFramework::Persistence::Mappings.new mappings.define(Order, table: :orders) do field :id field :status end
Public Class Methods
Source
# File lib/object_framework/persistence/mapping.rb, line 155 def initialize @mappings = {} end
Public Instance Methods
Source
# File lib/object_framework/persistence/mapping.rb, line 161 def define(entity_class, table:, &description) raise ConfigurationError, "Mappings are finalized" if @mappings.frozen? raise ConfigurationError, "Duplicate entity mapping" if @mappings.key?(entity_class) raise ConfigurationError, "Duplicate table mapping" if @mappings.values.any? { |mapping| mapping.table == table.to_sym } mapping = Mapping.new(entity_class, table:) mapping.instance_eval(&description) if description @mappings[entity_class] = mapping end
Registers one Mapping per domain class and table. Evaluates the declaration block against that mapping and returns it.
Source
# File lib/object_framework/persistence/mapping.rb, line 170 def each(&block) @mappings.values.each(&block) end
Source
# File lib/object_framework/persistence/mapping.rb, line 174 def fetch(entity_class) @mappings.fetch(entity_class) { raise ConfigurationError, "Unmapped entity: #{entity_class}" } end
Source
# File lib/object_framework/persistence/mapping.rb, line 178 def validate! each do |mapping| mapping.identity mapping.associations.each do |association| target = fetch(association.target) if association.kind == :has_many && !target.columns.include?(association.foreign_key) raise ConfigurationError, "Collection foreign key is not mapped" end end end each(&:finalize!) @mappings.freeze self end