Sha256: fde4eb22612585d43ff6817f2237df7804c2dd26cd51a6faad2347dcdec8731b
Contents?: true
Size: 1.94 KB
Versions: 1
Compression:
Stored size: 1.94 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(&:to_sym) association_names | @default_schema.association_names end private def base Object.const_get(@base_name) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
upgrow-0.0.5 | lib/upgrow/active_record_schema.rb |