1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
module Virtus
# Class methods that are added when you include Virtus
module ClassMethods
include Extensions::Methods
include ConstMissingExtensions
# Hook called when module is extended
#
# @param [Class] descendant
#
# @return [undefined]
#
# @api private
def self.extended(descendant)
super
descendant.send(:include, AttributeSet.create(descendant))
end
private_class_method :extended
# Returns all the attributes defined on a Class
#
# @example
# class User
# include Virtus
#
# attribute :name, String
# attribute :age, Integer
# end
#
# User.attribute_set # =>
#
# TODO: implement inspect so the output is not cluttered - solnic
#
# @return [AttributeSet]
#
# @api public
def attribute_set
@attribute_set
end
# @see Virtus::ClassMethods.attribute_set
#
# @deprecated
#
# @api public
def attributes
warn "#{self}.attributes is deprecated. Use #{self}.attribute_set instead: #{caller.first}"
attribute_set
end
private
# Setup descendants' own Attribute-accessor-method-hosting modules
#
# Descendants inherit Attribute accessor methods via Ruby's inheritance
# mechanism: Attribute accessor methods are defined in a module included
# in a superclass. Attributes defined on descendants add methods to the
# descendant's Attributes accessor module, leaving the superclass's method
# table unaffected.
#
# @param [Class] descendant
#
# @return [undefined]
#
# @api private
def inherited(descendant)
super
AttributeSet.create(descendant)
descendant.module_eval { include attribute_set }
end
# The list of allowed public methods
#
# @return [Array<String>]
#
# @api private
def allowed_methods
public_instance_methods.map(&:to_s)
end
# @api private
def assert_valid_name(name)
if instance_methods.include?(:attributes) && name.to_sym == :attributes
raise ArgumentError, "#{name.inspect} is not allowed as an attribute name"
end
end
end # module ClassMethods
end # module Virtus
|