Sha256: 71dfe72c2ac9a63db916d39d2277a34a5d0b9bd2b6ea0eb30151fb5b6b860e04
Contents?: true
Size: 1.57 KB
Versions: 1
Compression:
Stored size: 1.57 KB
Contents
module StaticRecord # Provides has_static_record when included module HasStaticRecord def self.included(base) base.extend(ClassMethods) end module ClassMethods # :nodoc: def has_static_record(table_name, options = nil) options ||= {} class_eval do define_setter(table_name, options) define_getter(table_name, options) end end private def define_setter(table_name, options) define_method("#{table_name}=") do |static_record| unless static_record.class.pkey err = "No primary key has been defined for #{static_record.class}" raise NoPrimaryKey, err end table = __method__.to_s.delete('=') options[:class_name] ||= table.camelize superklass = static_record.class.superclass unless superklass.to_s == options[:class_name] err = "Record must be an instance of #{options[:class_name]}" raise ClassError, err end send(:"#{table}_static_record_type=", static_record.class.name) end end def define_getter(table_name, options) define_method(table_name) do table = __method__.to_s record_type = send(:"#{table}_static_record_type") return nil unless record_type options[:class_name] ||= table.camelize # eager loading may be disabled, initialize parent class superklass = options[:class_name].constantize superklass.find_by(klass: record_type) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
static-record-1.0.0.pre.4 | lib/static_record/migrations/has_static_record.rb |