Sha256: d293428f2210412bab5d9a565d2dd17594bad182c557fabb7c443b1513e8b74d

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

# encoding: utf-8

module Assertion

  # Collection of DSLs for various modules and classes
  #
  module DSL

    # Allows adding aliases to `#object` method.
    #
    module Attribute

      # Adds alias to the [#object] method
      #
      # @param [#to_sym] name
      #
      # @return [undefined]
      #
      # @raise [NameError]
      #   When a given name is either used by instance methods,
      #   or reserved by the `#state` method to be implemented later.
      #
      def attribute(name)
        __check_attribute__(name)
        alias_method name, :object
      end

      private

      # Ensures the `#object` is defined
      #
      # @param [Class] klass
      #
      # @return [undefined]
      #
      def self.extended(klass)
        klass.__send__ :attr_reader, :object
      end

      # Checks if alias name for `#object` is free
      #
      # @param [#to_sym] key
      #
      # @return [undefined]
      #
      # @raise [NameError] if the key is already in use
      #
      def __check_attribute__(key)
        return unless (instance_methods << :state).include? key.to_sym
        fail NameError.new "#{self}##{key} is already defined"
      end

    end # module Attribute

  end # module DSL

end # module Assertion

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
assertion-0.2.5 lib/assertion/dsl/attribute.rb
assertion-0.2.4 lib/assertion/dsl/attribute.rb