Sha256: 6fbe1088447e550844002f2e72419bde9d3ee8833f22a893e9e55a3dac81f7c0

Contents?: true

Size: 1.84 KB

Versions: 6

Compression:

Stored size: 1.84 KB

Contents

module Virtus

  # Virtus module that can define attributes for later inclusion
  #
  # @private
  module ModuleExtensions
    include ConstMissingExtensions

    # @api private
    def self.extended(mod)
      super
      setup(mod)
    end

    # @api private
    def self.setup(mod, inclusions = [Model], attribute_definitions = [])
      mod.instance_variable_set('@inclusions', inclusions)
      mod.instance_variable_set('@attribute_definitions', attribute_definitions)
    end

    # Define an attribute in the module
    #
    # @see Virtus::Extensions#attribute
    #
    # @return [self]
    #
    # @api private
    def attribute(name, type, options = {})
      @attribute_definitions << [name, type, options]
      self
    end

    private

    # Extend an object with Virtus methods and define attributes
    #
    # @param [Object] object
    #
    # @return [undefined]
    #
    # @api private
    def extended(object)
      super
      @inclusions.each { |mod| object.extend(mod) }
      define_attributes(object)
      object.set_default_attributes
    end

    # Extend a class with Virtus methods and define attributes
    #
    # @param [Object] object
    #
    # @return [undefined]
    #
    # @api private
    def included(object)
      super

      if Class === object
        @inclusions.each { |mod| object.send(:include, mod) }
        define_attributes(object)
      else
        object.extend(ModuleExtensions)
        ModuleExtensions.setup(object, @inclusions, @attribute_definitions)
      end
    end

    # Define attributes on a class or instance
    #
    # @param [Object,Class] object
    #
    # @return [undefined]
    #
    # @api private
    def define_attributes(object)
      @attribute_definitions.each do |attribute_args|
        object.attribute(*attribute_args)
      end
    end

  end # module ModuleExtensions
end # module Virtus

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
virtus-1.0.0.beta8 lib/virtus/module_extensions.rb
virtus-1.0.0.beta7 lib/virtus/module_extensions.rb
virtus-1.0.0.beta6 lib/virtus/module_extensions.rb
virtus-1.0.0.beta5 lib/virtus/module_extensions.rb
virtus-1.0.0.beta4 lib/virtus/module_extensions.rb
virtus-1.0.0.beta3 lib/virtus/module_extensions.rb