Sha256: 115712d1e473a81b1916c4567ee27b802e7c90abce6bf7804ab1cf3c45de14a8
Contents?: true
Size: 1.98 KB
Versions: 2
Compression:
Stored size: 1.98 KB
Contents
# frozen_string_literal: true module Upgrow # A Schema that dynamically infers attribute names from an Active Record Base, # plus the attributes from an original Schema. # # This object is used to power Models so they can include Active Record # attributes from a matching class based on the Model's name. class ActiveRecordSchema # Set the Schema's initial state. # # @param base_name [String] the name of the Active Record Base class that # should be used to fetch attribute names. # @param default_schema [ModelSchema] the original Model Schema to be used # to fetch and define custom attributes. def initialize(base_name, default_schema) @base_name = base_name @default_schema = default_schema end # Define a custom attribute in the default Schema. # # @param name [Symbol] the name of the new attribute. def attribute(name) @default_schema.attribute(name) end # Define a custom association in the default Schema. # # @param name [Symbol] the name of the association. def association(name) @default_schema.association(name) end # The list of attribute names. This is an aggregate of both the attributes # from the Active Record Base as well as any custom attributes from the # default Schema. # # @return [Array<Symbol>] the list of attribute names. def attribute_names base.attribute_names.map(&:to_sym) | @default_schema.attribute_names end # The list of association names. This is an aggregate of both the # associations from the Active Record Base as well as any custom # associations from the default Schema. # # @return [Array<Symbol>] the list of attribute names. def association_names association_names = base.reflections.keys.map do |key| key.sub('_record', '').to_sym end association_names | @default_schema.association_names end private def base Object.const_get(@base_name) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
upgrow-0.0.4 | lib/upgrow/active_record_schema.rb |
upgrow-0.0.3 | lib/upgrow/active_record_schema.rb |