Sha256: 9dd72b4a8456091d1d2f97ecd901a175af9fd41e72ed10590800b6d162b1ed31

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

# encoding: utf-8

module Assertion

  module DSL

    # Allows adding aliases to values of the `#attributes` hash.
    #
    module Attributes

      # List of declared attributes
      #
      # @return [Array<Symbol>]
      #
      def attributes
        @attributes ||= []
      end

      # Declares new attribute(s) by name(s)
      #
      # @param [#to_sym, Array<#to_sym>] names
      #
      # @return [undefined]
      #
      # @raise [NameError]
      #   When a given name is either used by instance methods,
      #   or reserved by the `#check` method to be implemented later.
      #
      def attribute(*names)
        names.flatten.map(&:to_sym).each(&method(:__add_attribute__))
      end

      # @private
      def self.extended(klass)
        klass.__send__(:define_method, :attributes) { @attributes ||= {} }
      end

      private

      def __add_attribute__(name)
        __check_attribute__(name)
        attributes << define_method(name) { attributes[name] }
      end

      def __check_attribute__(name)
        return unless (instance_methods << :check).include? name
        fail NameError.new "#{self}##{name} is already defined"
      end

    end # module Attributes

  end # module DSL

end # module Assertion

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
assertion-0.2.2 lib/assertion/dsl/attributes.rb
assertion-0.2.1 lib/assertion/dsl/attributes.rb